set intent, OnCreate in recycler view to go to another specific activity for specific list(row)

babukancha :

I have implemented onClick, and when I clicked to each list all are going to "abc" activity but I want to navigate list having word "dd" into activity "abcActivity" and list having word "ss" to "defActivity".

I tried to add plus another intent but it again shows same for all list :(

please help me, I read all the suggested articles given while writing this but non of them were answer of this what i asked. please help me.

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 myIntent = new Intent(MainActivity.this, abc.class);
        myIntent.putExtra("dd", 1); 
        MainActivity.this.startActivity(myIntent);


        Intent feedbackIntent = new Intent(MainActivity.this, def.class);
        feedbackIntent.putExtra("ss", 0); 
        MainActivity.this.startActivity(feedbackIntent);

    }
}

This is code I added under oncreate of "abc"

Intent myIntent = getIntent();
String value = myIntent.getStringExtra("dd" );

This is code I added under oncreate of "def"

Intent intent = getIntent();
        String value = intent.getStringExtra("ss");

And this is Adapter named 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);
    }
}

I KNOW i am wrong :(. I'm beginner please tolerate these and recommend solution.enter code here

babukancha :

I was just trying different methods from myself and finally this worked, I'm beginner and when I nailed it I was very happy :p

To pass intent from recycler there is no need to write any code in anctivity where we are sending intent.

just write the code given below in onNoteLitner by if else statement:

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

after that leave Adapter same if you have as mine, i mean if you have an adapter as of mine question above.

then, implement Parcelable in Word(which provide images, text) to adapter, then it should look like below:

public class Word implements Parcelable {

    private int imageResource;
    private String title;

    public Word(int imageResource, String title) {
        this.imageResource = imageResource;
        this.title = title;
    }

    protected Word(Parcel in) {
        imageResource = in.readInt();
        title = in.readString();
    }

    public static final Creator<Word> CREATOR = new Creator<Word>() {
        @Override
        public Word createFromParcel(Parcel in) {
            return new Word(in);
        }

        @Override
        public Word[] newArray(int size) {
            return new Word[size];
        }
    };

    public int getImageResource() {
        return imageResource;
    }

    public String getTitle() {
        return title;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(imageResource);
        parcel.writeString(title);
    }
}

Feel free to ask if you faced problem, i will try my best to help you!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=162974&siteId=1