背景:
嗨,我正在开发一个电子商务食品应用程序。对于应用程序的一部分,我想创建一个收藏夹列表,用户可以在列表视图中收藏他们选择的食物,并将收藏的食物添加到收藏列表中。现在,我可以通过在我的产品列表适配器类中设置图像资源来更改图像按钮状态。但是,在会话关闭或重新打开后,我无法维持状态。我估计它需要一些数据保存机制,比如保存的首选项?所以我的问题是:
如何在适配器类中保存图像按钮状态
以下是我的代码的一部分,以及我使用sqlite存储我的食物aka产品的方式
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抽屉活动)
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 :(得分:1)
当您从sqlite数据库中检索数据时,对我来说最好的解决方案似乎是在模型上添加一个存储isFavorite的列。
如果用户将食物标记为收藏,则通过将isFavorite设置为true来更新您的食物原料
然后在适配器中,读取此字段并相应地更新图像。
因此,当用户单击您的按钮时,只需在您的sqlite数据库中设置相关对象的isFavorite为true即可。
然后在您的适配器中,您将遇到类似这样的情况:
if(product.isFavorite()) {
holder.imageView.setImageBitmap(favoriteImage);
} else {
holder.imageView.setImageBitmap(notFavImage);
}