搜索从android中的数据库加载的列表视图

时间:2013-06-27 08:15:04

标签: java android eclipse

我的应用程序中有数据库,它在listview中完全加载但是当在listview中进行搜索时它不起作用且应用程序没有崩溃下面是我的代码,其中我从数据库中检索数据并在listview中进行搜索。

    -------------DatabaseClass----
public Cursor getCursor() {
    // TODO Auto-generated method stub
    // SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    // queryBuilder.setTables(TABLE_NAME);
    // openDataBase();

    Cursor c = null;
    try {
        String[] asColumnsToReturn = new String[] { "lang_from","_id" };
        c = myDataBase.query("words2", asColumnsToReturn, null, null, null,
                null, null);

    } catch (Exception e) {
        System.out.print("" + e);
    }
    return c;
}

     -----------MainActivity----------------
public class MainActivity extends Activity {

DatabaseClass myDbHelper;
private Cursor ourCursor = null;
private IngredientAdapter adapter = null;

ListView myListView;
EditText etSearch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    etSearch = (EditText) findViewById(R.id.etsearch);
    myListView = (ListView) findViewById(R.id.listView1);
    myListView.setTextFilterEnabled(true);
    // = new DatabaseClass();
    myDbHelper = new DatabaseClass(this);

    try {

        myDbHelper.createDataBase();

    } catch (IOException ioe) {

        throw new Error("Unable to create database");

    }

    try {

        myDbHelper.openDataBase();
        ourCursor = myDbHelper.getCursor();
        startManagingCursor(ourCursor);
        try {
            adapter = new IngredientAdapter(this, ourCursor);
        } catch (Exception e) {
            System.out.print("" + e);
        }
        myListView.setAdapter(adapter);
    } catch (SQLException sqle) {

        throw sqle;

    }
    etSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2,
                int arg3) {
            // When user changed the Text
            // MainActivity.this.adapter.getFilter().filter(cs);
            Log.d("TAGGGGGGGG", cs.toString());
            adapter.getFilter().filter(cs);

            // adapter.notifyDataSetChanged();
//MainActivity.this.adapter.getFilter().filter(cs.toString());
        }

        @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
            // adapter.getFilter().filter(s.toString());

        }
    });
}

class IngredientAdapter extends CursorAdapter {
    @SuppressWarnings("deprecation")
    public IngredientAdapter(Context context, Cursor c) {
        super(context, c);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void bindView(View row, Context ctxt, Cursor c) {
        // TODO Auto-generated method stub
        IngredientHolder holder = (IngredientHolder) row.getTag();
        holder.populateFrom(c, myDbHelper);
        Log.d("TAAAAAAAAAAAAAAAGGGGG", "GOOD");
    }

    @Override
    public View newView(Context ctxt, Cursor c, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.row, parent, false);
        IngredientHolder holder = new IngredientHolder(row);
        row.setTag(holder);
        Log.d("TAAAAAAAAAAAAAAAGGGGG", "GOOD");
        return (row);
    }
}

class IngredientHolder {
    private TextView name = null;

    IngredientHolder(View row) {
        name = (TextView) row.findViewById(R.id.ingredientText);

    }

    void populateFrom(Cursor c, DatabaseClass r) {
        name.setText(r.getName(c));
    }
}
 }

请帮助和抱歉弱英语

2 个答案:

答案 0 :(得分:2)

我在我的应用程序中实现了这种功能。

简要说明。 实现自己的扩展Filter的类,就像下一个类一样:

private class PlanetFilter extends Filter {
@Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        // We implement here the filter logic
        if (constraint == null || constraint.length() == 0) {
            // No filter implemented we return all the list
            results.values = planetList;
            results.count = planetList.size();
        }
        else {
            // We perform filtering operation
            List<Planet> nPlanetList = new ArrayList<Planet>();

            for (Planet p : planetList) {
                if (p.getName().toUpperCase().startsWith(constraint.toString().toUpperCase()))
                    nPlanetList.add(p);
            }

            results.values = nPlanetList;
            results.count = nPlanetList.size();

        }
        return results;
    }
}

在基础适配器中实现Filterable接口,它必须实现getFilter()方法:

@Override
public Filter getFilter() {
    if (planetFilter == null)
        planetFilter = new PlanetFilter();

    return planetFilter;
}

要将所有内容绑定在一起,请在textWatcher上使用edittext,然后输入文字。

editTxt.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        System.out.println("Text ["+s+"]");
        aAdpt.getFilter().filter(s.toString());                           
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {
    }
});

该代码来自下一个tutorial

答案 1 :(得分:0)

您永远不会为适配器指定过滤器 - 您需要声明此过滤器并根据要过滤的内容告诉它。