0

リストビューで検索機能を作成したいのですが、arraylistを使用して検索機能を実行できるかどうか少し疑問があります.ここにコードを添付しました..

Totalarraylist は arraylist であり、その arraylist からデータを Question アダプター クラス コードに渡しています。

    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

にフィルターを追加し、フィルターをListAdapteronTextChanged適用しますlistadapter

于 2013-02-25T10:50:04.147 に答える
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 に答える