1

嗨,我在这里做一个应用程序,我需要在 lisview 中显示 cloumns,并且我必须为该列表视图设置搜索(过滤)操作,我尝试使用以下几乎可以正常工作的代码,在过滤功能中,如果我在编辑文本中遇到问题输入第一个字母意味着它显示在列表视图下面它显示了与字母相关的名称,但它在两列中都显示了输入该字母相关名称的内容,但我想要输入字母意味着时间我只需要过滤基于第一列的名称,如果进入 ?任何我需要根据 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);

 }
 }
  } 
4

1 回答 1

-1

试试这个例子

Android中的Listview名称搜索

在这里,您只需输入编辑文本即可搜索列表项名称

于 2013-03-06T06:37:49.663 回答