3

我已经在网上搜索了几天(实际上是晚上;),但目前我一直在为我的问题寻找解决方案。基本上我想要的是一个 SQLite 的列表视图,带有过滤器和重要的东西:保持列表行结果与 SQLite 行的一致。

我从一个基本的列表视图开始很容易,但是不可能保持 SQLite 行和过滤后的列表视图之间的一致性。然后我转向一个简单的光标,但我从来没有得到过滤的结果,只显示了完整的 SQLite 列表。

我想我在告诉 SimpleCursorAdapter 用过滤刷新的方向上遗漏了一些东西。我发现很多页面都有一些代码,但似乎无法在我自己的代码中实现它。它与: setFilterQueryProvider (我认为)我也读过这个 SimpleCursorAdapter 现在已经过时并转移到装载机?让我一步一步来,所以希望在一些帮助下,我可以让这个过滤器继续工作到加载器。

感谢您花时间阅读我的代码。因为现在已经快凌晨 5 点了,希望我的英语还可以 :)

public class MainActivity extends Activity {
    String TAG = "MainActivity";            // for logging purposes
    private ArrayList<String> data;         // location which receives all products to display in the list
    static int ProductCursorPosition;       // the id which will returned by pressing the listview
    private ListView listView_Products;             // define the listview variabele
    SimpleCursorAdapter adapter;
    EditText inputSearch;

public void  onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);    
    setContentView(R.layout.act_main);
    Log.v(TAG, "onCreate");
 //   Worker dbworker = new Worker(this.getApplicationContext());
//  Log.v(TAG,"Open database");
//  dbworker.open();
//  Log.v(TAG,"Get database entry's");           
//  data = dbworker.getProductListView(); 
//  dbworker.close();
}

@Override
protected void onResume() {
    super.onResume();
    Log.v(TAG, "onResume");
    displayListView();          // show me the products in the listview
    aantalbestellingen();       // update the total numbers of bread waiting in order
}

// display the listview with products
public void displayListView() {
    Log.v(TAG,"Select database");
    Worker dbworker = new Worker(this.getApplicationContext());
    Log.v(TAG,"Open database");
    dbworker.open();
    String[] columns = new String[]{ Worker.KEY_ROWID, Worker.KEY_PRODUCT};
    String[] from = new String[]{Worker.KEY_PRODUCT};

    int [] to = {R.id.text1};
    final Cursor cursor = Worker.ourDatabase.query(true, // isdistinct
            Worker.DB_TABLE_PROD, // table name
            columns,// select clause
            null, // where cluase
            null, // where clause parameters
            null, // group by
            null, // having
            null, // nogwat 
            null);// limit
    startManagingCursor(cursor);


    adapter = new SimpleCursorAdapter(this, R.layout.lv_customlayout01, cursor, from, to);

    // find the view for the listview and connect the listView_Products to it
    listView_Products = (ListView) findViewById(R.id.lvProducts);
    // Shows the adapter content in the listview
    listView_Products.setAdapter(adapter);  

    dbworker.close();


    /**
     * Enabling Search Filter
     * */
    inputSearch = (EditText) findViewById(R.id.etSearchbar); 
    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            Log.v(TAG,"Filter: onTextChanged");
            Log.v(TAG,"Filterstring: " + cs);
            Log.v(TAG,"adapter: " + adapter);
            adapter.getFilter().filter(cs); 
//          adapter.getFilter().filter(cs.toString());
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            Log.v(TAG,"Filter: beforeTextChanged");
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            Log.v(TAG,"Filter: afterTextChanged");
//          adapter.notifyDataSetChanged();

        }
    });


    listView_Products.setOnItemClickListener( new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        Log.v(TAG, "Pressed the listview\n" + 
        "View arg2 = " + arg2 + "\n" +
        "int arg3 = " + arg3 + "\n");   
    }
    });

}

}

好的,我稍微更改了代码: 现在我有一个带有过滤结果的列表视图。

// display the listview with products
public void displayListView() {
    Log.v(TAG,"DB select");
    Worker dbworker = new Worker(this.getApplicationContext());
    Log.v(TAG,"DB open");
    dbworker.open();

    // SQLite get cursor from Worker 
    final Cursor ItemCursor = Worker.cursorsetup01();
    // Start managing the cursor
    startManagingCursor(ItemCursor);

    // Columns to be bound to the adapter
        String[] FROMcolumns = new String[]{Worker.KEY_PRODUCT, Worker.KEY_INFO };
        // THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
        int[] ToXMLView = new int[] {R.id.tvProduct, R.id.tvProductInfo};
        // CREATE THE ADAPTER USING THE CURSOR POINTING TO THE DESIRED DATA AS WELL AS THE LAYOUT INFORMATION

        Log.v(TAG,"CREATE THE ADAPTER");    
        adapter = new SimpleCursorAdapter(this, R.layout.lv_customlayout01, ItemCursor, FROMcolumns, ToXMLView);
        // find the view for the listview and connect the listView_Products to it

        listView_Products = (ListView) findViewById(R.id.lvProducts);
        // SET THIS ADAPTER AS YOUR LISTACTIVITY'S ADAPTER
        listView_Products.setAdapter(adapter);      

    adapter.setFilterQueryProvider(new FilterQueryProvider() {

        public Cursor runQuery(final CharSequence substring) {
            String[] dbquerycolumns = new String[]{Worker.KEY_PRODUCT, Worker.KEY_INFO, Worker.KEY_ROWID };    
            Worker dbworker = new Worker(getApplicationContext());
            Log.v(TAG,"Open database");
            dbworker.open();
            return Worker.ourDatabase.query(
                            Worker.DB_TABLE_PROD, // TABLE
                            dbquerycolumns,// COLUMNS
                            "product LIKE '%" + substring.toString() + "%'" , // SELECTION
                            null, // SELECTION ARGS
                            null, // GROUP BY
                            null, // HAVING
                            "_id LIMIT 100");// ORDER BY (limit the results to 100)
                    }

        });


    // Connect the textedit searchbar to a change listener and action upon changes
    mySearchText = (EditText)findViewById (R.id.etSearchbar);
    mySearchText.addTextChangedListener (new TextWatcher() {

        @Override
        public void afterTextChanged (Editable s) {
            Log.v(TAG,"Textchanged: AfterTextChanged: " + s);
            adapter.getFilter().filter(s.toString());
            startManagingCursor(ItemCursor);
        }
4

2 回答 2

1

我创建了自己的过滤文本方法:

/** For Filter City **/
@SuppressWarnings("deprecation")
private SimpleCursorAdapter getAdapter(String city)
{
    dbHelper.open();
    if(city.equals(null))
    {
        cityCursor = dbHelper.getComapnyArea(city);
    }
    else
    {
        cityCursor = dbHelper.getComapnyArea(city);
    }

    if (cityCursor != null) {
        String columns[] = new String[] { DatabaseHelper.CA_NAME };

        int to[] = new int[] { R.id.listTextView };

        cityAdapter = new SimpleCursorAdapter(context, R.layout.list_textview, cityCursor, columns, to);
        listView.setAdapter(cityAdapter);
    }
    dbHelper.close();

    return cityAdapter;
}

这将在 SearchView 中使用,例如:

/** For Filter City **/
@SuppressWarnings("deprecation")
private SimpleCursorAdapter getAdapter(String city)
{
    dbHelper.open();
    if(city == null)
    {
        cityCursor = dbHelper.getComapnyArea();
    }
    else
    {
        cityCursor = dbHelper.getComapnyArea(city);
    }

    if (cityCursor != null) {
        String columns[] = new String[] { DatabaseHelper.CA_NAME };

        int to[] = new int[] { R.id.listTextView };

        cityAdapter = new SimpleCursorAdapter(context, R.layout.list_textview, cityCursor, columns, to);
    }

    return cityAdapter;
}
于 2013-06-04T06:27:39.480 回答
0

对你有帮助的几件事

  1. 加载程序(或支持人员)解决一致性问题
  2. 自定义适配器的完整实现 - 而不是预打包的 - 用于管理和跟上 EditText 发生的更新

现在,您必须对现有代码进行一些修改。

  1. 维护从数据库加载的数据的旧实例 - 您总是需要使用它来核对 -Collection1
  2. 维护一个类似的集合,通过自定义适配器向 UI 显示 -Collection2
  3. 当 EditText 中的文本发生更改时 - (A) 删除条目Collection2并使用新过滤器重新创建它 (b) 使用最新的集合创建适配器的新实例2 (c) 将此新实例设置为 listView (d) 回收用于优化的旧实例

这样一来,您的更新对用户来说将是即时的,而您可以完全控制解决当前问题以及可能出现的任何问题。

于 2012-11-10T04:52:27.730 回答