自动完成文本视图不起作用 [英] Autocompletetextview not working

查看:36
本文介绍了自动完成文本视图不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to implement autocompletetextview in my application,for that I follow this tutorial http://manishkpr.webheavens.com/android-autocompletetextview-example-json/

我有以下回复...

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

我得到的这个输出......在输入'adi'之后的输出中它也显示'cat1'......

and this output i am getting...in following output after type 'adi' it displays 'cat1' too..

 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;

   }


  }

适配器文件

 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> 

推荐答案

检查这个

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

这篇关于自动完成文本视图不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆