简体   繁体   English

在Android中刷新ListAdapter

[英]Refresh ListAdapter in Android

I am having some problem when trying to refresh the list view in Android. 尝试在Android中刷新列表视图时遇到问题。 This is my onCreate method: 这是我的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();
}

And I create a rebuildListView method to set listAdapter to null and call the buildListView method again: 然后,我创建一个rebuildListView方法以将listAdapter设置为null,然后再次调用buildListView方法:

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: 当我成功地将记录插入数据库时​​,我通过调用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: 这是我的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;
    }
}

However, my problem now is let's say I got record 1,2 and 3 first. 但是,我现在的问题是,我先获得了记录1,2和3。 Then when I added another record which is 4, the list view supposed to show me 1-4 only. 然后,当我添加另一个记录是4时,列表视图应该只显示1-4。 But with my codes, what I am getting is 1,2,3,1,2,3,4 . 但是用我的代码,我得到的是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. 我不知道为什么为什么listAdapter继续堆叠记录,尽管我将其设置为null并再次重建它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM