什么样的地狱实现 Parcelable ?
他正在传递给适配器 String[] 所以
- 在位置获取项目(字符串)
- 创建意图
- 把它当作额外的
- 开始活动
- 在活动中获得额外奖励
要存储产品列表,您可以在此处使用 HashMap
(例如作为 STATIC 对象)
描述产品的示例类:
public class Product {
private String _name;
private String _description;
private int _id
public Product(String name, String description,int id) {
_name = name;
_desctription = description;
_id = id;
}
public String getName() {
return _name;
}
public String getDescription() {
return _description;
}
}
Product dell = new Product("dell","this is dell",1);
HashMap<String,Product> _hashMap = new HashMap<>();
_hashMap.put(dell.getName(),dell);
然后你传递给适配器键集:
String[] productNames = _hashMap.keySet().toArray(new String[_hashMap.size()]);
当您在适配器中返回视图时,您可以像这样设置侦听器:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Context context = parent.getContext();
String itemName = getItem(position)
someView.setOnClikListener(new MyOnClickListener(context, itemName));
}
private class MyOnClickListener implements View.OnClickListener {
private String _itemName;
private Context _context
public MyOnClickListener(Context context, String itemName) {
_context = context;
_itemName = itemName;
}
@Override
public void onClick(View view) {
//------listener onClick example method body ------
Intent intent = new Intent(_context, SomeClassToHandleData.class);
intent.putExtra(key_to_product_name,_itemName);
_context.startActivity(intent);
}
}
然后在其他活动中:
@Override
public void onCreate(Bundle) {
String productName = getIntent().getExtra(key_to_product_name);
Product product = _hashMap.get(productName);
}
*key_to_product_name 是一个公共静态字符串,用作额外的密钥
ps。抱歉打错了,我很着急:)
ps2。这应该让你知道怎么做
ps3。当我有更多时间时,我会添加详细说明
我的评论:
- 请勿使用任何开关声明
- 不要为每个产品创建单独的活动(你只需要一个)