我在嘗試刷新Android中的列表視圖時遇到了一些問題。這是我的onCreate方法:在Android中刷新ListAdapter
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爲空,然後再次調用buildListView方法:
public void rebuildListView() {
listview.setAdapter(null);
builtListView();
}
當我成功地插入記錄到數據庫中,我刷新
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類:
致電rebuildListView列表視圖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並重新構建它。
在此先感謝。
在你BuilListView方法,其中,定義ArrrayList,嘗試:ArrayList的 trans_list =新的ArrayList ; \t trans_list = trc.getTransactionRec(session_userID); –
Dhaval
2014-08-29 12:27:59
@Dhaval它做什麼? – 2014-08-29 12:29:58
嘗試改變我的臺詞。它會創建一個新的數組列表,因此沒有舊的記錄。 – Dhaval 2014-08-29 12:37:22