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