Below is part of my code in AsyncTask:
protected void onPostExecute(Void result){
super.onPostExecute(result);
if(pDialog.isShowing()){
pDialog.dismiss();
}
if(jsonStr != null){
try{
jsonObj = new JSONObject(jsonStr);
//Getting JSON Array Node
sales = jsonObj.getJSONArray("Result");
//looping through all results
for(int i = 0; i<sales.length();i++){
JSONObject s = sales.getJSONObject(i);
WarehouseSalesDetails wsd = new WarehouseSalesDetails();
wsd.expiry_date = s.getString("expiry_date");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date actual_date = sdf.parse(wsd.expiry_date);
if(new Date().before(actual_date)){
wsd.id = s.getInt("id");
id = wsd.id; //to pass down the value to onClick below;
wsd.company_name = s.getString("company_name");
wsd.promotion_image= s.getString("promotion_image");
wsd.title = s.getString("title");
wsd.promotional_period = s.getString("promotional_period");
data.add(wsd);
}
}
Log.d("TAG",sales_details.toString());
}catch(final JSONException e){
Log.e(TAG, "JSON parsing error: " + e.getMessage());
} catch (ParseException e) {
e.printStackTrace();
}
}else{
Log.e(TAG,"Couldn't get json from server");
}
//update RecyclerView
warehouse_recycler = (RecyclerView)((AppCompatActivity) context).findViewById(R.id.recyclerView);
mAdapter = new AdapterRecycler(context, data);
final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
warehouse_recycler.setLayoutManager(layoutManager);
warehouse_recycler.setAdapter(mAdapter);
warehouse_recycler.addOnItemTouchListener(
new RecyclerItemClickListener(context,warehouse_recycler,new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(context, "ID is " + id,
Toast.LENGTH_SHORT).show();
}
@Override
public void onLongItemClick(View view, int position){
//do whatever
}
}));
}
The problem that I am facing now is that how can I pass the wsd.id
from the for loop to the onItemClick
method ? The object wsd
has to be created inside the for loop. Is there any workaround ?
Edit:
Adapter code as below:
public class AdapterRecycler extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private LayoutInflater inflater;
List<WarehouseSalesDetails> data = Collections.emptyList();
WarehouseSalesDetails current;
int currentPos = 0;
//to initialize context and data sent from MainActivity
public AdapterRecycler(Context context, List<WarehouseSalesDetails> data){
this.context = context;
inflater = LayoutInflater.from(context);
this.data = data;
}
//inflate the layout when viewholder created
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = inflater.inflate(R.layout.item_listview,parent,false);
MyHolder holder = new MyHolder(view);
return holder;
}
//Bind data
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position){
//get current position of item in recyclerview to bind data and assign values from list
MyHolder myHolder = (MyHolder) holder;
WarehouseSalesDetails current = data.get(position);
myHolder.textName.setText(current.company_name);
myHolder.textTitle.setText(current.title);
myHolder.textPeriod.setText(current.promotional_period);
//myHolder.textPeriod.setText(ContextCompat.getColor(context,R.color.colorAccent));
//load image into imageview using glide
Glide.with(context).load(current.promotion_image)
.placeholder(R.drawable.error)
.error(R.drawable.error)
.into(myHolder.ivImage);
}
@Override
//return total item from list
public int getItemCount(){
return data.size();
}
class MyHolder extends RecyclerView.ViewHolder{
TextView textName;
TextView textTitle;
TextView textPeriod;
ImageView ivImage;
//create constructor to get widget reference
public MyHolder(View itemView){
super(itemView);
textName = (TextView)itemView.findViewById(R.id.company_name);
ivImage = (ImageView)itemView.findViewById(R.id.promotion_image);
textTitle = (TextView)itemView.findViewById(R.id.title);
textPeriod = (TextView)itemView.findViewById(R.id.promotional_period);
}
}
}