【问题标题】:Android save state of buttons in gridAndroid在网格中保存按钮的状态
【发布时间】:2016-04-28 22:45:58
【问题描述】:

我有两个问题:

1st)我如何选择网格的特定按钮,因为我实现它的方式总是选择第一个按钮,即使我点击另一个按钮。

2nd)在实现点“1”后,我想向用户显示可用的产品,这些产品将以绿色背景颜色显示。如果用户单击按钮,背景颜色将变为红色。问题是,如果我更改活动然后再次返回,颜色将是默认颜色,即绿色。我希望活动保存这些按钮的状态,以便在用户离开它们时重新加载。

下面是代码:

    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);

    }

//这里是改变背景颜色的按钮事件 public void goToBin(View 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 个按钮时:

但是当我离开那个活动并回到这个按钮时,按钮将全部变为绿色......而不是保存我之前将颜色更改为红色的按钮。

谢谢。

【问题讨论】:

    标签: android button grid savestate


    【解决方案1】:

    试试这些: 1.修改ShowList.java

    SharedPreferences pref_restore = this.getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE);
    int position = pref_restore.getInt("position", -1);
    
    adapter = new GridViewAdapter(this, productsGridView, position);
    
    view = (GridView) findViewById(R.id.gridView2);
    
    view.setAdapter(adapter);
    
    view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GridViewAdapter adapter = new GridViewAdapter(ShowList.this, productsGridView, position);
                view.setAdapter(adapter);
    
                //Save position to SharedPreferences;
                SharedPreferences pref_save = ShowList.this.getSharedPreferences("PREFERENCE", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = pref_save.edit();
                editor.putInt("position", position);
                editor.apply();
            }
        });
    
    1. 修改GridViewAdapter.java

    修改 GridViewAdapter 构造函数为

    public GridViewAdapter(Context context, ArrayList<Product> products, int mSelectedPosition) 
    

    并像这样在 getView 方法中修改正文:

    holder.productName = (Button) convertView.findViewById(R.id.textViewList1);
    if(position == mSelectedPosition){
        holder.productName.setBackgroundColor(0xffff0000);
    }
    
    1. 如果您无法单击项目,请尝试为您的 Button 设置一些属性,该属性包含在 listview_values.xml 中:

       <Button 
           android:id="@+id/textViewList1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:focusableInTouchMode="false"
           android:clickable="false"
           android:focusable="false"/>
      

    【讨论】:

    • 它不允许我在 setonItemClickListener 中执行 setAdapter
    • 抱歉,mAdapter 是适配器。
    • 你不能使用 view.setAdapter();因为你没有用 final 声明它。试试这个:最终的 GridView 视图;
    • 这不起作用,因为当我将某些东西声明为 final 时,我必须对其进行初始化,并且它的值不能被修改,并且当我在 @OnCreate 方法之前对其进行初始化时,它会给出一个错误,这使得它无法正常工作,因为我需要该变量为全局
    • 尝试在这里查看我的项目示例:mediafire.com/download/j5rv3775c7hbb5q/MyApplication.rar
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    相关资源
    最近更新 更多