Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – Passing Data from Recycler View to new Activity in a Quote Application,new activity is opening but data not coming

Posted on November 14, 2022

Solution 1 :

On your itemClick in adapter use bundle to pass data

Intent intent = new Intent(context, NewActivity.class);

intent.putExtra(“myKey”, AnyValue);
startActivity(intent);

You can get the passed values by this way:

Bundle extras = intent.getExtras(); 

String myString = extras.getString(“myKey”);

Problem :

I’m creating a Quote Application where quote will be inside recycler view ,and on click same quote
will appear on which user clicked.As I tried using on Click Listener inside View Holder in Adapter
Class,app activity changed to other,but data not showing.

 My Adapter Class.

                 public class QuotesAdapter extends RecyclerView.Adapter<QuotesAdapter.QuotesViewHolder> {

List<String> quotes = null;
Context context;

public QuotesAdapter(List<String> quotes, Context context) {
    this.quotes = quotes;
    this.context = context;
}

@NonNull
@Override
public QuotesViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.list_item_quote, viewGroup, false);
    return new QuotesViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final QuotesViewHolder quotesViewHolder, int i) {
    String[] colors = {"#448AFF", "#FFC107", "#009688", "#E91E63", "#FF5722"};
    final String quote = quotes.get(i);
    quotesViewHolder.txtQuote.setText(quote);
    int color = i % colors.length;
    final int intColor = Color.parseColor(colors[color]);
    quotesViewHolder.quoteContainer.setBackgroundColor(intColor);

    


}

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

public class QuotesViewHolder extends RecyclerView.ViewHolder {

    TextView txtQuote;
    LinearLayout quoteContainer;

    public QuotesViewHolder(@NonNull View itemView) {
        super(itemView);
        txtQuote = itemView.findViewById(R.id.txtQuote);
        quoteContainer = itemView.findViewById(R.id.quoteContainer);
    }
}
 }

MainActivity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RecyclerView quoteList = findViewById(R.id.quoteList);
    quoteList.setLayoutManager(new LinearLayoutManager(this ));
    quoteList.setAdapter(new QuotesAdapter(getQuotes(), this));



}



private List<String> getQuotes(){
    List<String> quotes = new ArrayList<>();
    BufferedReader bufferedReader = null;

    try {
        bufferedReader = new BufferedReader(new InputStreamReader(this.getAssets().open("quotes.txt"), "UTF-8"));
        String line;
        while ((line = bufferedReader.readLine()) != null){
            quotes.add(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        if(bufferedReader != null){
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return  quotes;
}

}

Comments

Comment posted by APP Learener

AS I’m new I’m not able to figure out key here,so pplease specify acc. to my case.

Comment posted by SkypeDogg

key

Comment posted by APP Learener

SkypeDogg please have a look on my code and then Please tell me about my key .Here,quotes that I’m fetching from assets in my recycler view ,is what I want in my new activity

READ  [ANSWERED] flutter - can't install intel HAMX
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress