寻找listview上搜索功能的最佳匹配解决方案

时间:2014-02-19 14:13:20

标签: android

我想在自定义列表视图中添加搜索功能。我实际上在互联网上搜索了几个小时,但我无法按照所有那些复杂的教程或演示,所以我决定在这里发布我的问题。希望有人能帮助我解决这个问题。这是我的代码

    ListView lv;
    EditText inputSearch;
    CustomListView testingAdapter;
    //at oncreate method
    lv = (ListView) findViewById(R.id.lvEditSavingList);
    inputSearch = (EditText) findViewById(R.id.inputSearch);
    loadListViewData();

    private void loadListViewData() {
    // TODO Auto-generated method stub
    UnderControlDb db = new UnderControlDb(getApplicationContext());
    String[] info = db.MySavingShowData();
    int counter = info.length;
    Integer[] imageID = new Integer[counter];
    for(int i=0;i<counter;i++){
        imageID[i] = R.drawable.edit_notes_list;
    }
    testingAdapter = new CustomListView(MySaving.this, info, imageID);
    lv.setAdapter(testingAdapter);
}

然后这是我的自定义类

public class CustomListView extends ArrayAdapter<String>{
private final Activity context;
private final String[] data;
private final Integer[] imageId;
public CustomListView(Activity context,String[] data, Integer[] imageId) {
    super(context, R.layout.my_saving_list, data);
    this.context = context;
    this.data = data;
    this.imageId = imageId;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView= inflater.inflate(R.layout.my_saving_list, null, true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.tvSavingListView);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.imgS);
    txtTitle.setText(data[position]);
    imageView.setImageResource(imageId[position]);
    return rowView;
}

}

和我的xml代码

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
    <TextView
        android:id="@+id/tvSavingListView"
        android:layout_width="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:textSize="16sp"
        android:layout_height="30dp" />

    <ImageView
        android:id="@+id/imgS"
        android:layout_width="25dp"
        android:layout_height="25dp"/>
</TableRow>
</TableLayout>

1 个答案:

答案 0 :(得分:0)

&#39; ave写了一个小样本,以向您展示如何从listView中搜索数据。 找到下面的代码,并且毫不犹豫地向我提出任何问题以获得更多说明。

public class TestSearch extends Activity {
    ListView lv;
    EditText inputSearch;
    CustomListView testingAdapter;

    public class listContent{
        public String info;
        public int imageInfo;

        public listContent(String info, int imageInfo) {
            this.info = info;
            this.imageInfo = imageInfo;
        }
        public String getInfo() {
            return info;
        }
        public void setInfo(String info) {
            this.info = info;
        }
        public int getImageInfo() {
            return imageInfo;
        }
        public void setImageInfo(int imageInfo) {
            this.imageInfo = imageInfo;
        }

    }

    ArrayList<listContent> itemsContent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_search);
        itemsContent = new ArrayList<listContent>();
        CustomListView testingAdapter;
        lv = (ListView) findViewById(R.id.lvEditSavingList);
        inputSearch = (EditText) findViewById(R.id.inputSearch);
        inputSearch.addTextChangedListener(textWatcher);
        loadListViewData();
    }
    private TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            String text = inputSearch.getText().toString().toLowerCase(Locale.getDefault());
            testingAdapter.filter(text);
        }
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) { 
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
        }
    };
    private void loadListViewData() {
        // TODO Auto-generated method stub
        itemsContent.add(new listContent("text", R.drawable.ic_launcher));
        itemsContent.add(new listContent("hello", R.drawable.ic_launcher));
        itemsContent.add(new listContent("dear", R.drawable.ic_launcher));
        itemsContent.add(new listContent("test", R.drawable.ic_launcher));
        itemsContent.add(new listContent("lol", R.drawable.ic_launcher));

        testingAdapter = new CustomListView(TestSearch.this, itemsContent);
        lv.setAdapter(testingAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.test_search, menu);
        return true;
    }
    public class CustomListView extends BaseAdapter{
        private final Context mContext;
        private ArrayList<listContent> items;
        ArrayList<listContent> filtereditems;

        public CustomListView(Context context, ArrayList<listContent> items) {
            this.mContext= context;
            this.items = items;
            this.filtereditems = new ArrayList<listContent>();
            this.filtereditems.addAll(items);
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            View rowView= inflater.inflate(R.layout.my_saving_list, null, true);
            TextView txtTitle = (TextView) rowView.findViewById(R.id.tvSavingListView);
            ImageView imageView = (ImageView) rowView.findViewById(R.id.imgS);
            txtTitle.setText(items.get(position).getInfo());
            imageView.setImageResource(items.get(position).getImageInfo());
            return rowView;
        }

        public void filter(String charText) {
            charText = charText.toLowerCase(Locale.getDefault());
            items.clear();
            if (charText.length() == 0) {
                items.addAll(filtereditems);
            } 
            else
            {
                for (listContent wp : filtereditems) 
                {
                    if(wp.getInfo().startsWith(charText))
                    {
                        items.add(wp);
                    }
                }
            }
            notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            return items.size();
        }
        @Override
        public Object getItem(int arg0) {
            return items.get(arg0);
        }
        @Override
        public long getItemId(int arg0) {
            return arg0;
        }
    }
    }