如何使用 Cursor Adapter 刷新 listView [英] how to refresh the listView using the Cursor Adapter
问题描述
我使用 CursorAdapter
创建了一个 ListView
.现在我正在尝试更新 ListView
并将值刷新到 ListView .
I have created a ListView
using CursorAdapter
. Now I am Trying to update the ListView
and Refresh the value to the ListView .
但我无法弄清楚.如何使用 Loader
或 changeCursor()
来刷新我的 ListView
But I am not able to figure out . How to work with Loader
or changeCursor()
to refresh my ListView
下面是我设置CursorAdapter
的代码:
//到这里就成功了
SQLDataSore datastore = new SQLDataSore(PrintContent.this);
Cursor cursor = datastore.getJSONData();
final CursorDemo cursorDemo = new CursorDemo(PrintContent.this, cursor);
list_View.setAdapter(cursorDemo);
My Button
onClick
我正在将值更新到数据库中//成功完成
My Button
onClick
I am updating the Value into the Database
//SucessFully Done
btn_check.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View view ) {
String editTextValue = edit_check.getText().toString();
if (editTextValue!=null) {
SQLDataSore sqlDataSore = new SQLDataSore(PrintContent.this);
Cursor cursor_update = sqlDataSore.updateData(editTextValue);
//Here How Should I update my ListView ...?
}
}
我的 UpdateData 方法:
My UpdateData Method:
public Cursor updateData(String editContent){
SQLiteDatabase updateContent = getReadableDatabase();
Cursor cursor_update = updateContent.rawQuery( "update " +TABLE_NAME + " set content = '"+ editContent
+"' "+" where _id = 357", null);
return cursor_update;
}
CursorDemo 类
CursorDemo Class
public class CursorDemo extends CursorAdapter{
public CursorDemo(Context context, Cursor c) {
super(context, c , false);
// TODO Auto-generated constructor stub
}
@Override
public void changeCursor(Cursor cursor) {
// TODO Auto-generated method stub
super.changeCursor(cursor);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView txt_content = (TextView) view.findViewById(R.id.txt_content);
TextView txt_likes_count = (TextView) view.findViewById(R.id.txt_likescount);
TextView txt_name = (TextView) view.findViewById(R.id.txt_name);
TextView txt_display_name = (TextView) view.findViewById(R.id.txt_display_name);
txt_content.setText(cursor.getString(cursor.getColumnIndex("content")));
}
@Override
public View newView(Context context , Cursor cursor, ViewGroup viewGroup) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.message_row_view, viewGroup ,false);
return view;
}
}
感谢任何帮助...});
Any Help is Appreciated... });
推荐答案
如果 CursorDemo 扩展了 CursorAdapter,则必须使用 adapter.swapCursor(cursor_update);
If CursorDemo extends CursorAdapter, then you have to use adapter.swapCursor(cursor_update);
那应该将旧光标替换为新光标并重新加载数据.使用 swapCursor
,旧游标不会关闭.
That should swap the old cursor out for the new one and reload the data. With swapCursor
, the old cursor is not closed.
这篇关于如何使用 Cursor Adapter 刷新 listView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!