Android 使用SimpleCursorAdapter更新ListView(无需重新初始化适配器)
我想使用Android 使用SimpleCursorAdapter更新ListView(无需重新初始化适配器),android,android-listview,android-cursoradapter,Android,Android Listview,Android Cursoradapter,我想使用SimpleCursorAdapter更新活动中的ListView小部件。我可以通过每次按下按钮时初始化simpledapter将项目添加到我的列表视图中,如下所示: public void saveButton(View view){ EditText editText = (EditText) findViewById(R.id.edit_text); String data = editText.getText().toString(); databas
SimpleCursorAdapter
更新活动中的ListView
小部件。我可以通过每次按下按钮时初始化simpledapter
将项目添加到我的列表视图中,如下所示:
public void saveButton(View view){
EditText editText = (EditText) findViewById(R.id.edit_text);
String data = editText.getText().toString();
database.insertData(data);
Log.v("database",database.getAllData().toString());
// initialization happens every time upon click.
// This method is coupled to a Button's onClick attribute.
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(),R.layout.layout_file,database.getAllData(),new String[]{"candy"},new int[]{R.id.label},SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listview.setAdapter(adapter);
}
但是,我希望复制相同的行为,而不必每次都重新初始化SimpleCursorAdapter
。我尝试使用bindView
方法,但我的ListView本身没有更新
这是全部代码。第一个是活动
:
public class MainActivity extends Activity {
ListView listview;
DatabaseTools database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database = new DatabaseTools(getApplicationContext(),null,null,1);
//ArrayList<String> result = database.getAllData();
Cursor cursor = database.getAllData();
listview = (ListView) findViewById(R.id.list_view);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(),R.layout.layout_file,cursor,new String[]{"candy"},new int[]{R.id.label},SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listview.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void saveButton(View view){
EditText editText = (EditText) findViewById(R.id.edit_text);
String data = editText.getText().toString();
database.insertData(data);
Log.v("database",database.getAllData().toString());
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(),R.layout.layout_file,database.getAllData(),new String[]{"candy"},new int[]{R.id.label},SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listview.setAdapter(adapter);
}
}
回答自己的问题。通过调用SimpleCursorAdapter
的changeCursor()
方法可以解决此问题
public void saveButton(View view){
EditText editText = (EditText) findViewById(R.id.edit_text);
String data = editText.getText().toString();
database.insertData(data);
Log.v("database",database.getAllData().toString());
// using the changeCursor method provides a cursor that can be
// updated with a 'screenshot' of the latest data returned by the database.
SimpleCursorAdapter adapter = (SimpleCursorAdapter) listview.getAdapter();
adapter.changeCursor(database.getAllData());
}
根据文档,changeCursor()
将基础光标更改为新光标。如果存在一个现有的光标,它将被关闭
public void saveButton(View view){
EditText editText = (EditText) findViewById(R.id.edit_text);
String data = editText.getText().toString();
database.insertData(data);
Log.v("database",database.getAllData().toString());
// using the changeCursor method provides a cursor that can be
// updated with a 'screenshot' of the latest data returned by the database.
SimpleCursorAdapter adapter = (SimpleCursorAdapter) listview.getAdapter();
adapter.changeCursor(database.getAllData());
}