我想在 recyclerview 中显示编辑文本数据,但它不起作用
Posted
技术标签:
【中文标题】我想在 recyclerview 中显示编辑文本数据,但它不起作用【英文标题】:I want to show edit text data in recyclerview but it doesnot work 【发布时间】:2020-03-19 04:39:54 【问题描述】:请帮忙。当我在移动设备上运行应用程序时,如果它运行则会崩溃,然后应用程序不工作数据不会显示在 recyclerview 中。 当我在移动设备中运行应用程序时,如果它运行则它会崩溃,然后应用程序不工作数据不会显示在 recyclerview 中。当我在移动设备中运行应用程序时,如果它运行则它会崩溃,然后应用程序不工作数据不会显示在 recyclerview 中。
**Adapter Class**
package com.deitel.note;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class myadapter
public class Adapter extends RecyclerView.Adapter<Adapter.Viewholder>
String Name;
String Details;
Context context;
private List<modelclass> modelclassList;
public Adapter(@NonNull MainActivity mainActivity, ArrayList<modelclass> list)
this.context=mainActivity;
this.modelclassList=list;
@NonNull
@Override
public Viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType)
View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout,viewGroup,false);
return new Viewholder(view);
@Override
public void onBindViewHolder(@NonNull Viewholder holder, int position)
Name=modelclassList.get(position).getName();
Details=modelclassList.get(position).getDetails();
@Override
public int getItemCount()
return modelclassList.size();
class Viewholder extends RecyclerView.ViewHolder
public TextView name;
public TextView details;
public Button btn_add;
public Viewholder(@NonNull View itemView)
super(itemView);
name=itemView.findViewById(R.id.text_name);
details=itemView.findViewById(R.id.text_details);
btn_add=itemView.findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
);
**Model Class**
package com.deitel.note;
import android.telephony.SignalStrength;
public class modelclass
String name;
String details;
public modelclass(String name, String details)
this.name = name;
this.details = details;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getDetails()
return details;
public void setDetails(String details)
this.details = details;
**Main Activity**
package com.deitel.note;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.Adapter;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
RecyclerView recyclerView;
Adapter adapter;
ArrayList<modelclass> list=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView.findViewById(R.id.recyclerview);
LinearLayoutManager mlayoutmanager=new LinearLayoutManager(this);
mlayoutmanager.setOrientation(RecyclerView.VERTICAL);
recyclerView.setLayoutManager(mlayoutmanager);
【问题讨论】:
【参考方案1】:首先,您没有在适配器内的视图上调用setText
。
第二。当您设置类变量String Name;
、String Details;
时,它们只能是一个值,这意味着每次显示适配器中的项目并调用onBind
方法时,您只是替换了变量。
第三。你的代码中有一些其他警告(代码样式和东西),所以我把它拿走了并为你清理了它。 :)
适配器类
public class MyAdapter //Class names should be in [pascalcase][1]
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>
private List<ModelClass> elements;
/**
* //You don't need context here, you can easily cause a memory leak.
* @param elements should be a list and not an [ArrayList] that way your not limited to [ArrayList] but [ArrayList] will work
*/
public Adapter(List<ModelCase> elements)
this.elements = elements;
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType)
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
return new ViewHolder(inflater.inflate(R.layout.item_layout, viewGroup, false));
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
ModelClass model = modelclassList.get(position);
holder.name.setText(model.getName());
holder.details.setText(model.getDetails());
@Override
public int getItemCount()
return elements.size();
protected class ViewHolder extends RecyclerView.ViewHolder //Class names should be in [pascalcase][1]
TextView name; //Class names should be in [camelCase][2]
TextView details; //Class names should be in [camelCase][2]
Button addButton; //Class names should be in [camelCase][2]
public ViewHolder(@NonNull View itemView)
super(itemView);
name = itemView.findViewById(R.id.text_name);
details = itemView.findViewById(R.id.text_details);
addButton = itemView.findViewById(R.id.btn_add);
addButton.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
);
模型类
package com.deitel.note;
import android.telephony.SignalStrength;
public class ModelClass //Class names should be in [pascalcase][2]
private String name;
private String details;
public ModelClass(String name, String details)
this.name = name;
this.details = details;
public String getName()
return name;
public void setName(String name)
this.name = name;
public String getDetails()
return details;
public void setDetails(String details)
this.details = details;
主要活动
package com.deitel.note;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.Adapter;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
RecyclerView recyclerView;
MyAdapter.Adapter adapter;
ArrayList<ModelClass> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerview);
LinearLayoutManager layoutmanager = new LinearLayoutManager(this); //LinearLayoutManager is RecyclerView.VERTICAL by default
recyclerView.setLayoutManager(layoutmanager);
//Fill in the list before creating the adapter
adapter = new MyAdapter.Adapter(list);
recyclerView.setAdapter(adapter);
【讨论】:
以上是关于我想在 recyclerview 中显示编辑文本数据,但它不起作用的主要内容,如果未能解决你的问题,请参考以下文章
我想在我的 recyclerview 中显示来自 URL 的图片 [重复]