- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我已经在网上搜索了几天(实际上是几个晚上 ;),但目前我一直在为我的问题寻找解决方案。基本上我想要的是 SQLite 的 ListView ,带有过滤器和重要的事情:保持列表行结果与 SQLite 行的一致。
我从一个基本的 ListView 开始很容易,但是不可能保持 SQLite 行和过滤后的 ListView 之间的一致性。然后我转向一个简单的游标,但我从未得到过滤结果,只显示了完整的 SQLite 列表。
我想我在告诉 SimpleCursorAdapter 用过滤刷新的方向上遗漏了一些东西。我发现很多页面都有一些代码,但似乎无法在我自己的代码中实现它。它与以下内容有关:setFilterQueryProvider(我认为)我还读过这个 SimpleCursorAdapter is now obsolete and moves to loaders?让我一步一个脚印,希望在一些帮助下我可以让这个过滤工作继续加载程序。
感谢您花时间阅读我的代码。现在已经快凌晨 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");
}
});
}
}
好的,我稍微修改了一下代码:现在我有一个带有过滤结果的 ListView 。
// 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);
}
最佳答案
我已经为过滤文本创建了自己的方法:
/** 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;
}
关于SimpleCursorAdapter 上的安卓过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13319065/
我已经在网上搜索了几天(实际上是几个晚上 ;),但目前我一直在为我的问题寻找解决方案。基本上我想要的是 SQLite 的 ListView ,带有过滤器和重要的事情:保持列表行结果与 SQLite 行
当我在 Droidio 中输入“SimpleCursorAdapter”时,对于应该添加哪个 SimpleCursorAdapter 类是矛盾的: 我也不知道 - 我应该使用哪一个作为在 ListVi
下面是让我悲伤的代码示例。如果我将 simpleCursorAdapter 放在 textchangedlistener 之外,但不在我不断收到消息中,则 simpleCursorAdapter 可以
我的每一行都有一个 ListView,我有一个 LinearLayout,其中包含一些对象(主要是一些 TextView)。 这个ListView我从光标动态填充它。在这个游标中,我有一个值 true
我正在尝试构建 android 应用程序,它似乎将 Force Close 保持在同一位置 (setListAdapter/SimpleCursorAdapter) 现在不管我做什么。这是 DBAda
我有一个关于 SimpleCursorAdapter 的恼人问题。我的程序有 ListView 和 ListActivity。每行都有自己的布局:
此代码通过显示数据库中的所有数据正常工作,但我在过滤时遇到问题。我尝试了很多代码但没有任何效果,有人可以帮助我吗?谢谢 package com.example.dictionary; imp
我想实现一个自定义的 SimpleCursorAdapter,它只在偶数行上显示背景颜色(在我的例子中是蓝色)。我的实现如下: public class ColoredCursorAdapter ex
我正在使用自定义的 SimpleCursorAdapter。目前,如果在数据库/游标中找不到任何条目,我的列表将保持为空。现在,如果光标/数据库中没有条目,我想在列表中显示一条消息。我该如何处理这个事
我一直在开发 Android 应用程序,我有一个问题 - ListView 使用 SimpleCursorAdapter(此游标从 SQLite 数据库获取信息),但如果我编辑我的数据库,ListVi
我试图在我的类中设置一个“SimpleCursorAdapter”对象变量,以便所有方法都可以访问它,所有这些都是非常标准的东西。目前,我在“OnCreate”方法中遇到错误,显示以下错误: 错误:
我想将一个字符串附加到我从数据库中提取到游标中的 ID。我有一个包含 2 个项目的 ListView,我希望文本写入 Id+"some text"。我使用 SimpleCursorAdapter 和
我有一个用 SimpleCursorAdapter 填充的 ListView。对于我列表中的每一项,我按以下顺序排列:TextView > RatingBar > TextView > ImageVi
我正在开发我的第一个 Android 应用程序,但不知道如何让我的 SimpleCursorAdpater 填充 View 。我传入的游标在其中产生了结果,因此问题一定出在实例化适配器或将其绑定(bi
我正在尝试使用自定义适配器访问列表 Activity 。我已经在不使用任何自定义适配器的情况下直接进行了尝试,它运行良好,但因为我想在 ListView 中添加更多功能,所以我想实现一个自定义适配器。
我正在尝试使用带有 ViewBinder 的 SimpleCursorAdapter 从数据库中获取图像并将其放入我的 ListView 项目 View .这是我的代码: private void s
我正在查看 Android 开发者网站上的记事本教程,发现 SimpleCursorAdaptor 已弃用。 新的构造函数 public SimpleCursorAdapter (Context co
我正在使用已弃用的 SimpleCursorAdapter 将数据从 Cursor 显示到 ListView。我添加了额外的参数 0,它删除了弃用的警告,但我想使用更好的方式来显示数据。我读过一些关于
所以我使用 SimpleCursorAdapter 将 SQLite 中的数据适配到 ListView 中。让我们称这个数据库为 testData。我在 testData 中的一列使用 0 或 1 记
我正在使用一个数据库,从中获取一个游标,然后使用一个 simplecursoradapter 来填充一个 ListView 。我似乎无法弄清楚为什么应用程序在创建新的 simplecursoradap
我是一名优秀的程序员,十分优秀!