从自定义数据库筛选listview

时间:2017-01-24 10:26:34

标签: android

我在资产文件夹中有一个自定义数据库,并在列表视图中使用它。我的问题是我如何实现搜索/过滤数据库以显示搜索项目以及如何在这样的列表上实现clicklistner 代码:

数据库访问类:

public class DatabaseAccess {
    private SQLiteOpenHelper openHelper;
    private SQLiteDatabase database;
    private static DatabaseAccess instance;

    /**
     * Private constructor to aboid object creation from outside classes.
     *
     * @param context
     */
    private DatabaseAccess(Context context) {
        this.openHelper = new DatabaseOpenHelper(context);
    }

    /**
     * Return a singleton instance of DatabaseAccess.
     *
     * @param context the Context
     * @return the instance of DabaseAccess
     */
    public static DatabaseAccess getInstance(Context context) {
        if (instance == null) {
            instance = new DatabaseAccess(context);
        }
        return instance;
    }

    /**
     * Open the database connection.
     */
    public void open() {
        this.database = openHelper.getWritableDatabase();
    }

    /**
     * Close the database connection.
     */
    public void close() {
        if (database != null) {
            this.database.close();
        }
    }

    /**
     * Read all quotes from the database.
     *
     * @return a List of quotes
     */
    public List<String> genesis() {
        List<String> list = new ArrayList<>();
        Cursor cursor = database.rawQuery("SELECT n FROM key_english", null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            list.add(cursor.getString(0));
            cursor.moveToNext();
        }
        cursor.close();
        return list;
    }
}

主要活动:

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    EditText editText;
    String[] items;
    ArrayList<String> listItems;
    ArrayAdapter<String>adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText=(EditText)findViewById(R.id.txtsearch);
        genesis();


        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                MainActivity.this.adapter.getFilter().filter(s );
              /*  if(s.toString().equals("")){
                    //resetlist
                    genesis();

                }
                else {
                    //performsearch
                    searchItem(s.toString());
                }*/
            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });

    }

    public void searchItem(String textToSearch)
    {
        for(String item:items){
            if (!item.contains(textToSearch)){
                listItems.remove(item);

            }
        }
        adapter.notifyDataSetChanged();
    }

    public void genesis()
    {
        this.listView = (ListView) findViewById(R.id.listView);
        DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
        databaseAccess.open();
        List<String> key_english = databaseAccess.genesis();
        databaseAccess.close();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, key_english);
        this.listView.setAdapter(adapter);

    }

活动主Xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/txtsearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="search"/>

    <ListView
        android:id="@+id/listView"
        android:layout_width="320dp"
        android:layout_height="348dp"
        android:layout_gravity="center"
        android:layout_below="@+id/txtsearch"/>
</FrameLayout>

非常感谢任何帮助。谢谢你

0 个答案:

没有答案