尝试刷新Android中的列表视图时出现问题。这是我的onCreate方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transaction_rec);
context = this;
listview = (ListView) findViewById(R.id.listview);
builtListView();
}
public void builtListView() {
DatabaseAdapter mDbHelper = new DatabaseAdapter(this);
mDbHelper.createDatabase();
mDbHelper.open();
TransactionRecController trc = new TransactionRecController(
mDbHelper.open());
ArrayList<TransactionRecModel> trans_list = trc
.getTransactionRec(session_userID);
for (int i = 0; i < trans_list.size(); i++) {
String date = trans_list.get(i).getDate();
String categoryName = trans_list.get(i).getCategory();
String transDesc = trans_list.get(i).getTransDescription();
TransactionRecModel _TransModel = new TransactionRecModel();
_TransModel.setDate(date);
_TransModel.setCategory(categoryName);
_TransModel.setTransDescription(transDesc);
_translist.add(_TransModel);
}
// Set the data into listview
listview.setAdapter(new ListAdapter(this));
mDbHelper.close();
}
我创建了一个rebuildListView方法,将listAdapter设置为null并再次调用buildListView方法:
public void rebuildListView() {
listview.setAdapter(null);
builtListView();
}
当我成功将记录插入数据库时,我通过调用rebuildListView刷新列表视图:
TransactionRecController trc = new TransactionRecController(
mDbHelper.open());
if (trc.addTransactionRec(trm)) {
Toast.makeText(TransactionRec.this,
"Transaction Record Added successfully.",
Toast.LENGTH_LONG).show();
mDbHelper.close();
txtTransDesc.setText("");
txtAmount.setText("");
txtDate.setText("");
// Refresh the list view after record added
rebuildListView();
这是我的listAdapter类:
private class ListAdapter extends BaseAdapter {
LayoutInflater inflater;
ViewHolder viewHolder;
public ListAdapter(Context context) {
// TODO Auto-generated constructor stub
inflater = LayoutInflater.from(context);
}
// Bind the data to different textView for each rows
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = inflater.inflate(R.layout.trans_listview_row,
null);
viewHolder = new ViewHolder();
viewHolder.txt_ddate = (TextView) convertView
.findViewById(R.id.txtDisplayDate);
viewHolder.txt_ddesc = (TextView) convertView
.findViewById(R.id.txtDisplayDesc);
viewHolder.txt_dcat = (TextView) convertView
.findViewById(R.id.txtDisplayCat);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.txt_ddate.setText(_translist.get(position).getDate()
.trim());
viewHolder.txt_ddesc.setText(_translist.get(position)
.getTransDescription().trim());
viewHolder.txt_dcat.setText(_translist.get(position).getCategory()
.trim());
return convertView;
}
}
然而,我现在的问题是让我说我先得到记录1,2和3。然后当我添加另一个4的记录时,列表视图应该只显示1-4。但是根据我的代码,我得到的是 1,2,3,1,2,3,4 。我想知道为什么listAdapter会继续堆叠记录,尽管我将它设置为null并重新生成它。
提前致谢。
答案 0 :(得分:3)
您没有清除旧的列表数据。你已从列表中删除旧记录。
public void builtListView() {
_translist.clear();
DatabaseAdapter mDbHelper = new DatabaseAdapter(this);
mDbHelper.createDatabase();
mDbHelper.open();
TransactionRecController trc = new TransactionRecController(
mDbHelper.open());
ArrayList<TransactionRecModel> trans_list = trc
.getTransactionRec(session_userID);
for (int i = 0; i < trans_list.size(); i++) {
String date = trans_list.get(i).getDate();
String categoryName = trans_list.get(i).getCategory();
String transDesc = trans_list.get(i).getTransDescription();
TransactionRecModel _TransModel = new TransactionRecModel();
_TransModel.setDate(date);
_TransModel.setCategory(categoryName);
_TransModel.setTransDescription(transDesc);
_translist.add(_TransModel);
}
// Set the data into listview
listview.setAdapter(new ListAdapter(this));
mDbHelper.close();
}
或者您可以重新分配值
public void builtListView() {
_translist.clear();
DatabaseAdapter mDbHelper = new DatabaseAdapter(this);
mDbHelper.createDatabase();
mDbHelper.open();
TransactionRecController trc = new TransactionRecController(
mDbHelper.open());
_translist = trc
.getTransactionRec(session_userID);
// Set the data into listview
listview.setAdapter(new ListAdapter(this));
mDbHelper.close();
}