0

Strings are located in string-array, I called string and set value like

    nameCollection = getActivity().getResources().getStringArray(R.array.boy);

for (int i = 0; i < nameCollection.length; i++) {
names.add(new Name(nameCollection[i]));   //It will display all in the list

}

Now, If I pressed on any row, that text from string-array should be displayed.. For example,

cv.setOnClickListener(new OnClick....  {
 Toast.makeText(getApplicationContext(),"Need to display selected string row data",Toast.LENGTH_SHORT).show();
});

For example the list is like

APPLE
BALL
CAT
DOG

Now, If I press CAT then display toast as "CAT" I am using RecyclerView not listview. Thank you!! ;)

Queendevelopers
  • 183
  • 3
  • 20

3 Answers3

1

In your onBindViewHolder() of RecyclerView use following code.

holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "Click:" + names.get(position), Toast.LENGTH_SHORT).show();
            }
        });
Shaishav Jogani
  • 2,111
  • 3
  • 23
  • 33
1

Better way to implement the OnClickListener for RecyclerView which is much less tightly coupled is...

Snippet of usage:

RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.addOnItemTouchListener(
    new RecyclerItemClickListener(context, recyclerView ,new RecyclerItemClickListener.OnItemClickListener() {
      @Override public void onItemClick(View view, int position) {
        // do whatever
      }

      @Override public void onLongItemClick(View view, int position) {
        // do whatever
      }
    })
);

RecyclerItemClickListener implementation:

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;


public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
  private OnItemClickListener mListener;

  public interface OnItemClickListener {
    public void onItemClick(View view, int position);

    public void onLongItemClick(View view, int position);
  }

  GestureDetector mGestureDetector;

  public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
    mListener = listener;
    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
            if (child != null && mListener != null) {
                mListener.onLongItemClick(child, recyclerView.getChildAdapterPosition(child));
            }
        }
    });
}

  @Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
    View childView = view.findChildViewUnder(e.getX(), e.getY());
    if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
      mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
      return true;
    }
    return false;
  }

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

  @Override
  public void onRequestDisallowInterceptTouchEvent (boolean disallowIntercept){}
}
Ashish Rajvanshi
  • 466
  • 4
  • 15
  • I hope this will work for me.. because for doing these things onItemClickListener is needed and you have done that!! I will try this tonight and reply back to you.. Thank you for your effort. – Queendevelopers Sep 13 '16 at 11:15
  • I followed the code, but still can't display the selected code on ItemClickListener I did Toast.makeText(getApplicationContext(),names.get(position).getName()+"", Toast.LENGTH_SHORT).show(); where .getName() is not found for me!! :( – Queendevelopers Sep 13 '16 at 14:22
0

ListView have OnItemClickListner.If you use this listner you will get row position as parameter.Using this position you can acess data from string array.

listView.setOnItemClickListener(new OnItemClickListener()
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
        Toast.makeText(getApplicationContext(),names.get(position).getName()+"", Toast.LENGTH_SHORT).show();
    }
});
Jinesh Francis
  • 3,377
  • 3
  • 22
  • 37
  • getName() method is not found, so I use toString() but it displays Name@14756 below is the code Toast.makeText(view.getContext(),""+names.get(position).toString(), Toast.LENGTH_SHORT).show(); – Queendevelopers Sep 12 '16 at 09:11