Autocompletetextview不起作用

时间:2015-03-21 13:27:23

标签: android json autocompletetextview

我正在尝试在我的应用程序中实现autocompletetextview,因为我遵循本教程http://manishkpr.webheavens.com/android-autocompletetextview-example-json/

我有以下回应......

{
 "category":
  [
   {
      "id":"4",
      "name":"cat1"
   },
   {
      "id":"7",
    "name":"aditya"}
   ]
}

并且这个输出我得到...在'adi'类型后面的输出中它也显示'cat1'..

enter image description here

 public class JsonParse {

double current_latitude,current_longitude;
public JsonParse(){}
public JsonParse(double current_latitude,double current_longitude){
    this.current_latitude=current_latitude;
    this.current_longitude=current_longitude;
}
public List<SuggestGetSet> getParseJsonWCF(String sName)
   {
    List<SuggestGetSet> ListData = new ArrayList<SuggestGetSet>();
    try {
       String temp=sName.replace(" ", "%20");
       URL js = new URL("http://www.mywebsitename.zookas.in/web-service/categorylist.php?action=category");
       URLConnection jc = js.openConnection();
       BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
       String line = reader.readLine();
       JSONObject jsonResponse = new JSONObject(line);
       JSONArray jsonArray = jsonResponse.getJSONArray("category");
       for(int i = 0; i < jsonArray.length(); i++){
           JSONObject r = jsonArray.getJSONObject(i);
           ListData.add(new SuggestGetSet(r.getString("id"),r.getString("name")));
       }
   } catch (Exception e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
   }
    return ListData;

   }


  }

AdapterFile

 public class SuggestionAdapter extends ArrayAdapter<String>{

protected static final String TAG = "SuggestionAdapter";
private List<String> suggestions;
public SuggestionAdapter(Activity context, String nameFilter) {
    super(context, android.R.layout.simple_dropdown_item_1line);
    suggestions = new ArrayList<String>();
}

@Override
public int getCount() {
    return suggestions.size();
}

@Override
public String getItem(int index) {
    return suggestions.get(index);
}

@Override
public Filter getFilter() {
    Filter myFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            JsonParse jp=new JsonParse();
            if (constraint != null) {
                // A class that queries a web API, parses the data and
                // returns an ArrayList<GoEuroGetSet>
                List<SuggestGetSet> new_suggestions =jp.getParseJsonWCF(constraint.toString());
                suggestions.clear();
                for (int i=0;i<new_suggestions.size();i++) {
                    suggestions.add(new_suggestions.get(i).getName());
                }

                // Now assign the values and count to the FilterResults
                // object
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
            }
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence contraint,
                FilterResults results) {
            if (results != null && results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    };
    return myFilter;
}

}

    <LinearLayout 
     android:layout_width="match_parent"
       android:layout_height="match_parent"
        android:orientation="vertical" >

    <AutoCompleteTextView  
    android:id="@+id/autoCompleteTextView1"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:ems="10"  
    android:text=""
    android:layout_marginBottom="3dp"
    android:layout_marginTop="3dp"
    android:background="@drawable/loginedittext"
    android:textColor="@android:color/black"
    android:hint="Search Item...">  

    <requestFocus />  
</AutoCompleteTextView> 

1 个答案:

答案 0 :(得分:1)

检查这个

for (int i=0;i<new_suggestions.size();i++) {
      String name=new_suggestions.get(i).getName();
      if(name.contains(constraint)){
      suggestions.add(new_suggestions.get(i).getName());
         }                  
}