アクティビティでAutoCompleteTextView
を使用し、Web APIにクエリを実行して、ユーザーが入力したとおりにデータを入力します。これを行うにはどうすればよいですか?
新しいクラスを作成してAutoCompleteTextView.performFiltering
をオーバーライドしますか、またはカスタムリストアダプターを使用して、performFilteringをオーバーライドするカスタムAndroid.widget.Filter
を提供しますか?
または、私の最終目標を達成するためのより良い方法はありますか?
私はやや似たようなことをしましたが、それはクイック検索ボックス用であり、サービスの実装が含まれていましたが、ここでやりたいことではないと思います。
私は解決策を思いつきました、それが最良の解決策であるかどうかはわかりませんが、それは非常にうまくいくようです。 ArrayAdapterを拡張するカスタムアダプターを作成しました。カスタムアダプターでgetFilterをオーバーライドし、performFilteringをオーバーライドする独自のFilterクラスを作成しました。これにより、新しいスレッドが開始され、UIが中断されなくなります。以下は必要最低限の例です。
MyActivity.Java
public class MyActivity extends Activity {
private AutoCompleteTextView style;
@Override
public void onCreate(Bundle savedInstanceState) {
...
style = (AutoCompleteTextView) findViewById(R.id.style);
adapter = new AutoCompleteAdapter(this, Android.R.layout.simple_dropdown_item_1line);
style.setAdapter(adapter);
}
}
AutoCompleteAdapter.Java
public class AutoCompleteAdapter extends ArrayAdapter<Style> implements Filterable {
private ArrayList<Style> mData;
public AutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
mData = new ArrayList<Style>();
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Style getItem(int index) {
return mData.get(index);
}
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if(constraint != null) {
// A class that queries a web API, parses the data and returns an ArrayList<Style>
StyleFetcher fetcher = new StyleFetcher();
try {
mData = fetcher.retrieveResults(constraint.toString());
}
catch(Exception e) {
Log.e("myException", e.getMessage());
}
// Now assign the values and count to the FilterResults object
filterResults.values = mData;
filterResults.count = mData.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence contraint, FilterResults results) {
if(results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}
};
return myFilter;
}
}
上記の AJ。 の答えを展開すると、次のカスタムアダプターにはサーバーリクエストの処理とJSON解析も含まれます。
class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable
{
private ArrayList<String> data;
private final String server = "http://myserver/script.php?query=";
AutoCompleteAdapter (@NonNull Context context, @LayoutRes int resource)
{
super (context, resource);
this.data = new ArrayList<>();
}
@Override
public int getCount()
{
return data.size();
}
@Nullable
@Override
public String getItem (int position)
{
return data.get (position);
}
@NonNull
@Override
public Filter getFilter()
{
return new Filter()
{
@Override
protected FilterResults performFiltering (CharSequence constraint)
{
FilterResults results = new FilterResults();
if (constraint != null)
{
HttpURLConnection conn = null;
InputStream input = null;
try
{
URL url = new URL (server + constraint.toString());
conn = (HttpURLConnection) url.openConnection();
input = conn.getInputStream();
InputStreamReader reader = new InputStreamReader (input, "UTF-8");
BufferedReader buffer = new BufferedReader (reader, 8192);
StringBuilder builder = new StringBuilder();
String line;
while ((line = buffer.readLine()) != null)
{
builder.append (line);
}
JSONArray terms = new JSONArray (builder.toString());
ArrayList<String> suggestions = new ArrayList<>();
for (int ind = 0; ind < terms.length(); ind++)
{
String term = terms.getString (ind);
suggestions.add (term);
}
results.values = suggestions;
results.count = suggestions.size();
data = suggestions;
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
if (input != null)
{
try
{
input.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
if (conn != null) conn.disconnect();
}
}
return results;
}
@Override
protected void publishResults (CharSequence constraint, FilterResults results)
{
if (results != null && results.count > 0)
{
notifyDataSetChanged();
}
else notifyDataSetInvalidated();
}
};
}
同じように使用します:
public class MyActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState) {
...
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById (R.id.style);
int layout = Android.R.layout.simple_list_item_1;
AutoCompleteAdapter adapter = new AutoCompleteAdapter (this, layout);
textView.setAdapter (adapter);
}
}
Chu:ビューの外観をカスタマイズし、オブジェクトのアンラップをより詳細に制御するには、次の手順を実行します...
@Override
public View getView (int position, View convertView, ViewGroup parent) {
TextView originalView = (TextView) super.getView(position, convertView, parent); // Get the original view
final LayoutInflater inflater = LayoutInflater.from(getContext());
final TextView view = (TextView) inflater.inflate(Android.R.layout.simple_dropdown_item_1line, parent, false);
// Start tweaking
view.setText(originalView.getText());
view.setTextColor(R.color.black); // also useful if you have a color scheme that makes the text show up white
view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10); // override the text size
return view;
}
private AutoCompleteUserAdapter userAdapter;
private AutoCompleteTextView actvName;
private ArrayList<SearchUserItem> arrayList;
actvName = findViewById(R.id.actvName);
actvName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
actvName.setText(userAdapter.getItemNameAtPosition(position));
actvName.setSelection(actvName.getText().toString().trim().length());
}
});
actvName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
if (actvName.isPerformingCompletion()) {
// An item has been selected from the list. Ignore.
} else {
if (s.toString().toLowerCase().trim().length() >= 2) {
getUserList(s.toString().toLowerCase().trim());
}
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
private void getUserList(String searchText) {
//Add data to your list after success of API call
arrayList = new ArrayList<>();
arrayList.addAll(YOUR_LIST);
userAdapter = new AutoCompleteUserAdapter(context, R.layout.row_user, arrayList);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
actvName.setAdapter(userAdapter);
userAdapter.notifyDataSetChanged();
actvName.showDropDown();
}
});
}
AutoCompleteUserAdapter
/**
* Created by Ketan Ramani on 11/07/2019.
*/
public class AutoCompleteUserAdapter extends ArrayAdapter<SearchUserItem> {
private Context context;
private int layoutResourceId;
private ArrayList<SearchUserItem> arrayList;
public AutoCompleteUserAdapter(Context context, int layoutResourceId, ArrayList<SearchUserItem> arrayList) {
super(context, layoutResourceId, arrayList);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.arrayList = arrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(layoutResourceId, parent, false);
}
SearchUserItem model = arrayList.get(position);
AppCompatTextView tvUserName = convertView.findViewById(R.id.tvUserName);
tvUserName.setText(model.getFullname());
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
public String getItemNameAtPosition(int position) {
return arrayList.get(position).getName();
}
public String getItemIDAtPosition(int position) {
return arrayList.get(position).getId();
}
}