use*_*694 7 android onresume android-listview simplecursoradapter notifydatasetchanged
我有一个Listview从sqlite数据库拉取并显示数据.数据库第一列中的数据显示在ListView单击中,单击时,Activity开始显示与第一列关联的列的其余部分.编辑数据时ListView需要更新以反映这一点,但除非重新启动应用程序,否则它不会显示更新.
我已经打过电话,notifyDataSetChanged()并startActivityForResult()在我的onResume()方法,但没有奏效.我应该使用什么方法来完成ListView当前代码的更新?
我理解SimpleCursorAdapter可以使用,我试图实现该代码没有成功.我是一个新手,需要实际的代码才能理解需要做什么.
public class LoginList extends Activity implements OnClickListener, OnItemClickListener {
private ListView loginList;
private Button webLogin;
private ListAdapter loginListAdapter;
private ArrayList<LoginDetails> loginArrayList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_listview);
loginList = (ListView)
findViewById(R.id.loginlist);
loginList.setOnItemClickListener(this);
webLogin = (Button)
findViewById(R.id.button3);
webLogin.setOnClickListener(this);
loginArrayList = new ArrayList<LoginDetails>();
loginListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList());
loginList.setAdapter(loginListAdapter);
}
@Override
public void onClick (View v) {
Intent webLoginIntent = new Intent (this, LoginPlusActivity.class);
startActivity(webLoginIntent);
}
public List<String> populateList () {
List<String> webNameList = new ArrayList<String>();
dataStore openHelperClass = new dataStore (this);
SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();
Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);
startManagingCursor(cursor);
while (cursor.moveToNext()) {
String sName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_SITE));
String wUrl = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_ADDRESS));
String uName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_USERNAME));
String pWord = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_PASSWORD));
String lNotes = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_NOTES));
LoginDetails lpDetails = new LoginDetails();
lpDetails.setsName(sName);
lpDetails.setwUrl(wUrl);
lpDetails.setuName(uName);
lpDetails.setpWord(pWord);
lpDetails.setlNotes(lNotes);
loginArrayList.add(lpDetails);
webNameList.add(sName);
}
sqliteDatabase.close();
return webNameList;
}
@Override
protected void onResume() {
super.onResume();
loginListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList());
loginList.setAdapter(loginListAdapter);
}
@Override
public void onItemClick(AdapterView<?> arg0 , View arg1, int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "Selected ID :" + arg2, Toast.LENGTH_SHORT).show();
Intent updateDeleteLoginInfo = new Intent (this, UpdateDeleteLoginList.class);
LoginDetails clickedObject = loginArrayList.get(arg2);
Bundle loginBundle = new Bundle();
loginBundle.putString("clickedWebSite",clickedObject.getsName());
loginBundle.putString("clickedWebAddress",clickedObject.getwUrl());
loginBundle.putString("clickedUserName",clickedObject.getuName());
loginBundle.putString("clickedPassWord",clickedObject.getpWord());
loginBundle.putString("clickedNotes",clickedObject.getlNotes());
updateDeleteLoginInfo.putExtras(loginBundle);
startActivityForResult(updateDeleteLoginInfo, 0);
}
}
Run Code Online (Sandbox Code Playgroud)
这正是一个Loader伟大的东西.我建议您创建一个SimpleCursorAdapter将数据库绑定到UI(ListView在本例中),一个ContentProvider与数据库的接口,以及一个CursorLoader监视数据库的更改,并在必要时更新UI.在Loader将处理所有DB的变化和更新您ListView只需更新您的适配器.它看起来像预先做了很多工作,但是一旦配置就非常强大,并且将贯穿整个Android生命周期.
这些教程应该是有用的:
编辑
private ArrayList<LoginDetails> loginArrayList = new ArrayList<LoginDetails>();;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_listview);
loginList = (ListView)
findViewById(R.id.loginlist);
loginList.setOnItemClickListener(this);
webLogin = (Button)
findViewById(R.id.button3);
webLogin.setOnClickListener(this);
loginListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,loginArrayList );
loginList.setAdapter(loginListAdapter);
populateList();
}
@Override
public void onClick (View v) {
Intent webLoginIntent = new Intent (this, LoginPlusActivity.class);
startActivity(webLoginIntent);
}
public void populateList () {
loginListAdapter.clear();
loginArrayList.clear();
dataStore openHelperClass = new dataStore (this);
SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();
Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);
startManagingCursor(cursor);
while (cursor.moveToNext()) {
String sName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_SITE));
String wUrl = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_ADDRESS));
String uName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_USERNAME));
String pWord = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_PASSWORD));
String lNotes = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_NOTES));
LoginDetails lpDetails = new LoginDetails();
lpDetails.setsName(sName);
lpDetails.setwUrl(wUrl);
lpDetails.setuName(uName);
lpDetails.setpWord(pWord);
lpDetails.setlNotes(lNotes);
loginArrayList.add(lpDetails);
webNameList.add(sName);
}
loginListAdapter.notifyDatasetChanged();
sqliteDatabase.close();
}
Run Code Online (Sandbox Code Playgroud)
您正在丢失对列表视图的引用,指出您的列表未更新的原因...
在代码中进行此修改.1.声明(全局)时初始化ArrayList.
ArrayList loginArrayList = new ArrayList<LoginDetails>();
Run Code Online (Sandbox Code Playgroud)
直接将分配列表设置为适配器(onCreate)
loginListAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,loginArrayList);
呼叫populateList()在onCreate().
populateList()的新列表中添加与您的适配器关联的现有列表,而不是添加数据loginArrayList如果您的列表是全新的呼叫,adapter.clear()并且loginArrayList.clear()在populateList()添加数据之前loginArrayList.将数据添加到loginArrayList呼叫后adapter.notifyDataSetChanged()
这应该工作......
我的推荐。
SimpleCursorAdapterand 将使您的应用程序更快、代码更短。Cursor,并使用Adapter。swapCursor(newCursor)。这将更新您的列表,同时保持滚动位置。| 归档时间: |
|
| 查看次数: |
7717 次 |
| 最近记录: |