Multiautocompletetextview not working properly

佐手、 提交于 2019-12-11 13:58:11

问题


I am using multiautocomplete textview in my app,the items which I use with mutltiautocompletetextview is from json,now What I want is whenever user selects more than one value I need to get id of that item,I am able to get id of first item only,..

My response

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

回答1:


so you need full catids, so first change catid to array or list.

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

then change your code

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


              } 
             }


来源:https://stackoverflow.com/questions/29529819/multiautocompletetextview-not-working-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!