【问题标题】:Why does my listView doesn't show inserted rows?为什么我的 listView 不显示插入的行?
【发布时间】:2012-03-17 03:10:24
【问题描述】:

我有一个自定义的 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,但我不够聪明,无法弄清楚。

【问题讨论】:

    标签: android listview cursor android-cursoradapter


    【解决方案1】:

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

    adapter.notifydatasetcanged();
    

    【讨论】:

    • 否 ... 以为重新查询功能会自动升级适配器内的光标并升级 listView,但即使取消注释 datasetchanged 也不起作用...
    • 好的,然后试试这个,它既麻烦又便宜,但它可以使用自我意图启动此活动,即 new Intent(alpha.this,alpha.class);finish();这样您的活动将重新开始,您的列表将被更新
    • 谢谢,但我一直在寻找一种简单的方法......我编辑了我的帖子,因为我找到了一个糟糕的解决方案,如果我发现我的代码中有什么问题,我会再次发布。感谢您的帮助
    【解决方案2】:

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

    此方法已弃用。不要使用这个。只需请求一个新游标, 因此您可以异步执行此操作,并在 新光标回来了。

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

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

    【讨论】:

      【解决方案3】:

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

      private ListView listView;
      private ListAdapter notesCursorAdapter;
      private Cursor cursorNotes;
      
      private void update() {
      notesCursorAdapter = new NotesCursorAdapter(activity, cursorNotes, true); 
      listView.setAdapter(notesCursorAdapter);
      }
      

      【讨论】:

      • 你认为重新创建一个适配器比将新光标切换到同一个适配器更好吗?
      • 创建新适配器并不会真正增加操作系统的开销,因为它会释放旧适配器。创建新游标将导致它重新运行查询,然后重绘。所以是的,我投票支持创建新适配器。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多