2017-05-10 67 views
-2

我想顯示數據在一個'標籤列表',這是一個查看傳呼機和每頁內的列表視圖。爲此,我使用兩個適配器,一個用於查看傳呼機,另一個用於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; 
     } 
    } 
} 

回答

0

你做錯了。如果你想要基於標籤的功能,然後在ViewPager和ViewPager中管理它,每個屏幕的不同片段,然後在那做代碼。

如果您正在使用片段的方法,那麼你可以用這個方法來刷新列表視圖

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

此方法調用時撥打電話片段來可視區域。

+0

我不使用的片段,每 – ashwinx

+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; 
    } 
+0

我在'setupListView'中創建了一個新的'ALListViewAdapter',它在尋呼機適配器的'instantiateItem'內被調用。那不就是你說的嗎? – ashwinx

+0

不,instantiateItem是正確的,但如果您在View pager中有10個視圖,那麼您的適配器實例將只創建一個視圖,但不應該,它應該對每個視圖始終是新的。更新你的setupListView方法代碼,plz檢查... –

+0

你必須爲每個視圖尋呼機視圖創建新的Adapter對象。 –

相關問題