1

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);
            }
        }
    }
4
  • You can use setTag and getTag. Here is an example : stackoverflow.com/questions/5291726/… Commented Jan 7, 2017 at 5:34
  • you can get it from data.get(position) Commented Jan 7, 2017 at 5:40
  • @AnkurAggarwal can you please show how can I apply that in this case ? Commented Jan 7, 2017 at 5:55
  • Add your adapter code so we can provide solved code Commented Jan 7, 2017 at 6:00

1 Answer 1

1

You can achieve this simply by setTag and getTag with your ViewHolder view item in your adapter:

 @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            //This is my code for sample your code will be here for item layout setup
            View view = LayoutInflater.from(mContext).inflate(R.layout.item, null);
            //I am passing main item view to viewholder class
            return new ViewHolder(view);
        }

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {
    //your UI code setup from list data to item views

}

    public class ViewHolder extends RecyclerView.ViewHolder {
            //your views
            public ViewHolder(View itemView) {
                super(itemView);
                //view references
                itemView.setTag(your_list.get(getAdapterPosition()).id);
                //here I setTag as id you can set any object as tag and can get wherever this itemView is present, as we are retrieving in onItemClick
            }
        }

For your item touchlistener:

warehouse_recycler.addOnItemTouchListener(
            new RecyclerItemClickListener(context,warehouse_recycler,new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    //view.getTag will return your id
                    Toast.makeText(context, "ID is " + view.getTag,
                            Toast.LENGTH_SHORT).show();
                }
                @Override
                public void onLongItemClick(View view, int position){
                    //do whatever
                }
            }));

Updated answer:

No need to do setTag and getTag you can easily get id like this:

warehouse_recycler.addOnItemTouchListener(
                new RecyclerItemClickListener(context,warehouse_recycler,new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        WarehouseSalesDetails wsd = data.get(position);

                        //view.getTag will return your id
                        Toast.makeText(context, "ID is " + wsd .id,
                                Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onLongItemClick(View view, int position){
                        //do whatever
                    }
                }));
Sign up to request clarification or add additional context in comments.

3 Comments

it crashed at this line itemView.setTag(data.get(getAdapterPosition()).id);
No need to do setTag and getTag you can easily get id from WarehouseSalesDetails wsd = data.get(position); and id = wsd.id in your onItemClick. Check updated answer
aha. Thanks buddy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.