Android:从回收器视图中删除空行
所以我有一个用户输入和一个按钮。例如,当用户输入一些文本并单击按钮时,会出现另一个活动,并将用户输入加载到recyclerView中。 问题是,不知何故,我的回收器视图添加了一些空行,如图中所示 是否有任何代码可以修复此错误? 这是我的适配器Android:从回收器视图中删除空行,android,android-activity,android-recyclerview,Android,Android Activity,Android Recyclerview,所以我有一个用户输入和一个按钮。例如,当用户输入一些文本并单击按钮时,会出现另一个活动,并将用户输入加载到recyclerView中。 问题是,不知何故,我的回收器视图添加了一些空行,如图中所示 是否有任何代码可以修复此错误? 这是我的适配器 public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> { private ArrayList<Exam
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public TextView mTextViewLine1;
public ExampleViewHolder(View itemView) {
super(itemView);
mTextViewLine1 = itemView.findViewById(R.id.textview_line1);
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = exampleList;
}
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mTextViewLine1.setText(currentItem.getLine1());
}
@Override
public int getItemCount() {
return mExampleList.size();
}
}
公共类ExampleAdapter扩展了RecyclerView.Adapter{
私有ArrayList mExampleList;
公共静态类ExampleViewHolder扩展了RecyclerView.ViewHolder{
公共文本视图mTextViewLine1;
公共示例ViewHolder(视图项视图){
超级(项目视图);
mTextViewLine1=itemView.findViewById(R.id.textview\u line1);
}
}
公共示例适配器(ArrayList示例列表){
mExampleList=示例列表;
}
@凌驾
公共示例ViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.example_项,parent,false);
ExampleViewHolder evh=新的ExampleViewHolder(v);
返回evh;
}
@凌驾
BindViewHolder上的公共无效(例如ViewHolder,int位置){
ExampleItem currentItem=MeExampleList.get(位置);
holder.mTextViewLine1.setText(currentItem.getLine1());
}
@凌驾
public int getItemCount(){
返回mExampleList.size();
}
}
很难从适配器代码中分辨出来,可能是因为您的搜索。但是试试这个,看看会发生什么:
//in the adapter constructor
public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = new ArrayList<ExampleItem>();
for(int i=0 ; i<exampleList.size() ; i++){
ExampleItem item = exampleList.get(i);
if(item.getLine1() != null && !item.getLine1().isEmpty()){
mExampleList.add(item);
}
}
}
//在适配器构造函数中
公共示例适配器(ArrayList示例列表){
mExampleList=newArrayList();
对于(int i=0;i,很难从适配器代码中分辨出来,这可能与您的搜索有关。但尝试一下,看看会发生什么:
//in the adapter constructor
public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = new ArrayList<ExampleItem>();
for(int i=0 ; i<exampleList.size() ; i++){
ExampleItem item = exampleList.get(i);
if(item.getLine1() != null && !item.getLine1().isEmpty()){
mExampleList.add(item);
}
}
}
//在适配器构造函数中
公共示例适配器(ArrayList示例列表){
mExampleList=newArrayList();
对于(int i=0;如果首先将图像添加到您的问题中,那么您必须至少提供适配器代码。请发布您的代码,以便我们能够帮助您。@HasanBouTaam您是对的。我只需添加图像和适配器。首先将图像添加到您的问题中,然后您必须至少提供适配器代码。请发布您的代码,以便我们能够提供帮助帮助你。@HasanBouTaam你是对的。我只是添加了图像和适配器之类的东西,这是我以前尝试过的。你的代码也给了我“无法解析符号位置”。但我认为它接近我需要的。我的意思是I
不是位置之类的东西,这是我以前尝试过的。你的代码也给了我“无法解析符号位置”。但我认为它接近我需要的。我的意思是i
而不是position