在将数据设置为列表视图中的列表项时,我需要您的帮助。在我的列表视图中,每个项目都有一个 ViewPager,每个 viewpager 应该有不同的数据。我不知道如何为每个 viewpager 设置不同的数据。实际上基于 listitem 索引我需要设置数据查看器。请仔细阅读我粘贴在下面的代码片段并尝试帮助我。
public class MyListAdapter extends BaseAdapter {
private Activity activity;
private String [] titleData;
private int [] imageData;
private static LayoutInflater inflater=null;
private int iListPosition;
private GraphicalView graphView ;
ListView dashListView;
private Context context;
public MyListAdapter(View listView, Activity a, String[] titles, int[] images) {
activity = a;
context = a.getApplicationContext();
dashListView = (ListView)listView;
}
public int getCount() {
return titleData.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
iListPosition = position;
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) activity.getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.dash_board_list_row, parent, false);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) rowView.findViewById(R.id.threepageviewer);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
return rowView;
}
private class MyPagerAdapter extends PagerAdapter {
public int getCount() {
return 3;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0:
resId = R.layout.weight_middle;
break;
case 1:
resId = R.layout.weight_left;
break;
case 2:
resId = R.layout.weight_right;
break;
}
View view = inflater.inflate(resId, null);
if(resId == R.layout.weight_left && iListPosition == 0){
LinearLayout layout = (LinearLayout) view.findViewById(R.id.chartView);
layout.addView(graphView);
}
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub
}
}
}
但是在这里,所有列表项的 Viewpagers 都设置了相同的数据。理想情况下,我的要求是设置不同的数据。如果您有/知道任何解决方案,请告诉我。