gpt4 book ai didi

android - 如何使用 Cursor Adapter 刷新 listView

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:13:33 27 4
gpt4 key购买 nike

我使用 CursorAdapter 创建了一个 ListView 。现在我正在尝试更新 ListView 并将值刷新到 ListView 。

但我想不通。如何使用 LoaderchangeCursor() 刷新我的 ListView

下面是我设置CursorAdapter的代码:

//这里成功完成

SQLDataSore datastore = new SQLDataSore(PrintContent.this);

Cursor cursor = datastore.getJSONData();

final CursorDemo cursorDemo = new CursorDemo(PrintContent.this, cursor);

list_View.setAdapter(cursorDemo);

我的 Button onClick 我正在将值更新到数据库中//成功完成

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 方法:

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;
}

光标演示类

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;
}

}

感谢任何帮助... });

最佳答案

如果 CursorDemo 扩展了 CursorAdapter,那么你必须使用 adapter.swapCursor(cursor_update);

这应该将旧游标换成新游标并重新加载数据。使用 swapCursor,旧游标不会关闭。

关于android - 如何使用 Cursor Adapter 刷新 listView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20676701/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com