PagerAdapter无法应用于Android

时间:2015-07-25 08:12:49

标签: java android

我在ListView中添加了包含图像的视图寻呼机作为行项目。但是MyListAdapter在getView()方法中有问题,例如MyPagerAdapter adapter = new MyPagerAdapter(getContext(),getItem(position));我的代码在哪里出错。有人可以帮忙解决这个问题。谢谢。

这是我的代码

public class MyPagerAdapter extends PagerAdapter{

    private LayoutInflater mLayoutInflater;

    private static final int PAGE_NUM = 1;
    private String str;
    private int GalImages ;
    Context mContent;

    public MyPagerAdapter(Context context,int GalImages) {
        super();
        mLayoutInflater = LayoutInflater.from(context);
        this.GalImages = GalImages;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        LinearLayout layout = null;
        if(position == 0)
        {
            layout = (LinearLayout)mLayoutInflater.inflate(R.layout.page1, null);
            ImageView displayImag = (ImageView)layout.findViewById(R.id.text);
            displayImag.setImageResource(GalImages);
        }

        container.addView(layout);
        return layout;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public int getCount() {

        return PAGE_NUM;
    }

    @Override
    public boolean isViewFromObject(View view, Object obj) {

        return view.equals(obj);
    }

}

here is my ListAdapter

public class MyListAdapter extends ArrayAdapter<String> {

    private LayoutInflater inflater = null;

    public MyListAdapter(Context context, int resource, String[] items) {
        super(context, resource, items);
        inflater = LayoutInflater.from(context);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            convertView = inflater.inflate(R.layout.row,null);
        }

        ViewPager viewPager = (ViewPager)convertView.findViewById(R.id.viewpager);
        viewPager.setPageMargin(-margin);
        MyPagerAdapter adapter = new MyPagerAdapter(getContext(),getItem(position));
        viewPager.setAdapter(adapter);

        return convertView;
    }
}

here is my Activity code

public class MainActivity extends Activity {

    private int str[] = {R.drawable.star, R.drawable.star,  R.drawable.star};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView listView = (ListView)findViewById(R.id.listView);
        MyListAdapter adapter = new MyListAdapter(this,R.layout.row,str);
        listView.setAdapter(adapter);
    }}

PagerAdapter无法应用于Android中的(context,java.lang,String) 在这一行= MyListAdapter adapter = new MyListAdapter(this,R.layout.row,str);

1 个答案:

答案 0 :(得分:0)

MyPagerAdapter的构造函数采用Context和int。您正在MyListAdapter中使用Context和String调用构造函数。