我已经实施了一个小型购物车项目,有一个带按钮的产品列表。当我点击" ADD"按钮它将改变按钮名称" ADD"去除"删除"和颜色也改变绿色到红色。这里我的问题是,如果我重新访问产品表单,点击按钮应该是"删除"和红色。那么如何将点击的按钮位置添加到会话中。
我的代码:
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
final ViewHolderGrid holderForGrid;
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.consumer_catalog_list_item, null);
holderForGrid = new ViewHolderGrid(convertView);
convertView.setTag(holderForGrid);
} else {
holderForGrid = (ViewHolderGrid) convertView.getTag();
}
final BusinessCatalogVariables Catalog = catalogList.get(position);
holderForGrid.AddtoCart.setClickable(false);
holderForGrid.AddtoCart.setTag(position);
if (catalogList.get(position).isAdded()) {
holderForGrid.AddtoCart.setText("Remove");
holderForGrid.AddtoCart.setBackgroundResource(R.drawable.btnred);
} else {
holderForGrid.AddtoCart.setText("Add to Cart");
holderForGrid.AddtoCart.setBackgroundResource(R.drawable.buttonsignup);
}
if (Pref_Storage.checkDetail(context, Items)) {
holderForGrid.AddtoCart.setText("Remove");
holderForGrid.AddtoCart.setBackgroundResource(R.drawable.btnred);
Toast.makeText(context, Items, Toast.LENGTH_SHORT).show();
} else {
holderForGrid.AddtoCart.setText("Add to Cart");
holderForGrid.AddtoCart.setBackgroundResource(R.drawable.buttonsignup);
}
holderForGrid.AddtoCart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Position = (Integer) v.getTag();
Log.d("ADD BUTTON POSITION:", "position=" + position);
if (holderForGrid.AddtoCart.getText().equals("Add to Cart")) {
catalogList.get(position).setAdded(true);
holderForGrid.AddtoCart.setText("Remove");
holderForGrid.AddtoCart.setBackgroundResource(R.drawable.btnred);
Items = String.valueOf(Position);
Pref_Storage.setDetail(context, Items, "added");
//
} else if (holderForGrid.AddtoCart.getText().equals("Remove")) {
catalogList.get(position).setAdded(false);
holderForGrid.AddtoCart.setBackgroundResource(R.drawable.buttonsignup);
holderForGrid.AddtoCart.setText("Add to Cart");
Items = String.valueOf(Position);
Pref_Storage.deleteKey(context,Items);
}
}
});
return convertView;
}
答案 0 :(得分:0)
你必须使用setTag(),getTag()方法,
Class Pref_Storage.java [用于存储产品ID]
public class Pref_Storage {
private static SharedPreferences sharedPreferences = null;
public static void openPref(Context context) {
sharedPreferences = context.getSharedPreferences(context.getResources().getString(R.string.app_name),
Context.MODE_PRIVATE);
}
public static void deleteKey(Context context, String key) {
HashMap<String, String> result = new HashMap<String, String>();
Pref_Storage.openPref(context);
for (Entry<String, ?> entry : Pref_Storage.sharedPreferences.getAll()
.entrySet()) {
result.put(entry.getKey(), (String) entry.getValue());
}
boolean b = result.containsKey(key);
if (b) {
Pref_Storage.openPref(context);
Editor prefsPrivateEditor = Pref_Storage.sharedPreferences.edit();
prefsPrivateEditor.remove(key);
prefsPrivateEditor.commit();
prefsPrivateEditor = null;
Pref_Storage.sharedPreferences = null;
}
}
public static void setDetail(Context context, String key, String value) {
Pref_Storage.openPref(context);
Editor prefsPrivateEditor = Pref_Storage.sharedPreferences.edit();
prefsPrivateEditor.putString(key, value);
prefsPrivateEditor.commit();
prefsPrivateEditor = null;
Pref_Storage.sharedPreferences = null;
}
public static Boolean checkDetail(Context context, String key) {
HashMap<String, String> result = new HashMap<String, String>();
Pref_Storage.openPref(context);
for (Entry<String, ?> entry : Pref_Storage.sharedPreferences.getAll()
.entrySet()) {
result.put(entry.getKey(), (String) entry.getValue());
}
boolean b = result.containsKey(key);
return b;
}
}
在Adapter Class中你必须检查:
if(Pref_Storage.checkDetail(context, pid)){
holderForGrid.AddtoCart.setText("Remove");
}
else{
holderForGrid.AddtoCart.setText("Add to Cart");
}
holderForGrid.AddtoCart.setTag(position);
holderForGrid.AddtoCart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int pos = (int) v.getTag();
Button btn=(Button)v;
if (btn.getText().equals("Add to Cart")) {
catalogList.get(position).setAdded(true);
btn.setText("Remove");
btn.setBackgroundResource(R.drawable.btnred);
Pref_Storage.setDetail(context, pid, "added");
} else if (btn.getText().equals("Remove")) {
catalogList.get(position).setAdded(false);
btn.setBackgroundResource(R.drawable.buttonsignup);
btn.setText("Add to Cart");
Pref_Storage.deleteKey(context,pid);
}
}
});
答案 1 :(得分:0)
试试这个。
if (catalogList.get(position).isAdded()) {
holderForGrid.AddtoCart.setText("Remove");
holderForGrid.AddtoCart.setBackgroundResource(R.drawable.btnred);
} else {
holderForGrid.AddtoCart.setText("Add to Cart");
holderForGrid.AddtoCart.setBackgroundResource(R.drawable.buttonsignup);
}