如何在不重新设置自定义适配器的情况下刷新ExpandableList

时间:2015-02-18 07:44:24

标签: android android-arrayadapter expandablelistview baseadapter

我有一个扩展BaseExpandableListAdapter的自定义适配器。该适配器使用LinkedHashMap>变量类型作为源。当一个新数据到来时,我调用.setAdapter方法刷新可扩展列表,这是非常低效和编码错误。当我更新那个linkedhashmap并调用notifydatasetchanged并且notifydatasetinvalidated时,什么也没发生。这是我的填充代码;

注意:在我的适配器中hasStableIds为真

 LinkedHashMap<String,ArrayList<Income>> IncomeList = new LinkedHashMap<>();
SetIncomeTask incomeTask;
IncomesAdapter incomesAdapter;
IncomeDbAdapter incomeDbAdapter;

....

 class SetIncomeTask extends AsyncTask<Void,Void,Void>
{
    @Override
    protected Void doInBackground(Void... params) {
        incomeDbAdapter = new IncomeDbAdapter(getActivity());
        if(incomeDbAdapter.selectAllCount()>0){
            if(groupBy.getSelectedItemPosition() == 0)
                IncomeList.putAll(incomeDbAdapter.selectLastEntriesFilterByDate(DefaultValues.lastDays));
            else
                IncomeList.putAll(incomeDbAdapter.selectLastEntriesFilterByCategory(DefaultValues.lastDays));
                return null;
        }
        else
            return null;
    }

    @Override
    protected void onPostExecute(Void v) {

        if(IncomeList != null) {
            if(incomes.getAdapter()== null) {
                if(groupBy.getSelectedItemPosition() == 0)
                    incomesAdapter = new IncomesAdapter(getActivity(), IncomeList,true);
                else
                    incomesAdapter = new IncomesAdapter(getActivity(), IncomeList,false);

                incomes.setAdapter(incomesAdapter);
            }else {
                incomesAdapter.notifyDataSetChanged();
                incomesAdapter.notifyDataSetInvalidated();
            }

        }
    }
}

1 个答案:

答案 0 :(得分:0)

默认情况下,只要您拨打null,就会使用适配器AsyncTask制作适配器null

谢谢你,如果我帮助了你。