Listview TextWatcher过滤器排序错误的结果

时间:2014-10-12 16:26:09

标签: android listview filter android-edittext

Heyy家伙我列出哪个菜单显示菜单。现在要排序那些我已定义EditText的列表项目,因此用户在此edittext中输入列表项目由TextWatcher过滤。但问题是listview给了我错误的items.as我键入第一个字符,它给我列表的前6项,因为它是,当我键入第二个字符它给我列表的第一项,即使这些项目字符串中没有包含这些项目字符串。什么是错误?

 public class test extends Activity  {

 EditText editsearch;
LinearLayout Layout;    


 String temp;
 private ListView listview;
 private DisplayMetrics metrics;
 String[] values = new String[] { "American","Algerian ", "Andhra", "Anhui",   "Arabian", "Asian", 
     "Assamese", "Awadhi", "Bakery", "Beijing", "Bengali", 
     "Beverages" , "Bhojpuri", "Biryani", "Cafe", "Cameroonian",
     "canadian", "Cantonese", "Centrafrican", "Chinese", "Congolese", 
     "Continental", "Desserts", "EastAfrican", "Egyptian", "European",
     "Fast food", "Finger food", "French", "Fujian", "Gaon", "German",
     "Ghanaian", "Greek", "Gujrati", "Hakka ", "Healthy food", "HongKong",
     "Hyderabadi", "Icecream", "Indian", "Indonesian", "Italian", "Ivorian", 
     "Jain ", "Japanese", "Juices", "Kashmiri", "Kenyan", "Kerala",
     "Konkan", "Lucknowi", "Maasai", "Maharashtrian", "Malaysian",
     "Malwani", "Mangalorean", "mexican", "Middleeastern", "Nepalese", 
     "Northeastern", "NorthIndian", "Oriya", "Parsi", "Pizza", "Punjabi ",
     "Rajasthani ", "Rajasthani", "Sea food", "Shanghai", "Sushi", "Thai", 
     "Tibetan", "Tunisian ", "Udupi", "Zhejiang", "Zimbabwean "
      };

    private int mode = 1;
    public void onBackPressed() {
  // TODO Auto-generated method stub
    super.onBackPressed();
    overridePendingTransition (R.anim.slide_in_left, R.anim.slide_out_right);
    }
     @Override
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    //setContentView(R.layout.cusinetype_activity);
     setContentView(R.layout.cusinetype_activity);



      //Layout.setBackgroundResource(R.drawable.cusine_new);
       //setContentView(Layout); //you might be forgetting this
      metrics = new DisplayMetrics();
      getWindowManager().getDefaultDisplay().getMetrics(metrics);

      listview = new ListView(this);
     listview = (ListView) findViewById(R.id.listView1);
     listview.setFadingEdgeLength(0);
      ArrayList<String> strings = new ArrayList<String>();

         for (int i = 0;i<values.length; i++) {
     temp=values[i];
    strings.add(temp);
     }

   final MainAdapter mAdapter = new MainAdapter(this, strings, metrics);
  listview.setAdapter(mAdapter);
     //enables filtering for the contents of the given ListView
   listview.setTextFilterEnabled(true);
 //Pass results to ListViewAdapter Class
   EditText myFilter = (EditText) findViewById(R.id.editText1);
     myFilter.addTextChangedListener(new TextWatcher() {

  public void onTextChanged(CharSequence s, int start, int before,
          int count) {
      mAdapter.getFilter().filter(s);
      mAdapter.notifyDataSetChanged();

  }

  public void beforeTextChanged(CharSequence s, int start, int count,
          int after) {

  }

  public void afterTextChanged(Editable s) {
  }
  });




     //listview.setBackgroundResource(R.drawable.cusine_new);

   listview.setOnItemClickListener(new OnItemClickListener() {
    @Override
   public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {

   // ListView Clicked item index
    int itemPosition     = position;

    // ListView Clicked item value
    String  itemValue    = (String) listview.getItemAtPosition(position);

    Intent in = new Intent(getApplicationContext(),nearby_mainactivity3.class);
    in.putExtra("Value",itemValue );  
    startActivity(in);
      // Show Alert 
   // Toast.makeText(getApplicationContext(),
     // "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
    //  . show();

   }


 }); 

  }




  public class MainAdapter extends ArrayAdapter<String> {
  private Context context;
     private LayoutInflater mInflater;
   private ArrayList<String> strings;
 private DisplayMetrics metrics_;

  private class Holder {
  public TextView textview;
  }

  public MainAdapter(Context context, ArrayList<String> strings,
  DisplayMetrics metrics) {
  super(context, 0, strings);
  this.context = context;
  this.mInflater = (LayoutInflater) this.context
 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  this.strings = strings;
  this.metrics_ = metrics;
   }

  @Override
  public View getView(final int position, View convertView,
 ViewGroup parent) {
  final String str = this.strings.get(position);
 Log.d("timepass", str);
  final Holder holder;



if (convertView == null) {
convertView = mInflater.inflate(
        R.layout.list_item2,null);


holder = new Holder();
holder.textview = (TextView) convertView
  .findViewById(R.id.name);
holder.textview.setTextColor(0xFFFFFFFF);
 // holder.textview.setBackgroundResource(R.drawable.back);

  convertView.setTag(holder);

  } else {
  holder = (Holder) convertView.getTag();
 }

  holder.textview.setText(str);
    Animation animation = null;
    animation = AnimationUtils.loadAnimation(context, R.anim.slide_in_top);
   animation.setDuration(800);
 convertView.startAnimation(animation);
 animation = null;
  return convertView;
  }
 }
 }

1 个答案:

答案 0 :(得分:1)

请浏览this链接,这有助于您解决问题。

提供API是为了让您自己过滤数据的直接功能。我猜系统不会为你做这件事。