EditText用于过滤/搜索listView

时间:2016-01-23 15:56:16

标签: android sqlite listview search filter

我是编程新手,在创建应用程序的教程的帮助下,您可以创建隔离专区并将其存储在sqlite数据库中。借助listview,将显示货架。现在我在ListView的顶部设置了EditText,并且使用stackoverflow我试图实现“动态过滤”。我认为这可以使用“addTextChangedListener”,但它根本不起作用。我尝试了近10种不同的方法和4种不同的其他教程,花了一天和几乎晚上尝试但没有任何效果。希望你们能帮助我。

谢谢

遵循我的DataListActivity代码:

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.kasutwentyseven.gui4selfshelf.R;
import com.kasutwentyseven.gui4selfshelf.Scans.ScanActivity;

public class DataListActivity extends Activity {
public ListView listView;
public SQLiteDatabase sqLiteDatabase;
public ShelfDBHelper shelfDBHelper;
public Cursor cursor;
public EditText inputSearch;
public ListDataAdapter listDataAdapter; //DataSource - Adapter

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.data_list_layout);

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "Lobster.ttf");
    TextView myTextView = (TextView) findViewById(R.id.text_yourshelfs);
    myTextView.setTypeface(myTypeface);

    listView = (ListView) findViewById(R.id.list_view);
    listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout); //ArrayAdapter

    inputSearch = (EditText)findViewById(R.id.editText_search_shelf);


    listView.setAdapter(listDataAdapter);
    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
            listDataAdapter.getFilter().filter(s);
        }

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

        @Override
        public void afterTextChanged(Editable arg0) {

        }
    });

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent n = new Intent(getApplicationContext(), ScanActivity.class);
            n.putExtra("postition", position);
            startActivity(n);

shelfDBHelper = new ShelfDBHelper(getApplicationContext());
    sqLiteDatabase = shelfDBHelper.getReadableDatabase();
    cursor = shelfDBHelper.getInformations(sqLiteDatabase);
    if (cursor.moveToFirst()) {
        do {
            String name;

            name = cursor.getString(0);

            DataProvider dataProvider = new DataProvider(name);
            listDataAdapter.add(dataProvider);
        } while (cursor.moveToNext());
    }


}

我的适配器代码:

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.kasutwentyseven.gui4selfshelf.R;

import java.sql.SQLDataException;
import java.util.ArrayList;
import java.util.List;

public class ListDataAdapter extends ArrayAdapter{
private List list= new ArrayList();
private static final String TAG = "Shelf ADAPTER";
private ShelfDBHelper shelfDBHelper;
private SQLiteDatabase sqLiteDatabase;

public ListDataAdapter(Context context, int resource) {
    super(context, resource);
}
static class LayoutHandler
{
    TextView NAME;
}
@Override
public void add(Object object){
    super.add(object);
    list.add(object);
}
@Override
public int getCount(){
    return list.size();
}

@Override
public Object getItem(int position){
    return list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    LayoutHandler layoutHandler;
    if(row == null){
        LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.row_layout,parent,false);
        layoutHandler = new LayoutHandler();
        layoutHandler.NAME = (TextView)row.findViewById(R.id.text_shelf_name);
        row.setTag(layoutHandler);
    }else{
        layoutHandler = (LayoutHandler) row.getTag();

    }

    DataProvider dataProvider = (DataProvider)this.getItem(position);
    layoutHandler.NAME.setText(dataProvider.getName());

    return row;
}

2 个答案:

答案 0 :(得分:0)

看看这个图书馆 https://github.com/edsilfer/custom-searchable ,它可以满足您的需求,另外它可以为您的应用带来漂亮的动画效果。

答案 1 :(得分:0)

在使用自定义适配器时,需要在其中实现过滤器。你采取的方法是Android默认适配器。

自定义过滤器的代码是

{
    "name": "someone",
    "degree": "PhD"
}

代码来自https://gist.github.com/fjfish/3024308。你也可以在这里看到完整的参考资料。