-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFillAwareAndroid.java
More file actions
139 lines (127 loc) · 4.16 KB
/
Copy pathFillAwareAndroid.java
File metadata and controls
139 lines (127 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.example.testapp;
import android.content.Context;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import android.widget.Filterable;
public class AutoSuggestAdapter extends ArrayAdapter<String> implements Filterable
{
private ArrayList<String> mDataList = new ArrayList<>();
private String mType; // Available types are: names, companies, first_names, last_names, emails, addresses, colors
private String APIKey = "iSkwEtRMxzOFzWwoy8GEvsL7DMlpn94Uffrg8ETYMOlrsspEZI7Ck_ElqvevdIxz";
public AutoSuggestAdapter(Context context, int resource, String type)
{
super(context, resource);
mType = type;
}
@Override
public int getCount()
{
return mDataList.size();
}
@Override
public String getItem(int position)
{
return mDataList.get(position);
}
@Override
@NonNull
public Filter getFilter()
{
return new Filter()
{
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
FilterResults filterResults = new FilterResults();
if(constraint != null)
{
try
{
//get data from the web
String term = constraint.toString();
mDataList = new UpdateList().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new String[]{term, mType, APIKey}).get();
}
catch (Exception e)
{
Log.d("AutoCompleteAdapter", "Exception: " + e);
}
filterResults.values = mDataList;
filterResults.count = mDataList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
{
if(results != null && results.count > 0)
{
notifyDataSetChanged();
}
else
{
notifyDataSetInvalidated();
}
}
};
}
}
class UpdateList extends AsyncTask<String, Integer, ArrayList<String>>
{
@Override
protected ArrayList<String> doInBackground(String... strings)
{
ArrayList<String> result = new ArrayList<>();
HttpURLConnection client = null;
try
{
client = (HttpURLConnection) new URL("https://api.fillaware.com/v1/suggest/" + strings[1] + "?q=" + strings[0] + "&key=" + strings[2]).openConnection();
client.setRequestMethod("GET");
}
catch (IOException e)
{
Log.e("AutoSuggestAdapter", "HttpConnection exception: " + e);
}
StringBuilder string = new StringBuilder();
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line;
while((line = reader.readLine()) != null)
{
string.append(line).append('\n');
}
}
catch (IOException e)
{
Log.e("AutoSuggestAdapter", "Reader exception: " + e);
}
finally
{
client.disconnect();
}
try
{
JSONArray json = new JSONArray(string.toString());
for (int i = 0; i < json.length(); i++)
{
result.add(json.getString(i));
}
}
catch (JSONException e)
{
Log.e("AutoSuggestAdapter", "JSON exception: " + e);
}
return result;
}
}