1

I basically want to add a simple search ability to my list view. Where there is a search bar at the top and the app sorts the searches for me. I would be greatful if anyone could help me. This is a perfect example of what I want: Example. Thanks!
PetActivity.java

public class PetActivity extends Activity {
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pet_layout);
        ListView listView1 = (ListView) findViewById(R.id.listView1);

        Pet[] items = {
                new Pet("1"),
                new Pet(""),
                new Pet(""),
                new Pet(""),
                new Pet(""),
        };

        ArrayAdapter<Pet> adapter = new ArrayAdapter<Pet>(this,
                android.R.layout.simple_list_item_1, items);
        listView1.setAdapter(adapter);


        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
                if (position == 1)
                {
                    Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_LONG).show();
                }
            }
        });

    }
}

Pet.java

package com.thebuildcast.cubeworldtoolbox;

public class Pet {
    private String name;


    public Pet(){
        super();
    }

    public Pet(String name){
        super();
        this.name = name;
    }

    @Override
    public String toString(){
        return this.name;
    }
}
4

1 回答 1

0

这是一个非常模糊的问题,但是文档中的这两个链接可能会有所帮助

搜索

搜索视图

此外,我使用 aTextWatcher根据CharSeq输入的电流过滤数据库或列表中的项目。

文本观察器

当范围如此广泛时,很难给出一个代码示例,但这些应该能够让你开始弄清楚你想怎么做。

于 2013-08-15T01:23:26.570 回答