gpt4 book ai didi

java - 在 Android 中刷新 ListAdapter

转载 作者:行者123 更新时间:2023-12-01 22:44:11 26 4
gpt4 key购买 nike

我在尝试刷新 Android 中的 ListView 时遇到一些问题。这是我的 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刷新 ListView :

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 时, ListView 应该只显示 1-4。但通过我的代码,我得到的是 1,2,3,1,2,3,4。我想知道为什么 listAdapter 继续堆叠记录,尽管我将其设置为 null 并再次重建它。

提前致谢。

最佳答案

您没有清除旧列表数据。您已从列表中删除旧记录。

    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();
}

关于java - 在 Android 中刷新 ListAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25568221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com