Multiautocompletetextview工作不正常(Multiautocompletete

2019-10-22 14:05发布

我使用multiautocomplete TextView的在我的应用程序,这是我与mutltiautocompletetextview使用的项目是从JSON,现在只要用户选择多个值,我需要得到该项目的ID我想是的,我能得到第一项的ID只要,..

我的回复

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

JsonParse.java

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("");
       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;

   }
}

SuggestGetSet.java

public class SuggestGetSet {
String id,name;
public SuggestGetSet(String id, String name){
    this.setId(id);
    this.setName(name);
}
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

 }

SuggetionAdapter.java

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) {
                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());
                }*/

                for (int i=0;i<new_suggestions.size();i++) {
                    String name=new_suggestions.get(i).getName();
                    String id=new_suggestions.get(i).getId();
                    System.out.println("checis id"+id);

                    if(name.contains(constraint)){
                    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;
}
 }

MainActivity.java

          JsonParse jp=new JsonParse();
              List<NameValuePair> params = new ArrayList<NameValuePair>();
              List<SuggestGetSet> list =jp.getParseJsonWCF(acTextView.getText().toString());
              for(int i = 0;i<list.size();i++){
                if(list.get(i).getName().equals(acTextView.getText().toString()))
                 params.add(new BasicNameValuePair("parentid",list.get(i).getId()));
                System.out.println("what goes"+params);
                catid=list.get(i).getId().toString();
              }

Answer 1:

所以你需要充分catids,所以第一个变化CATID到数组或列表。

private List<String> catid=new ArrayList<String>();

然后改变你的代码

             String cats[]=acTextView.getText().toString().split(",");
             JsonParse jp=new JsonParse();
             for(String cat:cats){
                 List<SuggestGetSet> list =jp.getParseJsonWCF(acTextView.getText().toString());
              for(int i = 0;i<list.size();i++){
                if(list.get(i).getName().equals(cat))
                    catid.add(list.get(i).getId().toString());
                 //params.add(new BasicNameValuePair("parentid",list.get(i).getId()));


              } 
             }


文章来源: Multiautocompletetextview not working properly