S
S
Soudagh2021-05-24 20:08:14
Java
Soudagh, 2021-05-24 20:08:14

How to pass position number from RecyclerView to activity with subsequent interaction with DB?

There is a clickable recyclerview with themes and an activity that is navigated to after clicking on the recyclerview element. I need to fill this activity based on the selected option from the previous activity, namely, I need to get the id from the selected option (filling the recyclerview and the next activity comes from the database), and then use the id in another activity (filling another recyclerview based on the received id) .

There is an assumption what needs to be done through Intent, but all attempts failed :(

ChooseThemeActivity

package com.example.myproject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class ChooseThemeActivity extends AppCompatActivity {

    private RecyclerView mRecycler;
    private ThemeAdapter mThemeAdapter;
    private ThemeDBHelper databaseHelper;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_theme);

        mRecycler = findViewById(R.id.recycler_themes);

        databaseHelper = new ThemeDBHelper(this);
        List<Theme> themeList = databaseHelper.getTheme();

        ThemeAdapter.ThemeChoiceListener themeChoiceListener = (theme, position) ->
                Toast.makeText(getApplicationContext(), "Был выбран пункт " + theme.getTheme(),
                Toast.LENGTH_SHORT).show();



        mThemeAdapter = new ThemeAdapter(this, themeChoiceListener, themeList);
        mRecycler.setAdapter(mThemeAdapter);
    }


    public void onThemeChoiceClick(View view) {
        Intent intent = new Intent(this, WordActivity.class);
        //intent.putExtra("id", id);
        startActivityForResult(intent, 0);
    }

}


ThemeAdapter

package com.example.myproject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

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

    List<Theme> themes;

    interface ThemeChoiceListener {
        void onThemeChoiceClicked(Theme theme, int position);

    }

    private final ThemeChoiceListener onClickListener;

    private final LayoutInflater inflater;

    public ThemeAdapter(Context context, ThemeChoiceListener onClickListener, List<Theme> themes) {
        this.onClickListener = onClickListener;
        this.themes = themes;
        this.inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Theme theme = themes.get(position);
        holder.themeName.setText(themes.get(position).getTheme());
        holder.themeDescription.setText(themes.get(position).getDescriptionTheme());
        holder.itemView.setOnClickListener(v -> onClickListener.onThemeChoiceClicked(theme, position));
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder  {

        final TextView themeName, themeDescription;

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

            themeName = itemView.findViewById(R.id.themeName_tv);
            themeDescription = itemView.findViewById(R.id.description_tv);

        }




    }


}


WordActivity

package com.example.myproject;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class WordActivity extends AppCompatActivity {

    private  RecyclerView mRecycler;
    private WordAdapter mWordAdapter;
    private ThemeDBHelper databaseHelper;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.theme_fragment);

        mRecycler = findViewById(R.id.recycler_words);

        databaseHelper = new ThemeDBHelper(this);

        //long ID = getIntent().getLongExtra("ID");
        //Intent intent = getIntent();
        //String id = intent.getStringExtra("id");
        List<Word> wordList = databaseHelper.getWord(/*id*/);

        mWordAdapter = new WordAdapter(this, wordList);

        mRecycler.setAdapter(mWordAdapter);
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
foonfyrick, 2021-05-25
@foonfyrick

What's the problem? You have a list that you fill with data, then this list displays a recycle, when you click on an item, take its position and get the object you need from the list by index, then transfer it to another activity.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question