Reputation: 557
Here is my grid view fragment, i'm planning on adding 50 drawable items to the grid, my problem is onclick method. How can intent to new Activity class for these 50 items. i do have a onClick method which is working fine, but adding 50 case statement is painful, is there any way to dynamically intent 50 activity class depending on their class name.
public class FragMent1 extends Fragment {
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gridview,container,false);
GridView gridView = (GridView) view.findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(getActivity())); // uses the view to get the context instead of getActivity().
gridView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {
// TODO Auto-generated method stub
Intent n = null;
switch(position){
case 0:
n = new Intent(getActivity(), abc.class);
getActivity().startActivity(n);
break;
case 1:
n = new Intent(getActivity(), bcd.class);
getActivity().startActivity(n);
break;
case 2:
// n = new Intent(getActivity(), cbe.class);
getActivity().startActivity(n);
break;
}
}
});
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
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;
}
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(500, 500));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
private Integer[] mThumbIds = {
R.drawable.car,
R.drawable.dabangg,
R.drawable.dabangg,
R.drawable.car,
R.drawable.car,
R.drawable.dabangg
};
}
}
Upvotes: 0
Views: 420
Reputation: 604
Maybe you can pass in a key to the activity based on the item they selected in the grid as an intent extra. Grab the passed in value, and do whatever needs to be done.
Example:
gridView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {
Myobject obj=(MyObject)adapter.getItem(position);
Intent intent=new Intent(CurrentActivity.this,NextActivity.class);
intent.putExtra("MY_KEY",obj.getMyValue());
startActivity(intent);
}
}
});
Then in your onCreate method of your next activity you do
int defaultValue=-1;
int value=getIntent().getIntExtra("MY_KEY", defaultValue);
switch (value){
case 0:
break
}
and so on
Upvotes: 1