Android ViewPager and ListViews

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

I'm relatively new to Android development and development overall. I'm having trouble grasping the syntax/process for ViewPager.

I have several ListViews that I want to implement into a ViewPager. I've got the compatibility pack loaded and everything. But I haven't seen any complete examples of how to do this.

I learn best by looking at examples. If someone can post an examples of any projects you've implemented this sort of thing in, please let me know.

The issue is that I get a Null Pointer Exception on this line when trying to launch my activity:

listView1.setAdapter(new ArrayAdapter(this, R.layout.rowlayout, list1)); 

I suspect that I'm just doing this all wrong. If I don't use the ViewPager, I can get both lists to display their content. So I know the lists aren't null...

EDIT:

Thanks to VenomM for the answer below! Here's the code I ended up using, slightly modified from VenomM's examples.

ViewPagerAdapter:

public class ViewPagerAdapter extends PagerAdapter implements TitleProvider {     private ListView listView1;     private static String[] titles = new String[]     {        "Page 1",        "Page 2",        "Page 3",     };     private final Context context;      public ViewPagerAdapter( Context context )     {         this.context = context;     }      @Override     public String getTitle( int position )     {         return titles[ position ];     }      @Override     public int getCount()     {         return titles.length;     }      @Override     public Object instantiateItem(View collection, int position) {          LayoutInflater layoutInflater = ((Activity) context).getLayoutInflater();          listView1 = (ListView) layoutInflater.inflate(R.layout.listview1, null);          String[] listData = null;         MyArrayAdapter dataAdapter;          if (position == 0) {           listData = context.getResources().getStringArray(R.array.list1);           dataAdapter = new MyArrayAdapter((Activity) context,               R.layout.rowlayout, listData);         } else if (position == 1) {           listData = context.getResources().getStringArray(R.array.list2);           dataAdapter = new MyArrayAdapter((Activity) context,               R.layout.rowlayout, listData);         } else {           listData = context.getResources().getStringArray(R.array.list3);           dataAdapter = new MyArrayAdapter((Activity) context,               R.layout.rowlayout, listData);         }          listView1.setAdapter(dataAdapter);         listView1.setOnItemClickListener(new OnItemClickListener() {             @Override             public void onItemClick(AdapterView> adapter, View view,                 int position, long arg3) {               Toast.makeText(context,                   adapter.getAdapter().getItem(position).toString(),                   Toast.LENGTH_LONG).show();             }         });          ((ViewPager) collection).addView(listView1, 0);          return listView1;     }       @Override     public void destroyItem(View collection, int position, Object view) {         System.out.println("on destroyItem()");         ((ViewPager) collection).removeView((ListView) view);     }      @Override     public boolean isViewFromObject(View view, Object object) {         System.out.println("on isViewFromObject()");         return view == ((ListView) object);     }      @Override     public void finishUpdate( View view ) {}      @Override     public void restoreState( Parcelable p, ClassLoader c ) {}      @Override     public Parcelable saveState() {         return null;     }      @Override     public void startUpdate( View view ) {} } 

ArrayAdapter:

public class MyArrayAdapter extends ArrayAdapter{      private Activity context = null;     private String[] names = null;     private int rowLayoutId;     public MyArrayAdapter(Activity context, int textViewResourceId, String[] names) {         super(context, textViewResourceId, names);         this.context = context;         this.names = names;         this.rowLayoutId =textViewResourceId;     }      // static to save the reference to the outer class and to avoid access to     // any members of the containing class     static class ViewHolder {         protected ImageView imageView;         protected TextView textView;     }  } 

回答1:

I still can't understand why you change your Arrayadapter with object argument, if you want it to hold string items. Try changing

new ArrayAdapter(this, R.layout.rowlayout, list1) 

to

new ArrayAdapter(this, R.layout.rowlayout, list1) 

I used a custom ArrayAdapter, everything worked fine for me. Please let me know if you succeed.



回答2:

Here is an example:

MainActivity:

public class MainActivity extends Activity {      /** Called when the activity is first created. */   private Context mContext;      @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         mContext = this;         setContentView(R.layout.main);         ListView listview1 = new ListView(mContext);         ListView listview2 = new ListView(mContext);         ListView listview3 = new ListView(mContext);          Vector pages = new Vector();          pages.add(listview1);         pages.add(listview2);         pages.add(listview3);          ViewPager vp = (ViewPager) findViewById(R.id.viewpager);         CustomPagerAdapter adapter = new CustomPagerAdapter(mContext,pages);         vp.setAdapter(adapter);          listview1.setAdapter(new ArrayAdapter(mContext, android.R.layout.simple_list_item_1,new String[]{"A1","B1","C1","D1"}));         listview2.setAdapter(new ArrayAdapter(mContext, android.R.layout.simple_list_item_1,new String[]{"A2","B2","C2","D2"}));         listview3.setAdapter(new ArrayAdapter(mContext, android.R.layout.simple_list_item_1,new String[]{"A3","B3","C3","D3"}));      } } 

CustomViewPagerAdapter.java

public class CustomPagerAdapter extends PagerAdapter {   private Context mContext;  private Vector pages;   public CustomPagerAdapter(Context context, Vector pages) {   this.mContext=context;   this.pages=pages;  }   @Override  public Object instantiateItem(ViewGroup container, int position) {   View page = pages.get(position);   container.addView(page);   return page;  }   @Override  public int getCount() {   return pages.size();  }   @Override  public boolean isViewFromObject(View view, Object object) {   return view.equals(object);  }   @Override  public void destroyItem(ViewGroup container, int position, Object object) {   container.removeView((View) object);  }  } 

For more information check my blog. Here.



回答3:

Which ViewPager are you using? The V4 Compatibility jar?

If so, double check the events. Specifically the difference between onCreate,onCreateView & onActivityCreated.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!