我想顯示數據在一個'標籤列表',這是一個查看傳呼機和每頁內的列表視圖。爲此,我使用兩個適配器,一個用於查看傳呼機,另一個用於listview。其實現中的問題是,當我切換頁面時,列表視圖數據不會改變。以下是密碼Android:列表視圖數據沒有改變查看頁面
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;
}
}
}
我不使用的片段,每 – ashwinx
頁使用它。這將會更有效率。 –