Android 将数据从Recycler视图传递到Quote应用程序中的新活动,新活动正在打开,但数据未出现
我正在创建一个Quote应用程序,其中Quote将位于recycler视图中,并单击相同的Quote 将出现在用户单击的位置。当我尝试在适配器的视图保持架中使用“单击侦听器”时 类,应用程序活动已更改为其他,但数据未显示Android 将数据从Recycler视图传递到Quote应用程序中的新活动,新活动正在打开,但数据未出现,android,android-studio,android-recyclerview,Android,Android Studio,Android Recyclerview,我正在创建一个Quote应用程序,其中Quote将位于recycler视图中,并单击相同的Quote 将出现在用户单击的位置。当我尝试在适配器的视图保持架中使用“单击侦听器”时 类,应用程序活动已更改为其他,但数据未显示 My Adapter Class. public class QuotesAdapter extends RecyclerView.Adapter<QuotesAdapter.QuotesViewHolder> { List&
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);
}
}
}
我的适配器类。
公共类QuoteAdapter扩展了RecyclerView.Adapter{
列表引号=null;
语境;
公共引号适配器(列表引号、上下文){
this.quotes=quotes;
this.context=上下文;
}
@非空
@凌驾
public QuotesViewHolder onCreateViewHolder(@NonNull ViewGroup ViewGroup,int i){
LayoutFlater充气机=LayoutFlater.from(上下文);
视图=充气机。充气(R.layout.list\u item\u quote,视图组,false);
返回新的QuotesViewHolder(视图);
}
@凌驾
BindViewHolder上的公共无效(@NonNull final QuotesViewHolder QuotesViewHolder,int i){
字符串[]颜色={“#448AFF”、“#FFC107”、“#009688”、“#E91E63”、“#FF5722”};
最终字符串引号=quotes.get(i);
quotesViewHolder.txtQuote.setText(quote);
int color=i%colors.length;
final int intColor=Color.parseColor(colors[Color]);
quotesViewHolder.quoteContainer.setBackgroundColor(intColor);
}
@凌驾
public int getItemCount(){
返回quotes.size();
}
公共类QuotesViewHolder扩展了RecyclerView.ViewHolder{
TextView-txtQuote;
线性布局报价容器;
公共QuotesViewHolder(@NonNull View itemView){
超级(项目视图);
txtQuote=itemView.findviewbyd(R.id.txtQuote);
quoteContainer=itemView.findViewById(R.id.quoteContainer);
}
}
}
主要活动
公共类MainActivity扩展了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;
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView quoteList=findViewById(R.id.quoteList);
quoteList.setLayoutManager(新的LinearLayoutManager(本));
setAdapter(新的quoteAdapter(getQuotes(),this));
}
私有列表getQuotes(){
列表引号=新的ArrayList();
BufferedReader BufferedReader=null;
试一试{
bufferedReader=new bufferedReader(新的InputStreamReader(this.getAssets().open(“quotes.txt”),“UTF-8”);
弦线;
而((line=bufferedReader.readLine())!=null){
引号。添加(行);
}
}捕获(IOE异常){
e、 printStackTrace();
}
最后{
if(bufferedReader!=null){
试一试{
bufferedReader.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
返回报价;
}
}在您的项目上单击适配器使用包传递数据
Intent intent = new Intent(context, NewActivity.class);
intent.putExtra(“myKey”,AnyValue)星触觉(意向) 您可以通过以下方式获取传递的值:
Bundle extras = intent.getExtras();
String myString=extras.getString(“myKey”) 由于我是新来的,我无法在这里计算出键,因此请根据我的情况指定。
key
是您自己的默认字符串,用于识别通过Intent
传递的特定值。这里必须是相同的intent.putExtra(“myKey”,AnyValue)代码>这里:String myString=extras.getString(“myKey”)代码>。你可以按现在的样子离开——“myKey”。SkypeDogg请查看我的代码,然后请告诉我有关我的密钥的信息。这里是我从我的回收器视图中的资产中获取的报价,这是我在新活动中想要的