0

我想在 listview 中创建搜索功能,我有点怀疑使用 arraylist 是否可以执行搜索功能。这里我附上了代码..

Totalarraylist 是数组列表,我从该数组列表将数据传递给问题适配器类代码,

    searchbox.addTextChangedListener(new TextWatcher() {

              @Override
              public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                  // When user changed the Text

              If so then what to add here for search functionality...

              }

              @Override
              public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                      int arg3) {
                  // TODO Auto-generated method stub

              }


            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
          });


          adapterfirst = new Questionadapter(MainActivity.this,Totalarraylist);
      listviewfirst = (ListView) findViewById(R.id.listquestion);
      listviewfirst.setAdapter(adapterfirst);

}

public class  Questionadapter extends BaseAdapter  {

        private ArrayList<HashMap<String, Object>> data;    
          public  LayoutInflater inflater;
            Context context;



        public Questionadapter(Context context, ArrayList<HashMap<String,Object>> Totalarraylists) {
            // TODO Auto-generated constructor stub

            data=Totalarraylists;
              inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                System.out.println("data="+data);
        }

        public int getCount() {
            return data.size();
        }

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

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


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

            //
}
}

任何人都可以帮我做到这一点..

4

2 回答 2

0

试试这个,将您的数组列表与您的字符串进行比较,您将得到答案

  public class MainActivity extends Activity {

ArrayList<String> list = new ArrayList<String>();

private EditText searchEdt;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // making it full screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_ugsimply_beta);

    list.add("Android");
    list.add("Android1");
    list.add("blackberry");
    list.add("samsung");
    list.add("LG");
    searchEdt = (EditText) findViewById(R.id.searchEdt);

    searchEdt.addTextChangedListener(new TextWatcher() {

        private int textlength;
        private ArrayList<String> arrayList_sort = new ArrayList<String>();

        public void afterTextChanged(Editable s) {
            // Abstract Method of TextWatcher Interface.
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // Abstract Method of TextWatcher Interface.
        }

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            textlength = searchEdt.getText().length();
            if (arrayList_sort != null)
                arrayList_sort.clear();

            for (int i = 0; i < list.size(); i++) {

                if (textlength <= list.get(i).toString().length()) {

                    if (searchEdt
                            .getText()
                            .toString()
                            .equalsIgnoreCase(
                                    (String) list.get(i).toString()
                                            .subSequence(0, textlength))) {
                        arrayList_sort.add(list.get(i));
                        Log.d("TAG", "log" + arrayList_sort.size());
                    }
                }
            }

        }
    });

}

}

于 2013-02-25T11:02:03.940 回答
0

将过滤器添加到您的ListAdapter, 并将onTextChanged过滤器应用于listadapter

于 2013-02-25T10:50:04.147 回答