我有以下代码。当我在网格视图中单击图像时,特定图像应显示在另一个活动中。我怎么能这样做?
这是我的Activity类::
public class ProductsActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_view);
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new MyAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
// this 'mActivity' parameter is Activity object, you can send the current activity.
Intent i = new Intent(getApplicationContext(), MobileDisplayActivity.class);
i.putExtra("positionId",position);
startActivity(i);
}
});
}
如何更改我的适配器类以实现我的要求。我想问题出在getView()
。
this is my adapter class:
private class MyAdapter extends BaseAdapter
{
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public MyAdapter(Context context)
{
inflater = LayoutInflater.from(context);
items.add(new Item("Image 1", R.drawable.ic_image4));
items.add(new Item("Image 2", R.drawable.ic_image5));
items.add(new Item("Image 3", R.drawable.ic_image4));
items.add(new Item("Image 4", R.drawable.ic_image5));
items.add(new Item("Image 5", R.drawable.ic_image4));
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i)
{
return items.get(i);
}
@Override
public long getItemId(int i)
{
return items.get(i).drawableId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View v = view;
ImageView picture;
TextView name;
if(v == null)
{
v = inflater.inflate(R.layout.row_grid, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView)v.getTag(R.id.picture);
name = (TextView)v.getTag(R.id.text);
Item item = (Item)getItem(i);
picture.setImageResource(item.drawableId);
name.setText(item.name);
return v;
}
private class Item
{
final String name;
final int drawableId;
Item(String name, int drawableId)
{
this.name = name;
this.drawableId = drawableId;
}
}
}
}
答案 0 :(得分:1)
你应该在意图中输入drawable id,如下面的代码所示:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
int drawableId = ((Item) parent.getItemAtPosition(position)).drawableId;
Intent intent = new Intent(ProductsActivity.this, MobileDisplayActivity.class);
intent.putExtra("DRAWABLE_ID", drawableId);
startActivity(intent);
}
此外,您应该将Item类设为public static,以便从activity中访问它。 drawableId应该是公共的(或者你可以为这个字段创建一个getter。)
答案 1 :(得分:1)
您可以发送ID或您的drawable(也是整数),而不是将项目的位置发送到下一个活动。
这样做
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
// this 'mActivity' parameter is Activity object, you can send the current activity.
int imageId = ((Item) parent.getItemAtPosition(position)).drawableId;
Intent i = new Intent(getApplicationContext(), MobileDisplayActivity.class);
i.putExtra("imageId", imageId);
startActivity(i);
}
在MobileDisplayActivity
活动中,简单地执行此操作
int imageId = getIntent().getIntExtra("imageId", 0);
if(imageId > 0)
imageView.setBackgroundResource(imageId);
else
// something went wrong
注意:将Item
类设为public static或单独定义。