こんにちは、私は電子商取引の食品アプリに取り組んでいます。アプリのセクションでは、お気に入りリストを作成して、ユーザーがリストビューで自分の好きな料理を好きにし、好きな食べ物をお気に入りのリストに追加したいと思っていました。今、製品リストアダプタクラスのイメージリソースを設定することで、イメージボタンの状態を変更できます。しかし、私は、セッションが閉じられたり、再オープンされた後に状態を維持することはできません。保存された設定など、いくつかのデータ保存の仕組みが必要だったと思いますか?だから私の質問は次のとおりです。
以下
アダプタクラスの画像ボタンの状態を保存するためにどのように私のコードの一部と私は私の食料別名製品
を保存するための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;
}
時間を費やして無駄に私の状況の解決策を見つけることができませんでした。どんな助けでも本当にありがとう!
使用SQLiteデータベース –
かわりに、データベースを使用しての恒久的状態を保存するためにSharedPreferencesを使用することができます。 – Shruti
あなたは製品を格納するためにsqliteデータベースを使用していますか? – USKMobility