-1

I need to set OnClickListener for each list item of recycler view.
I want to navigate to a different activity by clicking each list view item.

So far I've managed to pass the click position via a callback to the same activity for each click item from then list. However, I want to start a different activity when clicking an item from the list.

Let's assume I want to click the row that has text "dd" then it should go to activity named "abc"

please help me. please.

This is main activity:

public class MainActivity extends AppCompatActivity implements WordAdapter.OnNoteListener {
    private RecyclerView recyclerView;
    private static final String TAG = MainActivity.class.getCanonicalName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.home_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);

        List<Word> wordList = new ArrayList<>();


        wordList.add(new Word(R.drawable.ic_launcher_background, "dd"));
        wordList.add(new Word(R.drawable.ic_launcher_background, "ss"));
        wordList.add(new Word(R.drawable.ic_launcher_background, "cc"));


        WordAdapter adapter = new WordAdapter(this);
        recyclerView.setAdapter(adapter);
        adapter.addItems(wordList);
    }

    @Override
    public void onNoteClick(int position) {
        Log.d(TAG, "clicked on the position:" + position);
Intent intent = new Intent(this, abc.class);
        this.startActivity(intent);
    }
}

This is Adapter named as WordAdapter:

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

    private List<Word> wordList;
    private OnNoteListener mOnNoteListener;

    public WordAdapter(List<Word> wordList, OnNoteListener onNoteListener) {
        this.wordList = wordList;
        this.mOnNoteListener = onNoteListener;
    }

    public WordAdapter(OnNoteListener onNoteListener) {
        this(new ArrayList<Word>(), onNoteListener);
    }

    public void addItems(List<Word> items) {
        wordList.addAll(items);
        notifyDataSetChanged();
    }

    public void clear() {
        wordList.clear();
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewholder, int position) {
        int resource = wordList.get(position).getImageResource();
        String title = wordList.get(position).getTitle();
        viewholder.setData(resource, title);
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView imageView;
        private TextView title;

        private OnNoteListener onNoteListener;

        public ViewHolder(@NonNull View itemView, OnNoteListener onNoteListener) {
            super(itemView);

            imageView = itemView.findViewById(R.id.imageView);
            title = itemView.findViewById(R.id.word);
            this.onNoteListener = onNoteListener;

            itemView.setOnClickListener(this);
        }

        private void setData(int resource, String titleText) {
            imageView.setImageResource(resource);
            title.setText(titleText);
        }

        @Override
        public void onClick(View view) {
            onNoteListener.onNoteClick(getAdapterPosition());
        }
    }

    public interface OnNoteListener {
        void onNoteClick(int position);
    }
}
Andrei T
  • 2,985
  • 3
  • 21
  • 28
Dibas Dauliya
  • 639
  • 5
  • 20
  • 1
    https://stackoverflow.com/a/44152237/4657385 – Sabyasachi Jun 20 '19 at 11:23
  • 3
    Possible duplicate of [How to add Onclick listener to recycler view](https://stackoverflow.com/questions/44151979/how-to-add-onclick-listener-to-recycler-view) – SaadAAkash Jun 20 '19 at 11:32
  • @sm_happy , @ahmedalijubair; I asked how can intent and receive in another activity when clicked in each list, I've already did all the things mentioned in that question's answers. please read the question, code give their before nonvoting. – Dibas Dauliya Jun 20 '19 at 12:10

3 Answers3

0

Update your interface to include ViewHolder as a parameter.

public interface OnNoteListener {
    public void onNoteClick(ViewHolder viewHolder);    // Don't need position anymore, since you can call viewHolder.getAdapterPosition() directly.
}

For the complete procedure to do that check out @sm_happy's provided link.

And then in your Activity's onNoteClick() method change Text

viewHolder.textView.setText(viewHolder.textView.getText().toString() + "dd");    // Although the better option would be to instead keep a parameterized string in your resources file.

and launch a new Activity.

Abbas
  • 3,529
  • 5
  • 36
  • 64
0

Your Adapter class will have your interface like below :

public class AdapterClass extends RecyclerView.Adapter<AdapterClass.Viewholder>{

private OnItemClick onItemClick;

public NotificationAdapter(Context context) {
       onItemClick = (OnItemClick)context;
    }

public interface OnItemClick {
        void onClick(int position);
    }

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

holder.layout.setOnClickListener(v -> {
            onItemClick.onClick(position);
        });

    }
}

Now your Activity will implement this interface like below :

         public class MainActivity extends AppCompatActivity implements AdapterClass.OnItemClick {


                @Override
                public void onClick(int position) {
                    Log.d(TAG, "clicked on the position:" + position);
                    startNewActivity(position);
                }

                private void startNewActivity(int position){
                Intent intent = new Intent(MainActivity.this, NewActivity.class);
                //Pass your data here if required
                intent.putExtra("key_for_data", position); 

                startActivity(intent);
        }
    }
Apurv
  • 391
  • 2
  • 9
0

So I will attach the full solution as I modified a bit the original code.

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


    private List<Word> wordList;
    private OnNoteListener mOnNoteListener;

    public WordAdapter(List<Word> wordList, OnNoteListener onNoteListener) {
        this.wordList = wordList;
        this.mOnNoteListener = onNoteListener;
    }

    public WordAdapter(OnNoteListener onNoteListener) {
        this(new ArrayList<Word>(), onNoteListener);
    }

    public void addItems(List<Word> items) {
        wordList.addAll(items);
        notifyDataSetChanged();
    }

    public void clear() {
        wordList.clear();
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewholder, int position) {
        int resource = wordList.get(position).getImageResource();
        String title = wordList.get(position).getTitle();
        viewholder.bind(resource, title, mOnNoteListener);
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;
        private TextView title;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.imageView);
            title = itemView.findViewById(R.id.word);
        }

        private void bind(final int resource, final String titleText, final OnNoteListener listener) {
            imageView.setImageResource(resource);
            title.setText(titleText);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onNoteClick(titleText);
                }
            });
        }
    }

    public interface OnNoteListener {
        void onNoteClick(String dataClicked);
    }
}


As you can see I changed a bit the code so that you can pass the data and not the position. Below you can see the code that starts the new activity.

    public class MainActivity extends AppCompatActivity implements WordAdapter.OnNoteListener {

    private RecyclerView recyclerView;

    private static final String TAG = MainActivity.class.getCanonicalName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.home_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setLayoutManager(layoutManager);

        List<Word> wordList = new ArrayList<>();


        wordList.add(new Word(R.drawable.ic_launcher_background, "dd"));
        wordList.add(new Word(R.drawable.ic_launcher_background, "ss"));
        wordList.add(new Word(R.drawable.ic_launcher_background, "cc"));


        WordAdapter adapter = new WordAdapter(this);
        recyclerView.setAdapter(adapter);
        adapter.addItems(wordList);
    }

    @Override
    public void onNoteClick(String dataClicked) {
        Log.d(TAG, "clicked on the position:" + dataClicked);
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra(STRING_EXTRA, dataClicked);
        startActivity(intent);
    }
}


For the second activity I added something basic.

    public class SecondActivity extends AppCompatActivity {


    public static final String TAG = SecondActivity.class.getSimpleName();

    public static String STRING_EXTRA = "string_extra";

    private TextView textView = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        textView = findViewById(R.id.tv_received);
        parseIntent(getIntent());
    }

    private void parseIntent(Intent intent) {
        if (intent != null) {
            Bundle extras = intent.getExtras();
            assert extras != null;
            String received = extras.getString(STRING_EXTRA);
            textView.setText(received);
        }

    }
}


I added a basic xml layout(second_activity.xml):

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">

    <TextView
        android:id="@+id/tv_received"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="received from activity one"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>


Now, you need to register the activity to the manifest and you should be done.

   <aplication tag here>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application>

        <activity android:name=".SecondActivity" />
Andrei T
  • 2,985
  • 3
  • 21
  • 28