如何在android中的listview中过滤多行

时间:2013-03-06 06:34:51

标签: android android-intent

你好我在这里做一个应用程序我需要在lisview中显示cloumns,我必须将搜索(过滤器)动作设置为该listview,我尝试使用下面几乎正常工作的代码,在过滤器功能中我遇到了问题编辑文本如果输入第一个字母意味着它显示在listview下方它显示该字母相关的名称,但它在两列中显示哪些e输入该字母相关的名称,但我想如果输入字母意味着我需要过滤时间仅基于第一列名字,如果输入?我需要根据2栏过滤任何一个字母。请任何有想法的人建议我......

     ListMobileActivity.class: 
    public class ListMobileActivity extends Activity {EditText edittext;
 ListView listview;
 String qustion="?";

 String[] text = { "One", "Two", "Three", "Four", "Five", "Six", "Seven",
 "Eight", "Nine", "Ten" };

 String[] text1 = { "udya", "aswini", "radha", "padma", "ram", "harish", "parasd",
         "adi", "harbinder", "pandu" };


 int textlength = 0;
 ArrayList<String> text_sort = new ArrayList<String>();
 ArrayList<String> text_sort1 = new ArrayList<String>();


 public void onCreate(Bundle savedInstanceState)
 {

 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 edittext = (EditText) findViewById(R.id.EditText01);
 listview = (ListView) findViewById(R.id.ListView01);
 listview.setAdapter(new MyCustomAdapter(text, text1));
 edittext.addTextChangedListener(new TextWatcher()
 {

 public void afterTextChanged(Editable s)
 {

 }

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

 }

 public void onTextChanged(CharSequence s, int start,
 int before, int count)
 {

 textlength = edittext.getText().length();
 text_sort.clear();
 text_sort1.clear();

 for (int i = 0; i < text.length; i++)
 {



 if (textlength <= text[i].length())
 {

 if (edittext.getText().toString().
 equalsIgnoreCase((String) text1[i].subSequence(0, textlength)))
 {
 text_sort.add(text[i]);
 text_sort1.add(text1[i]);

 }

     if ((edittext.getText().toString()).

     equalsIgnoreCase((String) text[i].subSequence(0,textlength)))
     {
     text_sort.add(text[i]);
     text_sort1.add(text1[i]);

     } 




 }
 }

 listview.setAdapter(new MyCustomAdapter
 (text_sort, text_sort1));

 }
 });
 }

 class MyCustomAdapter extends BaseAdapter
 {

 String[] data_text;
 String[] data_text1;


 MyCustomAdapter()
 {

 }

 MyCustomAdapter(String[] text, String[] text1)
 {
 data_text = text;
 data_text1 = text1;

 }
 MyCustomAdapter(ArrayList<String> text, ArrayList<String> text1)
 {
 data_text = new String[text.size()];
 data_text1 = new String[text1.size()];


 for(int i=0;i<text.size();i++)
 {
 data_text[i] = text.get(i);
 data_text1[i] = text1.get(i);

 }

 }

 public int getCount()
 {
 return data_text.length;
 }

 public String getItem(int position)
 {
 return null;
 }

 public long getItemId(int position)
 {
 return position;
 }

 public View getView(int position, View convertView, ViewGroup parent)
 {

 LayoutInflater inflater = getLayoutInflater();
 View row;

 row = inflater.inflate(R.layout.list_item, parent, false);

 TextView textview = (TextView) row.findViewById(R.id.TextView01);
 TextView textview1 = (TextView) row.findViewById(R.id.TextView02);


 textview.setText(data_text[position]);
 textview1.setText(data_text1[position]);

 textview.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        String sss=qustion.concat(edittext.getText().toString());
        Log.i("sss--------------", ""+sss);
    }
});

 return (row);

 }
 }
  } 

1 个答案:

答案 0 :(得分:-1)

试试这个例子

Listview names search in Android

在这里,您只需输入edittext

即可搜索列表项名称