1
我有2个问题:Android的保存按钮的状态电网
1)如何选择电网的特定按钮,因为我实现的方式,它总是选择的第一个按钮,即使我点击另一个按钮。
2nd)执行poiint“1”后,我想向用户显示可用的产品,它将以绿色背景色显示。如果用户点击按钮,backgroundcolor将变为红色。问题是,如果我改变活动,然后再回来,颜色将是默认的颜色,即绿色。我希望活动保存这些按钮的状态以便在用户离开时重新加载。
下面是代码:
public class ShowList extends AppCompatActivity {
...(some methods that aren't relevant)
//Here i'm retrieving objects from a database table and adding them to a List which will add items to a Grid using GridViewAdapter (class shown below)
public void addItemsToGridListView(String tableName) {
productsGridView.clear();
Cursor res = db.getFromSpecificTAble(tableName);
while(res.moveToNext()) {
String productName = res.getString(0);
String productPrice = res.getString(1);
String buyType = res.getString(2);
String productQuantity = res.getString(3);
Product p = new Product(productName, productPrice);p.setBuyType(buyType);p.setProductQuantity(productQuantity);
productsGridView.add(p);
}
adapter = new GridViewAdapter(this,productsGridView);
view = (GridView) findViewById(R.id.gridView2);
view.setAdapter(adapter);
}
//这里是按钮事件改变的backgroundColor 公共无效goToBin(视图v){
Button b = (Button) findViewById(R.id.textViewList1);
b.setBackgroundColor(0xffff0000);
adapter.notifyDataSetChanged();
}
}
public class GridViewAdapter extends BaseAdapter {
private ArrayList<Product> products;
private Context context;
private LayoutInflater inflater;
public GridViewAdapter(Context context, ArrayList<Product> products) {
this.products = products;
this.context = context;
inflater = LayoutInflater.from(this.context); }
@Override
public int getCount() {
return products.size();
}
@Override
public Object getItem(int position) {
return products.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
Button productName;
TextView productPrice;
TextView buyType;
TextView productQuantity;
ImageButton removeProduct;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null || (convertView.getTag() == null)) {
convertView = inflater.inflate(R.layout.listview_values, parent, false);
holder = new ViewHolder();
holder.productName = (Button) convertView.findViewById(R.id.textViewList1);
holder.productPrice = (TextView) convertView.findViewById(R.id.EditTextList2);
holder.buyType = (TextView) convertView.findViewById(R.id.textViewList3);
holder.productQuantity = (TextView) convertView.findViewById(R.id.textViewList4);
holder.removeProduct = (ImageButton) convertView.findViewById(R.id.removeProduct);
}
else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setTag(holder);
holder.productName.setText(products.get(position).getProductName());
holder.productPrice.setText(String.valueOf(products.get(position).getProductPrice()) + " €");
holder.buyType.setText(products.get(position).getBuyType());
holder.productQuantity.setText(String.valueOf(products.get(position).getProductQuantity()) + " Un.");
holder.removeProduct.setTag(new Integer(position));
return convertView;
}
}
当我开始活动它会是这样的:
当我点击按钮1:
但是,当我离开那个actitvity回来这一个按钮将是我以前改变了颜色都是绿色的,而不是保存的...红。
谢谢。
是什么mAdapter? – pmpc2
它不是让我在setonItemClickListener中设置适配器 – pmpc2
Sorroy,mAdapter是适配器。 –