2016-04-28 48 views
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; 
    } 
} 

當我開始活動它會是這樣的:

enter image description here

當我點擊按鈕1:

enter image description here

但是,當我離開那個actitvity回來這一個按鈕將是我以前改變了顏色都是綠色的,而不是保存的...紅。

謝謝。

回答

0

嘗試這些: 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(); 
     } 
    }); 
  • 修改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); 
    } 
    
  • 如果不能點擊的項目儘量設置一些屬性,以您的按鈕包含在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"/> 
    
  • +0

    是什麼mAdapter? – pmpc2

    +0

    它不是讓我在setonItemClickListener中設置適配器 – pmpc2

    +0

    Sorroy,mAdapter是適配器。 –