我想创建一个AutoCompleteTextView,用户可以在其中输入邮政编码或城市名称,但只有城市名称应出现在建议信息中下来。
我使用AsyncTask获取Church List,我使用HashMap存储城市名称和邮政编码。
异步类:
// Asynchronous Class for Fetching All Church.
public class FetchAllChurch extends AsyncTask<String, String, String> {
Boolean isJsonObjectNull = false;
HashMap<String, String> cityZipHashMap;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = new ProgressDialog(Search.this);
progressDialog.setMessage("Loading Cities...");
progressDialog.setCancelable(true);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
jsonParser = new JsonParser();
JSONObject jsonObject = jsonParser
.makeHttpRequest(Utility.GET_ALL_CHURCH_URL);
if (jsonObject != null) {
Log.v("All Church: ", jsonObject.toString());
try {
JSONArray jsonArray = jsonObject
.getJSONArray(Utility.SEARCH_RESULT);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject churchJsonObject = jsonArray
.getJSONObject(i);
String ePostalString = churchJsonObject
.getString(Utility.E_POSTAL);
String eZipCodeFullString = churchJsonObject
.getString(Utility.E_ZIP_FULL);
String eCityString = churchJsonObject
.getString(Utility.E_CITY);
cityZipHashMap = new HashMap<String, String>();
cityZipHashMap.put("city", eCityString);
cityZipHashMap.put("zip", ePostalString);
cityZipArrayList.add(cityZipHashMap);
}
// }
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progressDialog.dismiss();
AutoCompleteAdapter adapter = new AutoCompleteAdapter(
getApplicationContext(), android.R.layout.simple_list_item_1, cityZipArrayList);
searchAutoCompleteTextView.setAdapter(adapter);
}
适配器:
这是适配器:
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Filterable;
public class AutoCompleteAdapter extends ArrayAdapter<String>
implements Filterable {
private ArrayList<HashMap<String, String>> mData;
public AutoCompleteAdapter(Context context, int textViewResourceId,
ArrayList<HashMap<String, String>> mData) {
super(context, textViewResourceId);
this.mData = mData;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int index) {
return mData.get(index).toString();
}
@Override
public android.widget.Filter getFilter() {
// TODO Auto-generated method stub
android.widget.Filter filter = new android.widget.Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
// TODO Auto-generated method stub
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// A class that queries a web API, parses the data and
// returns an ArrayList<Style>
ArrayList<String> suggestion = new ArrayList<String>();
for (int i = 0; i < getCount(); i++) {
if (getItem(i).startsWith(
constraint.toString())) {
suggestion.add(getItem(i));
Log.v("Adapter: ", getItem(i)
+ "const" +constraint.toString());
}
}
// Now assign the values and count to the FilterResults
// object
filterResults.values = suggestion;
filterResults.count = suggestion.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// TODO Auto-generated method stub
clear();
@SuppressWarnings("unchecked")
ArrayList<String> newValues = (ArrayList<String>) results.values;
for (int i = 0; i < newValues.size(); i++) {
add(newValues.get(i));
}
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
}
我没有得到预期的结果 提前谢谢。
答案 0 :(得分:1)
请尝试这种方式,希望这有助于您解决问题。
<强> main.xml中强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="5dp">
<AutoCompleteTextView
android:id="@+id/autoComplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<强> MyActivity.java 强>
public class MyActivity extends Activity {
private AutoCompleteTextView autoComplete;
ArrayList<HashMap<String,String>> cityZipList;
ArrayList<HashMap<String,String>> autoCompleteList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cityZipList = new ArrayList<HashMap<String, String>>();
HashMap<String,String> row1 = new HashMap<String, String>();
row1.put("city","AAC");
row1.put("zip", "123");
cityZipList.add(row1);
HashMap<String,String> row2 = new HashMap<String, String>();
row2.put("city","AAD");
row2.put("zip", "231");
cityZipList.add(row2);
HashMap<String,String> row3 = new HashMap<String, String>();
row3.put("city","EFG");
row3.put("zip", "125");
cityZipList.add(row3);
HashMap<String,String> row4 = new HashMap<String, String>();
row4.put("city","AAL");
row4.put("zip", "334");
cityZipList.add(row4);
HashMap<String,String> row5 = new HashMap<String, String>();
row5.put("city","EFD");
row5.put("zip", "235");
cityZipList.add(row5);
HashMap<String,String> row6 = new HashMap<String, String>();
row6.put("city","JKL");
row6.put("zip", "333");
cityZipList.add(row6);
HashMap<String,String> row7 = new HashMap<String, String>();
row7.put("city","JKM");
row7.put("zip", "521");
cityZipList.add(row7);
HashMap<String,String> row8 = new HashMap<String, String>();
row8.put("city","AAN");
row8.put("zip","527");
cityZipList.add(row8);
autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
autoComplete.setAdapter(new AutoCompleteAdapter(this,R.layout.auto_item, cityZipList));
}
private class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
public AutoCompleteAdapter(Context context, int textViewResourceId,ArrayList<HashMap<String,String>> data) {
super(context, textViewResourceId);
}
@Override
public int getCount() {
return autoCompleteList.size();
}
@Override
public String getItem(int position) {
return autoCompleteList.get(position).get("city");
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(final CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
autoCompleteList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < cityZipList.size(); i++) {
if (cityZipList.get(i).get("city").startsWith(
constraint.toString()) || cityZipList.get(i).get("zip").startsWith(
constraint.toString())) {
autoCompleteList.add(cityZipList.get(i));
}
}
// Now assign the values and count to the FilterResults
// object
filterResults.values = autoCompleteList;
filterResults.count = autoCompleteList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
}
}
答案 1 :(得分:0)
使用我在代码中使用的自定义适配器。 我已经使用了这个带有哈希映射的适配器。
SERIALIZABLE