我对android非常新,当我点击它在视图寻呼机中显示的每个项目时我试图在视图寻呼机中显示我的网格项但是我错了所以请任何人帮助我 在我看来寻呼机适配器三个错误 Gallery中的GalleryAdapter(Activity,ArrayList)无法应用于GalleryViewpager
非静态方法getcount()无法从静态上下文中引用
非静态' listFlag'无法从静态上下文引用
任何人请帮助我,
这是我的主要活动适配器类 我的 的 GalleryAdapter.class
public class GalleryAdapter extends BaseAdapter{
public ArrayList<Integer> listFlag;
private Activity activity;
public GalleryAdapter(Activity activity, ArrayList<Integer> listFlag) {
super();
this.listFlag = listFlag;
this.activity = activity;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listFlag.size();
}
@Override
public String getItem(int position) {
// TODO Auto-generated method stub
return Integer.toString(listFlag.get(position));
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
public ImageView imgViewFlag;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.gallery_viewlayout, null);
view.imgViewFlag = (ImageView) convertView.findViewById(R.id.picture);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
//view.txtViewTitle.setText(listCountry.get(position));
view.imgViewFlag.setImageResource(listFlag.get(position));
return convertView;
}
}
这里是我的viewpager活动类
GalleryViewpagger.class
public class GalleryViewpagger extends AppCompatActivity {
private Toolbar mToolbar;
int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery_viewpagger);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
Intent p = getIntent();
position = p.getExtras().getInt("id");
GalleryAdapter galleryadapter=new GalleryAdapter(this);
List<ImageView> images = new ArrayList<ImageView>();
for (int i = 0; i < GalleryAdapter.getCount(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(GalleryAdapter.listFlag[i]);
imageView.setScaleType(ImageView.ScaleType.CENTER);
images.add(imageView);
}
// Set the images into ViewPager
ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);
VerticalViewPager viewpager = (VerticalViewPager) findViewById(R.id.pager);
viewpager.setAdapter(pageradapter);
// Show images following the position
viewpager.setCurrentItem(position);
}
}
my
MainActivity.class
public class AlbumView extends Fragment {
private GalleryAdapter mAdapter;
private ArrayList<Integer> listFlag;
private GridView gridView;
public AlbumView() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview= inflater.inflate(R.layout.fragment_album_view, container, false);
prepareList();
// prepared arraylist and passed it to the Adapter class
mAdapter = new GalleryAdapter(getActivity(), listFlag);
// Set custom adapter to gridview
gridView = (GridView)rootview.findViewById(R.id.gridView1);
gridView.setAdapter(mAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Launch ImageViewPager.java on selecting GridView Item
Intent i = new Intent(getActivity(), GalleryViewpagger.class);
// Show a simple toast message for the item position
// Send the click position to ImageViewPager.java using intent
i.putExtra("id", position);
// Start ImageViewPager
startActivity(i);
}
});
return rootview;
}
public void prepareList() {
listFlag = new ArrayList<Integer>();
listFlag.add(R.drawable.imagabun);
listFlag.add(R.drawable.bunnyarj);
listFlag.add(R.drawable.bunnyarj);
listFlag.add(R.drawable.imagabun);
listFlag.add(R.drawable.imagabun);
listFlag.add(R.drawable.bunnyarj);
listFlag.add(R.drawable.bunnyarj);
listFlag.add(R.drawable.imagabun);
listFlag.add(R.drawable.bunnyarj);
listFlag.add(R.drawable.bunnyarj);
listFlag.add(R.drawable.imagabun);
listFlag.add(R.drawable.imagabun);
listFlag.add(R.drawable.bunnyarj);
listFlag.add(R.drawable.bunnyarj);
listFlag.add(R.drawable.bunnyarj);
listFlag.add(R.drawable.bunnyarj);
listFlag.add(R.drawable.imagabun);
listFlag.add(R.drawable.imagabun);
listFlag.add(R.drawable.bunnyarj);
listFlag.add(R.drawable.bunnyarj);
}
}
感谢。
答案 0 :(得分:0)
Class
相反,它必须像:
GalleryAdapter galleryadapter=new GalleryAdapter(this);
或类似的东西。找到一个对象(listFlag)并将其传递给构造函数。
更新您的代码,让我们看看是否会再次发生其他问题。
<强>更新强>
由于您在MainActivity中初始化了listFlag,因此只需为GalleryAdapter创建另一个构造函数,该构造函数只需要一个参数Activity。
并尝试在该GalleryAdapter类中使listFlag对象为“static”,以便GC不会删除它。