我的应用程序是在线市场。我可以将产品添加到购物车。我正在尝试从“添加到购物车”到“购物车”中的更改图标。但它无法正常工作。如果我只添加一个产品并向下滚动,则所有其他产品图标也会更改。在RecyclerView适配器OnBindViewHolder中,我使用条件if。
我以JSON格式从服务器获取数据。并在recyclerview中显示它。我可以添加prudact到购物车,为此我创建表“Busket”。当用户将产品添加到购物车时,prudact数据被添加到“Busket”。例如,用户关闭应用程序。再次回到app,他可以发现他在其他图标中添加的产品并没有在另一个图标中添加产品。阿妮帮忙。谢谢!
我的完整适配器。
public class PrudactAdapter extends RecyclerView.Adapter<PrudactAdapter.PrudactViewHolder> {
List<PrudactModel> prudactsList;
public Context mContext;
public static EditPlayerAdapterCallback callback;
ArrayList<PrudactBusketModel> busketList = new ArrayList<>();
DBHelper dbHelper;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public ArrayList<Integer> tableBasket = new ArrayList<Integer>();
public ArrayList<Integer> tableFavorite = new ArrayList<>();
String LOG_TAG = "myLogs";
public PrudactAdapter(List<PrudactModel> persons, Context context){
this.prudactsList = persons;
this.mContext = context;
}
@Override
public PrudactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_of_pruduct, parent, false);
PrudactViewHolder pvh = new PrudactViewHolder(v,mContext);
return pvh;
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public void onBindViewHolder(final PrudactViewHolder prudactViewHolder, final int i) {
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
final PrudactModel p = prudactsList.get(i);
prudactViewHolder.catalogName.setText(p.getName());
prudactViewHolder.catalogPrice.setText(Integer.toString(p.getPrice())+" тг");
prudactViewHolder.catalogPriceOld.setText(Integer.toString(p.getOldPrice())+" тг");
prudactViewHolder.thumbNail.setImageUrl(p.getImgId(),imageLoader);
// final MediaPlayer addSound = MediaPlayer.create(mContext,R.raw.button28);
//// TODO Cheking PRICE
if (p.getOldPrice()==0){
prudactViewHolder.catalogPriceOld.setVisibility(View.INVISIBLE);
prudactViewHolder.aksia.setVisibility(View.INVISIBLE);
prudactViewHolder.catalogPrice.setTextColor(ContextCompat.getColor(mContext, R.color.black));
}
else {
prudactViewHolder.catalogPrice.setTextColor(ContextCompat.getColor(mContext, R.color.red));
prudactViewHolder.catalogPriceOld.setVisibility(View.VISIBLE);
prudactViewHolder.aksia.setVisibility(View.VISIBLE);
}
//// TODO Cheking DB
if (getTableBasket().contains(p.getId()))
prudactViewHolder.btn_buy.setBackgroundResource(R.drawable.tool);
else
prudactViewHolder.btn_buy.setBackgroundResource(R.drawable.tool1);
if (getTableFavorite().contains(p.getId()))
prudactViewHolder.btn_favorite.setBackgroundResource(R.drawable.favorite_d);
else
prudactViewHolder.btn_favorite.setBackgroundResource(R.drawable.favorite_c);
//// TODO: 02.05.2016 BUY BUTTON CLICK
prudactViewHolder.ll_buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper = new DBHelper(mContext);
final ContentValues cv = new ContentValues();
final SQLiteDatabase db = dbHelper.getWritableDatabase();
if (prudactViewHolder.btn_buy.getBackground().getConstantState()== ContextCompat.getDrawable(mContext,R.drawable.tool1).getConstantState()){
updateBuyButton(prudactViewHolder, true);
Log.d(LOG_TAG, "--- Insert in mytable: ---");
cv.put("id_prudact", p.getId());
cv.put("name",p.getName());
cv.put("id_image",p.getImgId());
cv.put("count",1);
cv.put("price",p.getPrice());
cv.put("priceOld",p.getOldPrice());
long rowID = db.insert("basket", null, cv);
Log.d(LOG_TAG, "row to basket inserted, ID = " + rowID);
callback.folderClicked(1);
}
else {
updateBuyButton2(prudactViewHolder, true);
callback.folderClicked(-1);
db.delete("basket","id_prudact=?",new String[]{String.valueOf(p.getId())});
}
dbHelper.close();
}
});
prudactViewHolder.btn_buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper = new DBHelper(mContext);
final ContentValues cv = new ContentValues();
final SQLiteDatabase db = dbHelper.getWritableDatabase();
if (prudactViewHolder.btn_buy.getBackground().getConstantState()== ContextCompat.getDrawable(mContext,R.drawable.tool1).getConstantState()){
updateBuyButton(prudactViewHolder, true);
Log.d(LOG_TAG, "--- Insert in mytable: ---");
cv.put("id_prudact", p.getId());
cv.put("name",p.getName());
cv.put("id_image",p.getImgId());
cv.put("count",1);
cv.put("price",p.getPrice());
cv.put("priceOld",p.getOldPrice());
long rowID = db.insert("basket", null, cv);
Log.d(LOG_TAG, "row to basket inserted, ID = " + rowID);
callback.folderClicked(1);
}
else {
updateBuyButton2(prudactViewHolder, true);
callback.folderClicked(-1);
db.delete("basket","id_prudact=?",new String[]{String.valueOf(p.getId())});
}
dbHelper.close();
}
});
//// TODO: 02.05.2016 FAVORITE BUTTON CLICK
prudactViewHolder.ll_favorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper = new DBHelper(mContext);
final ContentValues cv = new ContentValues();
final SQLiteDatabase db = dbHelper.getWritableDatabase();
if (prudactViewHolder.btn_favorite.getBackground().getConstantState()== ContextCompat.getDrawable(mContext,R.drawable.favorite_c).getConstantState()){
updateHeartButton(prudactViewHolder, true);
cv.put("id_prudact", p.getId());
cv.put("name",p.getName());
cv.put("id_image",p.getImgId());
cv.put("count",1);
cv.put("price",p.getPrice());
cv.put("priceOld",p.getOldPrice());
long rowID = db.insert("favorite", null, cv);
Log.d(LOG_TAG, "row to favorite inserted, ID = " + rowID);
}
else {
updateHeartButton2(prudactViewHolder, true);
db.delete("favorite","id_prudact=?",new String[]{String.valueOf(p.getId())});
}
}
});
prudactViewHolder.btn_favorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper = new DBHelper(mContext);
final ContentValues cv = new ContentValues();
final SQLiteDatabase db = dbHelper.getWritableDatabase();
if (prudactViewHolder.btn_favorite.getBackground().getConstantState()== ContextCompat.getDrawable(mContext,R.drawable.favorite_c).getConstantState()){
updateHeartButton(prudactViewHolder, true);
cv.put("id_prudact", p.getId());
cv.put("name",p.getName());
cv.put("id_image",p.getImgId());
cv.put("count",1);
cv.put("price",p.getPrice());
cv.put("priceOld",p.getOldPrice());
long rowID = db.insert("favorite", null, cv);
Log.d(LOG_TAG, "row to favorite inserted, ID = " + rowID);
}
else {
updateHeartButton2(prudactViewHolder, true);
db.delete("favorite","id_prudact=?",new String[]{String.valueOf(p.getId())});
}
}
});
//// TODO: 02.05.2016 SHARE BUTTON CLICK
prudactViewHolder.ll_share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String shareBody = "Мне нравится "+p.getName()+" за "+p.getPrice()+" тенге. unimax.kz";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
mContext.startActivity(Intent.createChooser(sharingIntent,"Elaman" ));
}
});
prudactViewHolder.btn_share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String shareBody = "Мне нравится "+p.getName()+" за "+p.getPrice()+" тенге. unimax.kz";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
mContext.startActivity(Intent.createChooser(sharingIntent,"Elaman" ));
}
});
prudactViewHolder.thumbNail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, ImageActivity.class);
intent.putExtra("image",p.getImgId());
mContext.startActivity(intent);
}
});
dbHelper.close();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return prudactsList.size();
}
public void setCallback(EditPlayerAdapterCallback callback2){
callback = callback2;
}
public static class PrudactViewHolder extends RecyclerView.ViewHolder{
CardView cv;
TextView catalogName,catalogPrice,catalogPriceOld;
NetworkImageView thumbNail;
ImageButton btn_favorite,btn_share, btn_buy;
LinearLayout ll_buy,aksia,ll_share,ll_favorite;
public PrudactViewHolder(View itemView,Context context) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
catalogName = (TextView)itemView.findViewById(R.id.name_catalog);
catalogPrice = (TextView)itemView.findViewById(R.id.price_catalog);
catalogPriceOld = (TextView)itemView.findViewById(R.id.old_price_catalog);
btn_buy = (ImageButton) itemView.findViewById(R.id.btn_buy);
btn_share = (ImageButton) itemView.findViewById(R.id.btn_share);
btn_favorite = (ImageButton) itemView.findViewById(R.id.izbrannyi);
thumbNail = (NetworkImageView)itemView.findViewById(R.id.image_catalog);
aksia = (LinearLayout) itemView.findViewById(R.id.aksia);
ll_buy = (LinearLayout) itemView.findViewById(R.id.ll_buy);
ll_share = (LinearLayout) itemView.findViewById(R.id.ll_share);
ll_favorite = (LinearLayout) itemView.findViewById(R.id.ll_favorite);
catalogPriceOld.setPaintFlags(catalogPriceOld.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
}
}
ArrayList<Integer> getTableBasket(){
ArrayList<Integer> id_list = new ArrayList<>();
dbHelper = new DBHelper(mContext);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor c = db.query("basket", null, null, null, null, null, null);
if (c.moveToFirst()) {
int id_prudactColIndex = c.getColumnIndex("id_prudact");
do {
id_list.add(c.getInt(id_prudactColIndex));
} while (c.moveToNext());
} else
Log.d(LOG_TAG, "In basket 0 rows");
c.close();
return id_list;
}
ArrayList<Integer> getTableFavorite(){
ArrayList<Integer> id_list = new ArrayList<>();
dbHelper = new DBHelper(mContext);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor c = db.query("favorite", null, null, null, null, null, null);
if (c.moveToFirst()) {
int id_prudactColIndex = c.getColumnIndex("id_prudact");
do {
id_list.add(c.getInt(id_prudactColIndex));
} while (c.moveToNext());
} else
Log.d(LOG_TAG, "In favorite 0 rows");
c.close();
return id_list;
}
}
我的数据库结构
db.execSQL("create table basket ("
+ "id integer primary key autoincrement,"
+ "id_prudact integer,"
+ "name text,"
+ "id_image integer,"
+ "count integer,"
+ "price integer,"
+ "priceOld integer,"
+ "nomer_zakaz text" + ");");
db.execSQL("create table favorite ("
+ "id integer primary key autoincrement,"
+ "id_prudact integer,"
+ "name text,"
+ "id_image text,"
+ "count integer,"
+ "price integer,"
+ "priceOld integer" + ");");
我的更新代码
public class PrudactAdapter extends RecyclerView.Adapter<PrudactAdapter.PrudactViewHolder> {
List<PrudactModel> prudactsList;
public Context mContext;
public static EditPlayerAdapterCallback callback;
ArrayList<PrudactBusketModel> busketList = new ArrayList<>();
DBHelper dbHelper;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
ArrayList<Integer> tableBasket = new ArrayList<>();
ArrayList<Integer> tableFavorite = new ArrayList<>();
String LOG_TAG = "myLogs";
public PrudactAdapter(List<PrudactModel> persons, Context context){
this.prudactsList = persons;
this.mContext = context;
tableBasket = getTableBasket();
tableFavorite = getTableFavorite();
}
@Override
public PrudactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_of_pruduct, parent, false);
PrudactViewHolder pvh = new PrudactViewHolder(v,mContext);
return pvh;
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public void onBindViewHolder(final PrudactViewHolder prudactViewHolder, final int i) {
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
final PrudactModel p = prudactsList.get(i);
prudactViewHolder.catalogName.setText(p.getName());
prudactViewHolder.catalogPrice.setText(Integer.toString(p.getPrice())+" тг");
prudactViewHolder.catalogPriceOld.setText(Integer.toString(p.getOldPrice())+" тг");
prudactViewHolder.thumbNail.setImageUrl(p.getImgId(),imageLoader);
// final MediaPlayer addSound = MediaPlayer.create(mContext,R.raw.button28);
//// TODO Cheking PRICE
if (p.getOldPrice()==0){
prudactViewHolder.catalogPriceOld.setVisibility(View.INVISIBLE);
prudactViewHolder.aksia.setVisibility(View.INVISIBLE);
prudactViewHolder.catalogPrice.setTextColor(ContextCompat.getColor(mContext, R.color.black));
}
else {
prudactViewHolder.catalogPrice.setTextColor(ContextCompat.getColor(mContext, R.color.red));
prudactViewHolder.catalogPriceOld.setVisibility(View.VISIBLE);
prudactViewHolder.aksia.setVisibility(View.VISIBLE);
}
//// TODO Cheking DB
if (tableBasket.contains(p.getId())){
Log.d("pa","id "+ p.getId());
prudactViewHolder.btn_buy.setBackgroundResource(R.drawable.tool);}
else
prudactViewHolder.btn_buy.setBackgroundResource(R.drawable.tool1);
if (tableFavorite.contains(p.getId()))
prudactViewHolder.btn_favorite.setBackgroundResource(R.drawable.favorite_d);
else
prudactViewHolder.btn_favorite.setBackgroundResource(R.drawable.favorite_c);
//// TODO: 02.05.2016 BUY BUTTON CLICK
prudactViewHolder.ll_buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper = new DBHelper(mContext);
final ContentValues cv = new ContentValues();
final SQLiteDatabase db = dbHelper.getWritableDatabase();
if (prudactViewHolder.btn_buy.getBackground().getConstantState()== ContextCompat.getDrawable(mContext,R.drawable.tool1).getConstantState()){
updateBuyButton(prudactViewHolder, true);
Log.d(LOG_TAG, "--- Insert in mytable: ---");
cv.put("id_prudact", p.getId());
cv.put("name",p.getName());
cv.put("id_image",p.getImgId());
cv.put("count",1);
cv.put("price",p.getPrice());
cv.put("priceOld",p.getOldPrice());
long rowID = db.insert("basket", null, cv);
Log.d(LOG_TAG, "row to basket inserted, ID = " + rowID);
callback.folderClicked(1);
}
else {
updateBuyButton2(prudactViewHolder, true);
callback.folderClicked(-1);
db.delete("basket","id_prudact=?",new String[]{String.valueOf(p.getId())});
}
dbHelper.close();
}
});
prudactViewHolder.btn_buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper = new DBHelper(mContext);
final ContentValues cv = new ContentValues();
final SQLiteDatabase db = dbHelper.getWritableDatabase();
if (prudactViewHolder.btn_buy.getBackground().getConstantState()== ContextCompat.getDrawable(mContext,R.drawable.tool1).getConstantState()){
updateBuyButton(prudactViewHolder, true);
Log.d(LOG_TAG, "--- Insert in mytable: ---");
cv.put("id_prudact", p.getId());
cv.put("name",p.getName());
cv.put("id_image",p.getImgId());
cv.put("count",1);
cv.put("price",p.getPrice());
cv.put("priceOld",p.getOldPrice());
long rowID = db.insert("basket", null, cv);
Log.d(LOG_TAG, "row to basket inserted, ID = " + rowID);
callback.folderClicked(1);
}
else {
updateBuyButton2(prudactViewHolder, true);
callback.folderClicked(-1);
db.delete("basket","id_prudact=?",new String[]{String.valueOf(p.getId())});
}
dbHelper.close();
}
});
//// TODO: 02.05.2016 FAVORITE BUTTON CLICK
prudactViewHolder.ll_favorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper = new DBHelper(mContext);
final ContentValues cv = new ContentValues();
final SQLiteDatabase db = dbHelper.getWritableDatabase();
if (prudactViewHolder.btn_favorite.getBackground().getConstantState()== ContextCompat.getDrawable(mContext,R.drawable.favorite_c).getConstantState()){
updateHeartButton(prudactViewHolder, true);
cv.put("id_prudact", p.getId());
cv.put("name",p.getName());
cv.put("id_image",p.getImgId());
cv.put("count",1);
cv.put("price",p.getPrice());
cv.put("priceOld",p.getOldPrice());
long rowID = db.insert("favorite", null, cv);
Log.d(LOG_TAG, "row to favorite inserted, ID = " + rowID);
}
else {
updateHeartButton2(prudactViewHolder, true);
db.delete("favorite","id_prudact=?",new String[]{String.valueOf(p.getId())});
}
}
});
prudactViewHolder.btn_favorite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper = new DBHelper(mContext);
final ContentValues cv = new ContentValues();
final SQLiteDatabase db = dbHelper.getWritableDatabase();
if (prudactViewHolder.btn_favorite.getBackground().getConstantState()== ContextCompat.getDrawable(mContext,R.drawable.favorite_c).getConstantState()){
updateHeartButton(prudactViewHolder, true);
cv.put("id_prudact", p.getId());
cv.put("name",p.getName());
cv.put("id_image",p.getImgId());
cv.put("count",1);
cv.put("price",p.getPrice());
cv.put("priceOld",p.getOldPrice());
long rowID = db.insert("favorite", null, cv);
Log.d(LOG_TAG, "row to favorite inserted, ID = " + rowID);
}
else {
updateHeartButton2(prudactViewHolder, true);
db.delete("favorite","id_prudact=?",new String[]{String.valueOf(p.getId())});
}
}
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public int getItemCount() {
return prudactsList.size();
}
public void setCallback(EditPlayerAdapterCallback callback2){
callback = callback2;
}
public static class PrudactViewHolder extends RecyclerView.ViewHolder{
CardView cv;
TextView catalogName,catalogPrice,catalogPriceOld;
NetworkImageView thumbNail;
ImageButton btn_favorite,btn_share, btn_buy;
LinearLayout ll_buy,aksia,ll_share,ll_favorite;
public PrudactViewHolder(View itemView,Context context) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
catalogName = (TextView)itemView.findViewById(R.id.name_catalog);
catalogPrice = (TextView)itemView.findViewById(R.id.price_catalog);
catalogPriceOld = (TextView)itemView.findViewById(R.id.old_price_catalog);
btn_buy = (ImageButton) itemView.findViewById(R.id.btn_buy);
btn_share = (ImageButton) itemView.findViewById(R.id.btn_share);
btn_favorite = (ImageButton) itemView.findViewById(R.id.izbrannyi);
thumbNail = (NetworkImageView)itemView.findViewById(R.id.image_catalog);
aksia = (LinearLayout) itemView.findViewById(R.id.aksia);
ll_buy = (LinearLayout) itemView.findViewById(R.id.ll_buy);
ll_share = (LinearLayout) itemView.findViewById(R.id.ll_share);
ll_favorite = (LinearLayout) itemView.findViewById(R.id.ll_favorite);
catalogPriceOld.setPaintFlags(catalogPriceOld.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
}
}
ArrayList<Integer> getTableBasket(){
ArrayList<Integer> id_list = new ArrayList<>();
dbHelper = new DBHelper(mContext);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor c = db.query("basket", null, null, null, null, null, null);
if (c.moveToFirst()) {
int id_prudactColIndex = c.getColumnIndex("id_prudact");
do {
id_list.add(c.getInt(id_prudactColIndex));
} while (c.moveToNext());
} else
Log.d(LOG_TAG, "In basket 0 rows");
c.close();
return id_list;
}
ArrayList<Integer> getTableFavorite(){
ArrayList<Integer> id_list = new ArrayList<>();
dbHelper = new DBHelper(mContext);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor c = db.query("favorite", null, null, null, null, null, null);
if (c.moveToFirst()) {
int id_prudactColIndex = c.getColumnIndex("id_prudact");
do {
id_list.add(c.getInt(id_prudactColIndex));
} while (c.moveToNext());
} else
Log.d(LOG_TAG, "In favorite 0 rows");
c.close();
return id_list;
}
}