I am making an app in which I have a recycler view. The data is coming from the service and setting up trough an adapter. I have a check box and a cross image in the item layout. I need to change the color of the particular item background to green when clicking on the check box and to red when clicking on the cross image. I need both the color to be there until I make the final submit.
I am sharing my code as below :
The activity class :
package com.xantatech.bazaar.sourcingteam;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.xantatech.bazaar.R;
import com.xantatech.bazaar.retrofit.RetrofitHelper;
import com.xantatech.bazaar.sourcingteam.adapter.OrderAdapter;
import com.xantatech.bazaar.sourcingteam.model.OrderResponse;
import com.xantatech.bazaar.sourcingteam.model.OrderResponseData;
import com.xantatech.bazaar.sourcingteam.model.SourcingOrderListStatusResponse;
import com.xantatech.bazaar.utility.DialogHelper;
import com.xantatech.bazaar.utility.NetworkUtil;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class OrderActivity extends AppCompatActivity implements Callback<OrderResponse>{
RecyclerView order_recycler;
String orderId;
OrderAdapter orderAdapter;
// ProgressDialog pd;
TextView order_no;
ArrayList<OrderResponseData> orderResponseDatas = new ArrayList<>();
public Button submitSourcingOrder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
orderId = getIntent().getStringExtra("orderId");
order_recycler = (RecyclerView)findViewById(R.id.order_recycler);
order_no = (TextView) findViewById(R.id.order_no);
submitSourcingOrder = (Button) findViewById(R.id.submitSourcingOrder);
order_recycler.setLayoutManager(new GridLayoutManager(this,1));
order_recycler.setHasFixedSize(true);
// pd = new ProgressDialog(this);
// pd.setMessage("Please Wait ...");
// pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// pd.setCancelable(false);
// pd.setProgress(0);
// pd.setMax(100);
// pd.show();
submitSourcingOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(OrderAdapter.arrayList.size() == OrderAdapter.orderListSize){
if(NetworkUtil.checkNetworkStatus(OrderActivity.this)){
DialogHelper.getInstance().dialogShow(OrderActivity.this);
RetrofitHelper.getInstance().sourcingSubmitOrder(sourcingSubmitOrderCallback, "1",orderId, "2");
}else {
Toast.makeText(OrderActivity.this, R.string.internet_connection, Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(OrderActivity.this, "Complete all the orders first", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onResponse(Call<OrderResponse> call, Response<OrderResponse> response) {
DialogHelper.getInstance().dialogDismiss();
if(response.isSuccessful()){
if(response.body().getSuccess().equalsIgnoreCase("1")){
// pd.dismiss();
orderResponseDatas.addAll(response.body().getOrderList());
order_no.setText(""+orderResponseDatas.size());
orderAdapter = new OrderAdapter(OrderActivity.this, orderResponseDatas);
order_recycler.setAdapter(orderAdapter);
}else {
// pd.dismiss();
Toast.makeText(this, "No data found", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(Call<OrderResponse> call, Throwable t) {
DialogHelper.getInstance().dialogDismiss();
Toast.makeText(OrderActivity.this, R.string.something_wrong_happened, Toast.LENGTH_SHORT).show();
// pd.dismiss();
}
Callback<SourcingOrderListStatusResponse> sourcingSubmitOrderCallback = new Callback<SourcingOrderListStatusResponse>() {
@Override
public void onResponse(Call<SourcingOrderListStatusResponse> call, Response<SourcingOrderListStatusResponse> response) {
DialogHelper.getInstance().dialogDismiss();
if(response.isSuccessful()){
if(response.body().getSuccess().equalsIgnoreCase("1")){
OrderAdapter.arrayList.clear();
OrderAdapter.orderListSize = 0;
Toast.makeText(OrderActivity.this, ""+response.body().getOrderList(), Toast.LENGTH_SHORT).show();
finish();
}else {
Toast.makeText(OrderActivity.this, R.string.something_wrong_happened, Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(Call<SourcingOrderListStatusResponse> call, Throwable t) {
DialogHelper.getInstance().dialogDismiss();
Toast.makeText(OrderActivity.this, R.string.something_wrong_happened, Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onResume() {
super.onResume();
if(NetworkUtil.checkNetworkStatus(OrderActivity.this)){
DialogHelper.getInstance().dialogShow(OrderActivity.this);
RetrofitHelper.getInstance().sourcingTeamOrder(OrderActivity.this, "1",orderId);
}else {
Toast.makeText(OrderActivity.this, R.string.internet_connection, Toast.LENGTH_SHORT).show();
}
}
}
The Adapter Class :
package com.xantatech.bazaar.sourcingteam.adapter;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.xantatech.bazaar.R;
import com.xantatech.bazaar.retrofit.RetrofitHelper;
import com.xantatech.bazaar.sourcingteam.model.OrderResponseData;
import com.xantatech.bazaar.sourcingteam.model.SourcingOrderListStatusResponse;
import com.xantatech.bazaar.utility.DialogHelper;
import com.xantatech.bazaar.utility.NetworkUtil;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Created by new on 12/10/17.
*/
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.MyViewHolder> implements Callback<SourcingOrderListStatusResponse>{
Context context;
ArrayList<OrderResponseData> orderResponseDatas = new ArrayList<>();
// private boolean onBind;
public static ArrayList<String> arrayList = new ArrayList<>();
public static int orderListSize = 0;
public int row_index = -1;
String type = "";
public OrderAdapter(Context context, ArrayList<OrderResponseData> orderResponseDatas) {
this.context = context;
this.orderResponseDatas = orderResponseDatas;
}
@Override
public OrderAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.recycler_order, parent, false);
return new OrderAdapter.MyViewHolder(view);
}
@Override
public void onBindViewHolder(final OrderAdapter.MyViewHolder holder, final int position) {
final OrderResponseData orderResponseData=orderResponseDatas.get(position);
holder.order_brand.setText(orderResponseDatas.get(position).getBrand());
holder.order_pname.setText(orderResponseDatas.get(position).getProductName());
holder.order_size.setText(orderResponseDatas.get(position).getSize());
holder.order_quantity.setText(orderResponseDatas.get(position).getQuantity());
holder.check.setOnCheckedChangeListener(null);
holder.check.setChecked(orderResponseData.isChecked());
holder.check.setEnabled(!orderResponseData.isEnabled());
holder.imgCross.setEnabled(!orderResponseData.isCrossEnabled());
if(type.equalsIgnoreCase("check")){
holder.itemView.setBackgroundColor(orderResponseData.isBackgroundSelected() ? Color.GREEN : Color.WHITE);
}else if(type.equalsIgnoreCase("cross")){
holder.itemView.setBackgroundColor(orderResponseData.isBackgroundSelected() ? Color.RED : Color.WHITE);
}
holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
orderResponseData.setChecked(isChecked);
orderResponseData.setEnabled(isChecked);
orderResponseData.setCrossEnabled(isChecked);
row_index = position;
type = "check";
orderResponseData.setBackgroundSelected(!orderResponseData.isBackgroundSelected());
holder.itemView.setBackgroundColor(orderResponseData.isBackgroundSelected() ? Color.GREEN : Color.WHITE);
notifyDataSetChanged();
if(NetworkUtil.checkNetworkStatus(context)){
// DialogHelper.getInstance().dialogShow(context);
// RetrofitHelper.getInstance().sourcingOrderListStatus(OrderAdapter.this, "1", orderResponseDatas.get(position).getId(), "1");
}else {
Toast.makeText(context, R.string.internet_connection, Toast.LENGTH_SHORT).show();
}
}
});
holder.imgCross.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
row_index = position;
type = "cross";
orderResponseData.setEnabled(true);
orderResponseData.setCrossEnabled(true);
orderResponseData.setBackgroundSelected(!orderResponseData.isBackgroundSelected());
holder.itemView.setBackgroundColor(orderResponseData.isBackgroundSelected() ? Color.RED : Color.WHITE);
notifyDataSetChanged();
if(NetworkUtil.checkNetworkStatus(context)){
//DialogHelper.getInstance().dialogShow(context);
//RetrofitHelper.getInstance().sourcingOrderListStatus(OrderAdapter.this, "1", orderResponseDatas.get(position).getId(), "3");
}else {
Toast.makeText(context, R.string.internet_connection, Toast.LENGTH_SHORT).show();
}
}
});
// if(row_index == position) {
// if(type.equalsIgnoreCase("check")) {
// holder.itemView.setBackgroundColor(Color.parseColor("#008000"));
// }else if(type.equalsIgnoreCase("cross")){
// holder.itemView.setBackgroundColor(Color.parseColor("#FF0000"));
// }
// } else {
// holder.itemView.setBackgroundColor(Color.parseColor("#FFFFFF"));
// }
// holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(CompoundButton compoundButton, final boolean checked) {
// if(checked){
// holder.check.setChecked(false);
// // Code to display your message.
//
// AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
// builder1.setMessage("CheckBox Clicked");
// builder1.setCancelable(true);
//
// builder1.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialogInterface, int i) {
// dialogInterface.cancel();
//// holder.check.setChecked(true);
// holder.itemView.setBackgroundColor(Color.parseColor("#008000"));
// if(NetworkUtil.checkNetworkStatus(context)){
//// DialogHelper.getInstance().dialogShow(context);
//// RetrofitHelper.getInstance().sourcingOrderListStatus(OrderAdapter.this, "1", orderResponseDatas.get(position).getId(), "1");
// }else {
// Toast.makeText(context, R.string.internet_connection, Toast.LENGTH_SHORT).show();
// }
// }
// });
//
// builder1.setNegativeButton("No", new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int id) {
// dialog.cancel();
// holder.check.setChecked(false);
// }
// });
//
// AlertDialog alert11 = builder1.create();
// alert11.show();
//
// }
//
// }
// });
// holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
// if(!onBind){
//// holder.itemView.setBackgroundColor(Color.parseColor("#008000"));
// holder.check.setChecked(false);
// holder.check.setEnabled(true);
// row_index = position;
// if(NetworkUtil.checkNetworkStatus(context)){
// DialogHelper.getInstance().dialogShow(context);
// RetrofitHelper.getInstance().sourcingOrderListStatus(OrderAdapter.this, "1", orderResponseDatas.get(position).getId(), "1");
//
// updateItem(orderResponseData);
// orderResponseDatas.remove(position);
// notifyItemRemoved(position);
// Log.d("position", "*******"+position);
// notifyDataSetChanged();
// }else {
// Toast.makeText(context, R.string.internet_connection, Toast.LENGTH_SHORT).show();
// }
// }
// }
// });
// holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
// if(checked){
//// holder.itemView.setBackgroundColor(Color.parseColor("#008000"));
// row_index = position;
//// holder.check.setChecked(true);
//// holder.check.setEnabled(false);
// notifyDataSetChanged();
// if(NetworkUtil.checkNetworkStatus(context)){
//// DialogHelper.getInstance().dialogShow(context);
//// RetrofitHelper.getInstance().sourcingOrderListStatus(OrderAdapter.this, "1", orderResponseDatas.get(position).getId(), "1");
// }else {
// Toast.makeText(context, R.string.internet_connection, Toast.LENGTH_SHORT).show();
// }
// }
// }
// });
// holder.imgCross.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// holder.itemView.setBackgroundColor(Color.parseColor("#FF0000"));
// holder.check.setEnabled(false);
// if(NetworkUtil.checkNetworkStatus(context)){
// DialogHelper.getInstance().dialogShow(context);
// RetrofitHelper.getInstance().sourcingOrderListStatus(OrderAdapter.this, "1", orderResponseDatas.get(position).getId(), "3");
// }else {
// Toast.makeText(context, R.string.internet_connection, Toast.LENGTH_SHORT).show();
// }
// }
// });
// if(orderResponseData.islast()){
// holder.itemView.setBackgroundColor(Color.parseColor("#008000"));
// onBind = true;
// holder.check.setChecked(true);
// holder.check.setEnabled(false);
// onBind = false;
// }
}
@Override
public int getItemCount() {
Log.d("orderSize",""+orderResponseDatas.size());
orderListSize = orderResponseDatas.size();
return orderResponseDatas.size();
}
@Override
public void onResponse(Call<SourcingOrderListStatusResponse> call, Response<SourcingOrderListStatusResponse> response) {
DialogHelper.getInstance().dialogDismiss();
if(response.isSuccessful()){
if(response.body().getSuccess().equalsIgnoreCase("1")){
Toast.makeText(context, ""+response.body().getOrderList(), Toast.LENGTH_SHORT).show();
arrayList.add("1");
}else {
Toast.makeText(context, R.string.something_wrong_happened, Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(Call<SourcingOrderListStatusResponse> call, Throwable t) {
DialogHelper.getInstance().dialogDismiss();
Toast.makeText(context, R.string.something_wrong_happened, Toast.LENGTH_SHORT).show();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView order_brand,order_pname,order_size,order_quantity;
CheckBox check;
ImageView imgCross;
// LinearLayout linearLayout1;
public MyViewHolder(View itemView) {
super(itemView);
order_brand = (TextView)itemView.findViewById(R.id.order_brand);
order_pname = (TextView)itemView.findViewById(R.id.order_pname);
order_size = (TextView)itemView.findViewById(R.id.order_size);
order_quantity = (TextView)itemView.findViewById(R.id.order_quantity);
check = (CheckBox) itemView.findViewById(R.id.check);
imgCross = (ImageView) itemView.findViewById(R.id.imgCross);
// linearLayout1 = (LinearLayout) itemView.findViewById(R.id.linearLayout1);
}
}
// public void updateItem(OrderResponseData orderResponseData){
// ArrayList<OrderResponseData> arrTeamResponseData=new ArrayList<>();
// arrTeamResponseData.add(orderResponseData);
// orderResponseDatas.addAll(arrTeamResponseData);
// orderResponseData=orderResponseDatas.get(orderResponseDatas.size()-1);
// orderResponseData.setIslast(true);
// arrayList.add(orderResponseData);
// Log.d("size", "************"+arrayList.size());
// notifyDataSetChanged();
//
// }
}
The Model class :
package com.xantatech.bazaar.sourcingteam.model;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by new on 12/10/17.
*/
public class OrderResponseData {
@SerializedName("id")
@Expose
private String id;
@SerializedName("brand")
@Expose
private String brand;
@SerializedName("product_name")
@Expose
private String productName;
@SerializedName("size")
@Expose
private String size;
@SerializedName("quantity")
@Expose
private String quantity;
@SerializedName("status")
@Expose
private String status;
private boolean islast;
private boolean isEnabled;
private boolean isChecked;
private boolean isCrossEnabled;
private boolean isBackgroundSelected;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getQuantity() {
return quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public boolean islast() {
return islast;
}
public void setIslast(boolean islast) {
this.islast = islast;
}
public void setEnabled(boolean enabled) {
isEnabled = enabled;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
public boolean isEnabled() {
return isEnabled;
}
public boolean isChecked() {
return isChecked;
}
public boolean isCrossEnabled() {
return isCrossEnabled;
}
public void setCrossEnabled(boolean crossEnabled) {
isCrossEnabled = crossEnabled;
}
public boolean isBackgroundSelected() {
return isBackgroundSelected;
}
public void setBackgroundSelected(boolean backgroundSelected) {
isBackgroundSelected = backgroundSelected;
}
}`
`
The Item layout.xml :
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_5sdp"
android:background="@color/colorWhite"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background = "@drawable/cell_shape"
android:orientation="vertical">
<CheckBox
android:id = "@+id/check"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_weight = "1.5"
android:gravity="center_horizontal"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgCross"
android:src="@android:drawable/ic_delete"
/>
</LinearLayout>
<TextView
android:id = "@+id/order_brand"
android:layout_width = "0dp"
android:layout_height = "match_parent"
android:layout_weight = "1.5"
android:background = "@drawable/cell_shape"
android:ellipsize = "end"
android:gravity="center"
android:padding = "@dimen/_7sdp"
android:text = "Brand"
android:singleLine = "true" />
<TextView
android:id = "@+id/order_pname"
android:layout_width = "0dp"
android:layout_height = "match_parent"
android:layout_weight = "2"
android:gravity="center"
android:background = "@drawable/cell_shape"
android:ellipsize = "end"
android:padding = "@dimen/_7sdp"
android:text = "Product"
android:singleLine = "true" />
<TextView
android:id = "@+id/order_size"
android:layout_width = "0dp"
android:layout_height = "match_parent"
android:layout_weight = "1.5"
android:background = "@drawable/cell_shape"
android:ellipsize = "end"
android:gravity="center"
android:padding = "@dimen/_7sdp"
android:text = "Size"
android:singleLine = "true" />
<TextView
android:id = "@+id/order_quantity"
android:layout_width = "0dp"
android:layout_height = "match_parent"
android:layout_weight = "2"
android:gravity="center"
android:background = "@drawable/cell_shape"
android:ellipsize = "end"
android:padding = "@dimen/_7sdp"
android:text = "Quantity"
android:singleLine = "true" />
<TextView
android:id = "@+id/order_mrp"
android:layout_width = "0dp"
android:layout_height = "match_parent"
android:layout_weight = "2"
android:gravity="center"
android:background = "@drawable/cell_shape"
android:ellipsize = "end"
android:padding = "@dimen/_7sdp"
android:text = "MRP"
android:singleLine = "true" />
</LinearLayout>