Android:Listview数据在视图寻呼机内部没有变化

时间:2017-05-10 11:08:32

标签: android listview android-viewpager

我想在“标签列表”中显示数据,这是一个视图寻呼机和每个页面内的列表视图。为此,我使用两个适配器,一个用于视图寻呼机,一个用于列表视图。其实现中的问题是当我切换页面时列表视图数据没有改变。以下是代码

public class ALTabbedListAdapter extends PagerAdapter {

    protected Context mContext;

    protected ArrayList<String> originalTabsList = new ArrayList<>();
    public ArrayList<OEDataRow> filteredDataList = new ArrayList<>();
    public ArrayList<OEDataRow> originalDataList = new ArrayList<>();

    public String group_key;
    private int num_pages;

    public View mView;
    public ListView mListView;
    public  ALListViewAdapter listViewAdapter; // declared as an inner class below.

    public ALTabbedListAdapter(Context context, String type, ArrayList<String> groupkey_rows, ArrayList<OEDataRow> dataRows) {
        mContext = context;
        originalTabsList = groupkey_rows;
        group_key = type;
        originalDataList = dataRows;
        num_pages = groupkey_rows.size();
    }

    @Override
    public int getCount() {
        return num_pages;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return originalTabsList.get(position);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
        collection.removeView((View) view);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        mView = setupListView(position);// This function call sets up the listview and returns the view, defined below.
        container.addView(mView);
        return mView;

    }

    public ArrayList<OEDataRow> getListViewData(/*OEDataRow row*/String row, ArrayList<OEDataRow> originalDataListParam) {
        ArrayList<OEDataRow> dataRowArrayList = new ArrayList<>();
        for (OEDataRow dataRow : originalDataListParam) {
            if (Boolean.toString(dataRow.getBoolean(group_key)).equalsIgnoreCase(row)/*row.getString("name")*/) {
                dataRowArrayList.add(dataRow);
            }
        }
        return dataRowArrayList;
    }

    public View setupListView(int position) {
        View view= ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.tablayout_listview_layout, null);
        mListView = (ListView) view.findViewById(R.id.tablayout_listview);
        ArrayList<OEDataRow> finallist = getListViewData(originalTabsList.get(position)/*dataRow*/, originalDataList);
        if(listViewAdapter==null) {
            listViewAdapter = new ALListViewAdapter(finallist);
        } else {
            listViewAdapter.changeFilterData(finallist);
        }
        mListView.setAdapter(listViewAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            }
        });
        listViewAdapter.notifyDataSetChanged();
        return view;
    }


    public class ALListViewAdapter extends BaseAdapter {

        public View mListItemView;
        public LayoutInflater inflater;

        public ALListViewAdapter(ArrayList<OEDataRow> dataList) {
            changeFilterData(dataList);
            inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return filteredDataList.size();
        }

        public void changeFilterData(ArrayList<OEDataRow> dataList){
            if(filteredDataList != null) {
                filteredDataList.clear();
            }
            filteredDataList=dataList;
            this.notifyDataSetChanged();
        }

        @Override
        public Object getItem(int i) {
            return filteredDataList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            if(mListItemView == null) {
                mListItemView = inflater.inflate(R.layout.listitem, null);
            }
            //TODO How to make it extendable?
            OEDataRow row = filteredDataList.get(i);
            TextView tv_title = (TextView)mListItemView.findViewById(R.id.listitem_namefield);
            tv_title.setText(row.getString("name"));

            return mListItemView;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您只为所有视图寻呼机视图创建一个适配器实例,不应该为每个视图创建新的列表视图适配器实例:

     public View setupListView(int position) {
        View view= ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.tablayout_listview_layout, null);
        mListView = (ListView) view.findViewById(R.id.tablayout_listview);
        ArrayList<OEDataRow> finallist = getListViewData(originalTabsList.get(position)/*dataRow*/, originalDataList);

        final  ALListViewAdapter alListViewAdapter = new ALListViewAdapter(finallist);
        mListView.setAdapter(alListViewAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                // Do your stuff...


                alListViewAdapter.notifyDataSetChanged();
            }
        });

        return view;
    }

答案 1 :(得分:0)

你做错了。如果你想要基于标签的功能,那么在ViewPager中管理它,在ViewPager中制作每个屏幕的不同片段,然后在其中制作代码。

如果您使用的是片段方法,则可以使用此方法刷新列表视图

    @Override
        public void setMenuVisibility(boolean menuVisible) {
            super.setMenuVisibility(menuVisible);
            if(menuVisible)
                yourListView.notifyDataSetChanged();
        }

当您的片段进入可见区域时,此方法会调用该调用。