片段中gridview的问题

时间:2013-08-20 22:49:29

标签: android gridview

我一直在尝试将gridview应用于片段,但是我在应用适配器时遇到了问题

public class Playerstats extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    //super.onCreate(savedInstanceState);

    View view = inflater.inflate(R.layout.playerstats, container, false);

    GridView gridview = (GridView) view.findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(getActivity()));

    gridview.setOnItemClickListener(new OnItemClickListener() {

          public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(getActivity().getApplicationContext(), "" + position, Toast.LENGTH_SHORT).show();
            }
        });
       return view;
    }


   public class ImageAdapter extends BaseAdapter {
   private Context mContext;

   public ImageAdapter(Context c) {
       mContext = c;
   }

   public int getCount() {
       return mThumbIds.length;
   }

   public Object getItem(int position) {
       return null;
   }

   public long getItemId(int position) {
       return 0;
   }

   // create a new ImageView for each item referenced by the Adapter
   public View getView(int position, View convertView, ViewGroup parent) {
       ImageView imageView;
       if (convertView == null) {  // if it's not recycled, initialize some attributes
           imageView = new ImageView(mContext);
           imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
           imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
           imageView.setPadding(8, 8, 8, 8);
       } else {
           imageView = (ImageView) convertView;
       }

       imageView.setImageResource(mThumbIds[position]);
       return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
       R.drawable.sample_2, R.drawable.sample_3,
    };
}

我遇到了问题:

The method setAdapter(ListAdapter) in the type GridView is not applicable for the arguments (ImageAdapter)

The constructor ImageAdapter(FragmentActivity) is undefined

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

使用此:new ImageAdapter(getActivity().getApplicationContext())。您的构造使用Context代替FragmentActivity(由getActivity()调用返回)

答案 1 :(得分:0)

这是完整的代码

public class Playerstats extends Fragment{

GridView gridview;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    //super.onCreate(savedInstanceState);
    View view = inflater.inflate(R.layout.playerstats, container, false);

     gridview = (GridView) view.findViewById(R.id.gridview);
     gridview.setAdapter(new ImageAdapter(getActivity().getApplicationContext()));



       gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(getActivity().getApplicationContext(), "" + position, Toast.LENGTH_SHORT).show();
            }
        });
       return view;
}

}