0

データベースにアイテムを追加するとlistViewに表示されないため、確かに何かが欠落しているカスタムcursorAdapterがありますが、アプリケーションを再起動すると、追加された行が表示されます。

これが私のアクティビティと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に影響を与えます。これはandroiddocのメモ帳の例ですが、カーソルを操作するだけでアダプターとlistViewを更新する方法があると確信していますが、それを理解するのに十分な知識はありません。

4

3 に答える 3

1

アクティビティの最後の行のコメントを外してみてください

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

@Aashishソリューションはお勧めしません。最良の方法は、リストビューとアダプターのメンバー変数を作成してから、アダプターを再作成することです。

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

これはGoogleがrequery()について言わなければならないことです

このメソッドは非推奨です。これは使用しないでください。新しいカーソルをリクエストするだけなので、これを非同期で実行し、新しいカーソルが戻ってきたらリストビューを更新できます。

こちらで確認できます。 http://developer.android.com/reference/android/database/Cursor.html

だから彼らは実際にあなたがしたことをすることを提案します。:)それでも他の方法でやりたい場合は、notesCursorAdapter.notifyDataSetChanged()のコメントを外してください。理想的には、requery()がnoifydatasetchange()を呼び出すことになっていますが、リストを更新するには明示的な呼び出しが必要であることがわかりました。

于 2012-03-16T08:44:53.840 に答える