Android列表视图内容在回拨时不会刷新

时间:2017-05-18 06:29:03

标签: java android listview

尝试在回拨时刷新列表视图内容时遇到了一些问题。在我的onCreate()中:

private ListAdapter mAdapter;
private ListView listview;

mAdapter = new ListAdapter(getActivity());
listview.setAdapter(mAdapter);

然后,在我的回调中工作,因为我设法打印出数据:

public void callback(ArrayList<Transaction> list)
{
    expenseslist.clear();

    Log.d("SIZE", String.valueOf(list.size()));
    for(int i = 0; i < list.size(); i++){
        expenseslist.add(list.get(i));
    }

    mAdapter.notifyDataSetChanged();
    listview.invalidateViews();
}


private class ListAdapter extends BaseAdapter {
    LayoutInflater inflater;
    ViewHolder viewHolder;

    public ListAdapter(Context context) {
        inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return expenseslist.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.transactioncat_listview_row,
                    null);
            viewHolder = new ViewHolder();

            viewHolder.txt_dcatTitle = (TextView) convertView
                    .findViewById(R.id.txtCatTitle);
            viewHolder.txt_dcatTotal = (TextView) convertView
                    .findViewById(R.id.txtCatTotal);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.txt_dcatTitle.setText(expenseslist.get(position)
                .getCategory().getCategoryName().trim());
        viewHolder.txt_dcatTotal.setText(Extra.formatCurrency(expenseslist.get(position)
                .getTransactionAmt()));

        return convertView;
    }
}

我调用了notifyDataSetChanged()和invalidateViews(),但它没有刷新。我不是在列表视图中添加新行,而是更新每行中的内容。

有什么想法吗?先谢谢!

修改

public class ExpenseActivity extends Fragment implements TransactionListCallBack{

    private ArrayList<Transaction> transactionlist = new ArrayList<Transaction>();
    private ArrayList<Transaction> expenseslist = new ArrayList<Transaction>();

    private ListAdapter mAdapter;
    private ListView listview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_expense,container,false);

        expenseslist.clear();

        transactionlist = (ArrayList<Transaction>)getArguments().getSerializable("transactionlist");
        for(int j = 0; j < transactionlist.size(); j++){
            expenseslist.add(transactionlist.get(j));
        }

        listview = (ListView) v.findViewById(R.id.transactionCatListview);

        // Binding the list into list view
        mAdapter = new ListAdapter(getActivity());
        listview.setAdapter(mAdapter);

        return v;
    }

    @Override
    public void callback(ArrayList<Transaction> list)
    {
        total = 0;
        expenseslist.clear();

        for(int i = 0; i < list.size(); i++){
            expenseslist.add(list.get(i));
            }
        }

        mAdapter.notifyDataSetChanged();
        listview.invalidateViews();
    }

    private class ListAdapter extends BaseAdapter {
        LayoutInflater inflater;
        ViewHolder viewHolder;

        public ListAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return expenseslist.size();
        }

        public Object getItem(int position) {
            return expenseslist.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {

                convertView = inflater.inflate(R.layout.transactioncat_listview_row,
                        null);
                viewHolder = new ViewHolder();

                viewHolder.txt_dcatTitle = (TextView) convertView
                        .findViewById(R.id.txtCatTitle);
                viewHolder.txt_dcatTotal = (TextView) convertView
                        .findViewById(R.id.txtCatTotal);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.txt_dcatTitle.setText(expenseslist.get(position)
                    .getCategory().getCategoryName().trim());

            viewHolder.txt_dcatTotal.setText(Extra.formatCurrency(expenseslist.get(position)
                    .getTransactionAmt()));

            return convertView;
        }
    }

    private class ViewHolder {
        CircularProgressBar progressBar;
        ImageView background_catview;
        ImageView icon_catview;
        TextView txt_dcatTitle;
        TextView txt_dcatTotal;
    }

    // override menu item to add new transaction
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        inflater.inflate(R.menu.menu_main_addtransaction, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_openNewTransaction) {
            new GetAllCategoriesAsyncTask(
                    new GetAllCategoriesAsyncTask.OnRoutineFinished() {
                        public void onFinish() {
                            AddTransactionActivity addFragment = new AddTransactionActivity();
                            FragmentTransaction tt = getFragmentManager().beginTransaction();

                            addFragment.setTransactionListCallBack(ExpenseActivity.this);

                            tt.replace(R.id.frame,addFragment);
                            tt.addToBackStack("tag");
                            tt.commit();
                        }
                    }).execute();
        }
        return super.onOptionsItemSelected(item);
    }
}

4 个答案:

答案 0 :(得分:0)

您需要在适配器中保留数据列表以使notifyDataSetChanged正常工作:

更改

mAdapter = new ListAdapter(getActivity(), expenseslist);

在适配器内:

list<Transaction> expenseslist;

public ListAdapter(Context context, list<Transaction> expenseslist) {
    inflater = LayoutInflater.from(context);
    this.expenseslist = expenseslist;
}

修改

同时更改这些方法:

    public Transaction getItem(int position) {
        return expenseslist.get(position);
    }

    public long getItemId(int position) {
        return expenseslist.get(position).getId();
    }

答案 1 :(得分:0)

你好适配器中的Denise创建了一个名为updateTransactions()的新函数。这是函数的代码,其中expenselist是您传递给适配器的列表。

public void updateTransactions(List<Transaction> list) {
    expenseslist.clear();
    expenseslist.addAll(list);
    notifyDataSetChanged();

    Log.d("SIZE", String.valueOf(expensesList.size()));
}

然后在你的回调函数中使用像这样的适配器实例调用updateTransactions()

public void callback(ArrayList<Transaction> list)
{
    mAdapter.updateTransactions(list);
}

编辑:演示运行代码

package ********;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by Rishabh Bhatia on 18/5/17.
 */

public class TestActivity extends Activity {

    private ListView list;
    private ListAdapter adapter;
    private ArrayList<String> dataList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        list = (ListView) findViewById(R.id.list);
        dataList = new ArrayList<>();

        for (int i = 0; i < 20; i++) {
            String string = "abc";
            dataList.add(string);
        }

        adapter = new ListAdapter(this, dataList);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("rishabh", "something is selected");
                String transaction = dataList.get(0);
                transaction = "xyz";
                dataList.remove(0);
                dataList.add(0, transaction);
                adapter.updateExpenses(dataList);
            }
        });
    }

    private class ListAdapter extends BaseAdapter {

        private ArrayList<String> expenseslist;

        LayoutInflater inflater;
        ViewHolder viewHolder;

        public ListAdapter(Context context, ArrayList<String> list) {
            inflater = LayoutInflater.from(context);
            this.expenseslist = (ArrayList<String>) list.clone();
        }

        public int getCount() {
            return expenseslist.size();
        }

        public Object getItem(int position) {
            return expenseslist.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {

                convertView = inflater.inflate(R.layout.row,
                        null);
                viewHolder = new ViewHolder();

                viewHolder.text = (TextView) convertView
                        .findViewById(R.id.tvRow);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.text.setText(expenseslist.get(position));

            return convertView;
        }

        public void updateExpenses(ArrayList<String> list) {
            Log.d("rishabh", "updating the expenses "+list.size());
            expenseslist.clear();
            for(int i =0; i<list.size(); i++) {
                Log.d("rishabh", "inside for loop");
                expenseslist.add(list.get(i));
                Log.d("rishabh", "updating item to list "+list.get(i));
            }
            notifyDataSetChanged();
            Log.d("rishabh", "updating the expenses "+expenseslist.size());
        }
    }

    static class ViewHolder {
        private TextView text;
    }

}

希望它有所帮助。

答案 2 :(得分:0)

进行此更改

public Object getItem(int position) {
    return expenseslist.get(position);
}

答案 3 :(得分:0)

你是在片段内编码吗?我可以问你在何时/何时使用这种方法

public void callback(ArrayList list);

正如我猜你在添加片段后立即从生命周期方法的活动中调用此方法。或者你可能不会在带有try-catch块的UI线程中调用此方法。