0

I have a RecyclerView which contains several data stored on CardView after long press i am enabling a context menu, i am getting the position of particular card and i am able to display the data on Toast also.
But what i want to do is to Store the data in List<> and then retrieve it from another activity class where i can use those data to set on some particular EditText.
I am not sure where i am getting wrong here is my code:-
getting Data on long press and storing in List

public List<String> Data=new ArrayList<>();
 TextView name,qunat,refill;
String nameMed,quantity,refillAmt;

    @Override
            public void onItemLongClick(View view, int position) {
            name = (TextView) view.findViewById(R.id.nameOfUpmingMed);
            qunat=(TextView)view.findViewById(R.id.QuantOfMed);
            refill=(TextView)view.findViewById(R.id.ReffilAmt);
            nameMed = name.getText().toString();
            quantity=qunat.getText().toString();
            refillAmt=refill.getText().toString();
            passData(nameMed,quantity,refillAmt,);
//          Data.add(nameMed);Data.add(quantity);Data.add(refillAmt);

            }
        }));

 public List<String> passData(String name, String quant, String refillAmt){
        String Name,Quant,Refill;
        Name=name;Quant=quant;Refill=refillAmt;
        Data.add(Name);
        Data.add(Quant);
        Data.add(Refill);
         for(int i=0;i<Data.size();i++){
            Toast.makeText(getActivity(), "Pressed card is and "+Data.get(i), Toast.LENGTH_SHORT).show();
        }
        return Data;

    }

When I am retrieving data on long click it is working fine, it give the data of particular card pressed. But when I am passing the Data to list on long press it shows the previous pressed data first and then the current data.

My 2nd question is i am trying to access this list in another class but it is not working.
code for other class is

prescriptionFragment=new PrescriptionFragment();
for(int i=0;i < prescriptionFragment.Data.size();i++){
            if(i==0){
                Toast.makeText(getApplication(),"data is "+prescriptionFragment.Data.get(i),Toast.LENGTH_LONG).show();
            }
        }

How to do this, how can i get data from one class to another on long press of a card view. Or is there any other way of doing this.

Vipul Singh
  • 393
  • 9
  • 26

2 Answers2

0

You can add GestureDetector to your RecyclerView like that :

First of all create an arrayList like that

List <Message> longPressedList=new ArrrayList<Message>();


final GestureDetector messageGestureDetector = new GestureDetector(MessageActivity.this, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            View child = recyclerviewmessages.findChildViewUnder(e.getX(), e.getY());
            Message longClickedMessage= new Message();
                if (recyclerviewmessages.getChildPosition(child) >= 0 && recyclerviewmessages.getChildPosition(child) <= Messages.size()) {
                    longClickedMessage= Messages.get(recyclerviewmessages.getChildPosition(child));
                    //add to arraylist   
                    longPressedList.add(longClickedMessage);
                    //send new list to other class
                    EventBus.getDefault().postSticky(new MessageListEvent (longPressedList));

                }


        }
    });

Then :

recyclerviewmessages.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
            View child = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());

            if (child != null && messageGestureDetector.onTouchEvent(motionEvent)) {
                //here your recyclerview touch event if matchs gesture detector 
                //you return true , else return false

                return true;
            }

            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {}

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean b) {}
    });

And if you want to use this list to another class i will suggest you to use EventBus :

Your MessageListEvent class :

public class MessageListEvent  {


private ArrayList<Message> messageList;

public MessageListEvent (ArrayList<Message> messages){
    this.messageList=messages;

}

In your fragment or other class in oncreate :

if (!EventBus.getDefault().isRegistered(this)) {
        EventBus.getDefault().register(this);
    }

Then Override Subscribe method :

@Subscribe
public void onMessageEvent(MessageListEvent event){

      //you can get list like that :  event.messageList


}

Add this to your build.gradle : compile 'org.greenrobot:eventbus:3.0.0'

Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
  • Your answer not relevant to his question. i think so – EngineSense May 12 '16 at 12:29
  • Thank you @Log.d , i just focused "My 2nd question is i am trying to access this list in another class but it is not working" motto . And i suggest him to use eventbus. He can combine this answer with others – Yasin Kaçmaz May 12 '16 at 12:32
0

In the ViewHolder of the Adapter of your RecyclerView, you can do...

class YourViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener
{
    TextView name, qunat, refill;

    public YourViewHolder(View view)
    {
        super(view);
        name = (TextView) view.findViewById(R.id.nameOfUpmingMed);
        qunat=(TextView)view.findViewById(R.id.QuantOfMed);
        refill=(TextView)view.findViewById(R.id.ReffilAmt);

        view.setOnLongClickListener(this);
    }

    @Override
    public boolean onLongClick(View v)
    {
        String nameMed = name.getText().toString();
        String quantity = qunat.getText().toString();
        String refillAmt = refill.getText().toString();
        passData(nameMed,quantity,refillAmt,);
        //Do other stuff you need to do.
        return true;
    }
}
esfox
  • 175
  • 3
  • 11
  • Ok but how do i carry it to next Class – Vipul Singh May 12 '16 at 12:24
  • Do you mean how you can get the List of your data from another class? – esfox May 12 '16 at 12:27
  • yes. I have done that but it seems to be not working – Vipul Singh May 12 '16 at 12:30
  • Do you have a different class for the RecyclerView adapter? Maybe the `passData()` method is outside the adapter class. If it is, you can make `passData()` static or make an interface that contains `passData()` and make the other class implement the interface you made. Then call `passData()` from the other class by doing something like... `((InterfaceName) ObjectOfOtherClass /*context object if the other class is an activity*/).passData(/*parameters*/);` – esfox May 12 '16 at 12:59
  • yes i have another class for RecyclerView adapter.. i will try to implement this. – Vipul Singh May 13 '16 at 05:17
  • Thanks its working now.. I made my passData() `static`.. @esfox – Vipul Singh May 13 '16 at 06:17