列表视图中的搜索功能

时间:2013-01-25 18:09:28

标签: android listview search

我在向ListView添加搜索功能时遇到了一些问题。我希望这样做,以便用户可以搜索ListView可点击的理想项目。我已经创建了一个没有可点击实现的搜索ListView。我尝试使用EditText和TextChangedListener添加搜索。这适用于普通的ListView但是当我点击listview的项目时没有任何事情发生..我知道我必须为listview中的每一行添加意图(例如ferrari)但是我如何实现意图?任何想法

public class Main extends Activity {

private ListView lv;


ArrayAdapter<String> adapter;


EditText inputSearch;


ArrayList<HashMap<String, String>> modelist;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    String modeller[] = {"Ferrari","Renault","Porche",};

    lv = (ListView) findViewById(R.id.list_view);
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.model_ismi, modeller);
    lv.setAdapter(adapter);

    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

            Main.this.adapter.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {


        }

        @Override
        public void afterTextChanged(Editable arg0) {

        }
    });
}

1 个答案:

答案 0 :(得分:1)

试试这个:

lv.setOnItemClickListener(new OnItemClickListener(){ 
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long _id){
                switch(position){
                    case 0: // "Ferrari"
                        Intent intent_ferrari=new Intent(Main .this,Ferrari.class);
                        startActivity(intent_ferrari);
                        break;
                    case 1: // "Renault"
                        break;
                    case 2: // "Porche"
                        break;
                    default:
                        break;
                }
            }
});