0

我有一个自定义 cursorAdapter 肯定缺少一些东西,因为当我在数据库中添加一个项目时,它不会显示在 listView 上,但是如果我重新启动应用程序,它会显示添加的行。

这是我的活动和我的 customAdapter :

package com.android.adapter;

import com.android.activity.R;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class NotesCursorAdapter extends CursorAdapter{

    private Context context;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_ADD_NOTE_BOTTOM = 1;
    private static final int TYPE_ADD_NOTE_TOP = 2;
    private static final int TYPE_MAX_COUNT = TYPE_ADD_NOTE_TOP + 1;

    public NotesCursorAdapter (Context context, Cursor cursor, boolean flag){
        super(context, cursor, flag);
        this.context = context;
    }



    @Override
    public void bindView(View v, Context con, Cursor cursor) {
        TextView content = (TextView)v.findViewById(R.id.note);
        content.setText(cursor.getString(cursor.getColumnIndex("content_note")));
    }

    @Override
    public View newView(Context con, Cursor cursor, ViewGroup vp) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.row_note, vp, false);
        bindView(v, context, cursor);
        return v;

    }

}


package com.android.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.android.adapter.NotesCursorAdapter;
import com.android.database.NoteDataSource;

public class EverydayNotesAndroid3Activity extends Activity {
    /** Called when the activity is first created. */

    private Cursor cursorNotes;
    private NoteDataSource dataNoteConnector;
    private NotesCursorAdapter notesCursorAdapter;
    private Activity activity;
    private ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        activity = this;

        dataNoteConnector = new NoteDataSource(activity);

        dataNoteConnector.open();

        cursorNotes = dataNoteConnector.getAllNotes();

        startManagingCursor(cursorNotes);

        listView = (ListView) findViewById(R.id.main_list);

        notesCursorAdapter = new NotesCursorAdapter(activity, cursorNotes, true);

        listView.setAdapter(notesCursorAdapter);

        Button b = new Button(activity);

        b = (Button) findViewById(R.id.done);

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                cursorNotes = dataNoteConnector.createNoteTop("American Alex is god");
                cursorNotes.requery();
                System.out.println("and everyone should listen to him");


//              notesCursorAdapter.changeCursor(cursorNotes); // automatically closes old Cursor
//              notesCursorAdapter.notifyDataSetChanged();
            }
        });

}

感谢您的关注。

编辑:我找到了解决方案,但我讨厌它,我希望有其他解决方案。

现在我有我的活动:

cursorNotes = dataNoteConnector.createNoteTop("American Alex is god");

                cursorNotes = dataNoteConnector.getAllNotes();

                notesCursorAdapter = new NotesCursorAdapter(activity, cursorNotes, true);

                listView.setAdapter(notesCursorAdapter);

我觉得这很疯狂,更新后应该有一种更简单的方法来更新列表。在这里,我正在重新创建一个游标,重新创建一个我用新游标提供的 cursorAdapter,然后将此 cursorAdapter 影响到 listView。这是来自 android doc 的记事本示例,但我确信有一种方法可以通过操纵光标来更新适配器和 listView,但我不够聪明,无法弄清楚。

4

3 回答 3

1

尝试取消评论活动中的最后一行

adapter.notifydatasetcanged();
于 2012-03-15T21:29:42.290 回答
0

好吧,我不会推荐@Aashish 解决方案。最好的办法是让你 listview 和适配器成员变量,然后重新创建适配器

private ListView listView;
private ListAdapter notesCursorAdapter;
private Cursor cursorNotes;

private void update() {
notesCursorAdapter = new NotesCursorAdapter(activity, cursorNotes, true); 
listView.setAdapter(notesCursorAdapter);
}
于 2012-03-16T08:24:37.673 回答
0

这就是谷歌对 requery() 的看法

此方法已弃用。不要使用这个。只需请求一个新游标,这样您就可以异步执行此操作,并在新游标返回后更新您的列表视图。

你可以在这里查看。 http://developer.android.com/reference/android/database/Cursor.html

所以他们实际上建议你做你已经做过的事情。:) 但如果您仍想以其他方式执行此操作,请取消注释 notesCursorAdapter.notifyDataSetChanged()。理想情况下,requery() 应该调用 noifydatasetchange(),但我观察到刷新列表需要显式调用。

于 2012-03-16T08:44:53.840 回答