我有一个自定义的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,但我不够聪明,无法弄明白。
答案 0 :(得分:1)
尝试取消评论活动中的最后一行
adapter.notifydatasetcanged();
答案 1 :(得分:0)
我不会推荐@Aashish解决方案。最好的方法是使listview和适配器成员变量,然后重新创建适配器
private ListView listView;
private ListAdapter notesCursorAdapter;
private Cursor cursorNotes;
private void update() {
notesCursorAdapter = new NotesCursorAdapter(activity, cursorNotes, true);
listView.setAdapter(notesCursorAdapter);
}
答案 2 :(得分:0)
这就是谷歌对requery()
所说的话不推荐使用此方法。不要使用它。只需要一个新光标, 因此,您可以异步执行此操作并在更新后再更新列表视图 新光标回来了。
你可以在这里查看。 http://developer.android.com/reference/android/database/Cursor.html
所以他们实际上建议你做你做过的事。 :)但是如果您仍然想以另一种方式执行此操作,请取消注释notesCursorAdapter.notifyDataSetChanged()。理想情况下,requery()应该调用noifydatasetchange(),但我观察到刷新列表需要显式调用。