0

I'm trying to get some data from my adapter with out success. I have in my main activity function that get some item position and need to get from this item the item name. I can figure out how to achieve that. I would be happy if someone can help me to fix it.

Function from my main activity -

        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            int position = viewHolder.getAdapterPosition();

            if (direction == ItemTouchHelper.LEFT) {
                Log.e(TAG, RecyclerAdapter.getIdFromPosition(position)); // Here what I try to do but to achieve that the getIdFromPosition
                                                                            function need to be static and I can do this function to be static 
            } else {
                Log.e(TAG,"Right");
            }
        }

The adapter function -

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

List<Task> data= Collections.emptyList();

public RecyclerAdapter(MainActivity mainActivity, List<Task> data) {
    this.data = data;
}


@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_list, viewGroup, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    Task current = data.get(position);

    //holder.tv1.setText(name[position]);
    ViewHolder.tv2.setText(current.data);
    if(current.sync != "null"){
        holder.tv1.setText("A");
    }
    holder.imageView.setTag(holder);
}

@Override
public int getItemCount() {
    return data.size();
}

public void getIdFromPosition(int position){
    Log.e(TAG, data.get(position).getId());
}

public static class ViewHolder extends RecyclerView.ViewHolder{
    public TextView tv1;
    public static TextView tv2;
    public ImageView imageView;

    public ViewHolder(View view) {
        super(view);

         tv1 = (TextView) view.findViewById(R.id.list_title);
         tv2 = (TextView)view.findViewById(R.id.list_desc);
         imageView = (ImageView) view.findViewById(R.id.list_avatar);

        view.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                //Log.e("test","test");
            }
        });

    }
}
0day
  • 21
  • 5
  • Possible duplicate of [How to add Onclick listener to recycler view](https://stackoverflow.com/questions/44151979/how-to-add-onclick-listener-to-recycler-view) – sam_c Oct 09 '17 at 22:06
  • You can find answers to this question in many posts on SO. For example: [Recycler onClick](https://stackoverflow.com/questions/24471109/recyclerview-onclick) and [here](https://stackoverflow.com/questions/44151979/how-to-add-onclick-listener-to-recycler-view) – sam_c Oct 09 '17 at 22:08
  • @sam_c the problem is not the onClick, the problem is the onSwiped function that need to get the Adapter parameters. – 0day Oct 09 '17 at 22:26

2 Answers2

0
public interface RecyclerClick {
    void onClick(Object o);
}

Now add the interface as a parameter in your adapter constructor:

public RecyclerAdapter(MainActivity mainActivity, List<Task> data, RecyclerClick click) {
   this.data = data;
   this.click = click;
}

And inside the viewholder, call onClick:

view.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            //Log.e("test","test");
            //Pass any object u want
            click.onClick(current);
        }
    });

And in your main activity implement this interface and pass this as a parameter for the adapter constructor:

adapter = new RecyclerAdapter(this, data, this);

Now the method onClick in your main activity will get called on every click with the desired data/object.

riadrifai
  • 1,108
  • 2
  • 13
  • 26
  • I think you don't understand the problem, the problem going with the onSwiped function that need to set the parameters not the onClick function. – 0day Oct 09 '17 at 22:25
0

To implement swipe for RecyclerView you need to implement ItemTouchHelper.SimpleCallback in the activity or fragment that hosts the RecyclerView adapter:

Something like this:

ItemTouchHelper touchHelper = new ItemTouchHelper(
 new ItemTouchHelper.SimpleCallback(ItemTouchHelper.LEFT) {

     @Override
     public abstract boolean onMove(RecyclerView recyclerView,
         ViewHolder viewHolder, ViewHolder target) {

         return true;// true if moved, false otherwise
     }

     @Override
     public void onSwiped(ViewHolder viewHolder, int direction) {
         int position = viewHolder.getAdapterPosition();
         MyData dataObj = myDataList.get(position);
         // execute whatever you choose to here. 
     }
});

Then when you set up your RecyclerView attach the ItemTouchHelper:

touchHelper.attachToRecyclerView(yourRecyclerView);
sam_c
  • 810
  • 7
  • 25
  • I'm just won't to pass data from the Adapter not to implement swipe because my swipe function all ready work, only need the data. – 0day Oct 10 '17 at 08:58
  • I'm not clear on what you want then. I don't understand what action you want to prompt the data passing, from any of the actions we have discussed you have access to your data. – sam_c Oct 10 '17 at 16:07