i'm working on simple app , i made a viewpager of 3 views, i need to put listview in the viepager but the listview won't showup here's my special class for viewpager package com.Schoolreporting.androidApp;
import java.util.ArrayList;
import android.content.Context;
import android.location.Address;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MyPagerAdapter extends PagerAdapter {
String Name;
// State number of pages
public int getCount() {
return 4;
}
public void setName(String Name) {
this.Name = Name;
}
// Set each screen's content
@Override
public Object instantiateItem(View container, int position) {
Context context = container.getContext();
LinearLayout layout = new LinearLayout(context);
// Add elements
TextView textItem = new TextView(context);
ListView grades=new ListView(context);
ArrayList<String>names=new ArrayList<String>();
names.add("Adel");
names.add("zetta");
names.add("Pringy");
ArrayAdapter<String> adapter=new ArrayAdapter<String> (context,android.R.layout.simple_list_item_1,names);
grades.setAdapter(adapter);
switch (position) {
case 0:
Toast.makeText(context,names.get(1),Toast.LENGTH_LONG).show();
textItem.setText(Name + "'s Grades");
grades.setAdapter(adapter);
Toast.makeText(context,names.get(1),Toast.LENGTH_LONG).show();
break;
case 1:
grades.setAdapter(adapter);
textItem.setText(Name + "'s grades");
break;
case 2:
textItem.setText( Name + "'s feedback");
break;
case 3:
textItem.setText( Name + "'s school");
break;
}
layout.addView(textItem);
((ViewPager) container).addView(layout, 0); // This is the line I
// added
return layout;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
You can do so using the following code:
Context context;
List<View> viewList;
public MyPagerAdapter(Context context) {
this.context = context;
viewList = new ArrayList<View>();
LayoutInflater inflater = LayoutInflater.from(context);
View pg1 = inflater.inflate(R.layout.pg1_layout, null);
View pg2 = inflater.inflate(R.layout.pg2_layout, null);
View pg3 = inflater.inflate(R.layout.pg3_layout, null);
viewList.add(pg1);
viewList.add(pg2);
viewList.add(pg3);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.