0

I have been trying to apply a gridview to a fragment but I am having issues with applying the adapter

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,
    };
}

I am getting the issues:

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

and

The constructor ImageAdapter(FragmentActivity) is undefined

Any suggestions?

4

2 回答 2

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;
}

}

于 2013-08-21T08:12:18.290 回答
0

使用这个:new ImageAdapter(getActivity().getApplicationContext())。您的构造使用 aContext而不是 a FragmentActivity(由getActivity()调用返回)

于 2013-08-20T23:08:59.573 回答