I am having some problem when trying to refresh the list view in Android. This is my onCreate method:
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();
}
And I create a rebuildListView method to set listAdapter to null and call the buildListView method again:
public void rebuildListView() {
listview.setAdapter(null);
builtListView();
}
And when I successfully insert the record into database, I refresh the list view by calling the 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();
And this is my listAdapter class:
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;
}
}
However, my problem now is let's say I got record 1,2 and 3 first. Then when I added another record which is 4, the list view supposed to show me 1-4 only. But with my codes, what I am getting is 1,2,3,1,2,3,4 . I wonder why the listAdapter keep on stacking the record although I set it to null and rebuild it again.
Thanks in advance.
You are not clearing the old list data. you have remove the old records from the list.
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();
}
or you can reassign the values
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.