2017-02-04 51 views
1

背景:保存的ImageButton狀態適配器類別

嗨,我工作的一個電子商務應用的食物。對於應用程序的一部分,我想創建一個收藏夾列表,用戶可以在列表視圖中選擇最喜歡的食物,並將收藏的食物添加到收藏夾列表中。現在,我可以通過在我的產品列表適配器類中設置圖像資源來更改圖像按鈕狀態。但是,在會議結束或重新開放後,我無法保持該狀態。我認爲它需要一些數據保存機制,如保存的偏好?所以我的問題是:

如何圖像按鈕的狀態保存在一個適配器類

下面是我的代碼部分順便說一句,我使用SQLite來存儲我的菜又名產品

ListProductListAdapter類別:

public class ListProductListAdapter extends BaseAdapter { 
private Context context; 
private int layout; 
private ArrayList<Product> productList2; 

@Override 
public int getCount() { 
    return productList2.size(); 
} 

@Override 
public Object getItem(int position) { 
    return productList2.get(position); 
} 

public ListProductListAdapter(Context context, int layout, ArrayList<Product> productList) { 
    this.context = context; 
    this.layout = layout; 
    this.productList2 = productList; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

private class ViewHolder2 { 
    ImageView imageView; 
    TextView textName, textStall, textPrice; 
    Button addCartButton; 
    ImageButton favBtn; 
} 

@Override 
public View getView(final int position, View view, ViewGroup viewGroup) { 

    View row = view; 
    ListProductListAdapter.ViewHolder2 holder = new ListProductListAdapter.ViewHolder2(); 

    if (row == null) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     row = inflater.inflate(layout, null); 
     holder.imageView = (ImageView) row.findViewById(R.id.listFoodImage); 
     holder.textName = (TextView) row.findViewById(R.id.listProductName); 
     holder.textStall = (TextView) row.findViewById(R.id.listProductStall); 
     holder.textPrice = (TextView) row.findViewById(R.id.listProductPrice); 
     holder.addCartButton = (Button) row.findViewById(R.id.addToCartButton); 
     holder.favBtn = (ImageButton)row.findViewById(R.id.favouriteButton); 


     row.setTag(holder); 
    } else { 
     holder = (ListProductListAdapter.ViewHolder2) row.getTag(); 
    } 

    Product product = productList2.get(position); 

    holder.textName.setText(product.getProductName()); 
    holder.textStall.setText(product.getProductStall()); 
    holder.textPrice.setText(product.getProductPrice()); 

    byte[] productImage = product.getProductImage(); 
    Bitmap bitmap = BitmapFactory.decodeByteArray(productImage, 0, productImage.length); 
    holder.imageView.setImageBitmap(bitmap); 

    // favorite button 
    holder.favBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View favBtn) { 
      favBtn.setSelected(!favBtn.isSelected()); 
      if(favBtn.isSelected()){ 
       ((ImageButton)favBtn).setImageResource(R.drawable.heart_red); 
       Toast.makeText(context, "Added to Favorites", Toast.LENGTH_SHORT).show(); 
      }else{ 
       ((ImageButton)favBtn).setImageResource(R.drawable.heart_grey); 
       Toast.makeText(context, "Removed from Favorites", Toast.LENGTH_SHORT).show(); 
      } 

     } 
    }); 

    // cart button 
    holder.addCartButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent i = new Intent(context, AdministratorActivity.class); 
      context.startActivity(i); 
     } 
    }); 
    holder.favBtn.setTag(productList2.get(position)); 
    return row; 

} 

ListActivity類:(與標準的Android drawe r活動)

public class ListActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

GridView gridView; 
ArrayList<Product>list; 
ListProductListAdapter adapter = null; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_list); 
    gridView = (GridView) findViewById(R.id.gridView); 
    list = new ArrayList<>(); 
    adapter = new ListProductListAdapter(this, R.layout.list_product_item, list); 
    gridView.setAdapter(adapter); 


    //get data from sqlite 
    Cursor cursor = LoginActivity.sqLiteHelper.getData("select * from product"); 
    list.clear(); 
    while (cursor.moveToNext()){ 
     int id = cursor.getInt(0); 
     byte[] image = cursor.getBlob(1); 
     String name = cursor.getString(2); 
     String stall = cursor.getString(3); 
     String price = cursor.getString(4); 
     list.add(new Product(image,id,name,stall,"S$ "+price)); 
    } 
    adapter.notifyDataSetChanged(); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
} 

@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.START)) { 
     drawer.closeDrawer(GravityCompat.START); 
    } else { 
     super.onBackPressed(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.list, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_cart) { 
     // Handle the camera action 
    } else if (id == R.id.nav_favourites) { 

    } else if (id == R.id.nav_trackOrder) { 

    } else if (id == R.id.nav_share) { 

    } else if (id == R.id.nav_about) { 

    } else if (id == R.id.nav_logOut) { 

    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 

我花了幾個小時的研究,無濟於事無法找到我的情況的解決方案。任何幫助將非常感激!

+0

使用SQLite數據庫 –

+0

您可以使用SharedPreferences保存狀態,而不是永久使用的數據庫。 – Shruti

+0

你使用sqlite數據庫來存儲產品? – USKMobility

回答

1

當你從sqlite數據庫中檢索你的數據時,對我來說最好的解決方案似乎在你的模型上添加一個列來存儲isFavorite。
如果用戶將食物標記爲收藏夾,則通過將isFavorite設置爲true來更新您的食物。
然後在您的適配器中,閱讀此字段並相應地更新圖像。

所以當用戶點擊你的按鈕時,只需在你的sqlite數據庫中設置isFavorite爲true即可。

然後在你的適配器,你將有這樣一個條件:

if(product.isFavorite()) { 
    holder.imageView.setImageBitmap(favoriteImage); 
} else { 
    holder.imageView.setImageBitmap(notFavImage); 
}