android – 如何使用Cursor Adapter刷新listView

前端之家收集整理的这篇文章主要介绍了android – 如何使用Cursor Adapter刷新listView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用CursorAdapter创建了一个ListView.现在我正在尝试更新ListView并将值刷新到ListView.

但我无法弄明白.如何使用Loader或changeCursor()来刷新我的ListView

下面是我设置CursorAdapter的代码

// SucessFully在这里完成

sqlDataSore datastore = new sqlDataSore(PrintContent.this);

Cursor cursor                 = datastore.getJSONData();

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

list_View.setAdapter(cursorDemo);

我的按钮onClick我正在将值更新到数据库
// SucessFully完成

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

CursorDemo类

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时,旧游标未关闭.

原文链接:https://www.f2er.com/android/317546.html

猜你在找的Android相关文章