java - 使用搜索 View 时适配器未更新

标签 java android android-recyclerview android-adapter

这基本上是一个笔记应用程序,其中我们通过从用户处获取标题和描述来动态地将数据添加到我们的应用程序中,问题是,当我们通过标题搜索某个笔记时,适配器中的数据集会消失,而不是给出可能的笔记,逻辑是在适配器类中的filter()函数中编写的

MainActivity.java

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
private ArrayList<Notes> list;
private NotesAdapter notesAdapter;//this is our notes adapter
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
@Override //
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    list=new ArrayList<>();//this is the list which we have to pass into adapter
    recyclerView=findViewById(R.id.rv);
    notesAdapter=new NotesAdapter(this,list);
    View dialogView= LayoutInflater.from(this).inflate(R.layout.dialog_main,null,false);
    final EditText title=dialogView.findViewById(R.id.t);
    final EditText description=dialogView.findViewById(R.id.d);
    final AlertDialog alertDialog=new AlertDialog.Builder(this)
            .setTitle("Enter the details:")
            .setCancelable(false)
            .setView(dialogView)
            .setPositiveButton("add", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    list.add(new Notes(title.getText().toString(),description.getText().toString(),false));
                    notesAdapter.notifyItemChanged(list.size());
                }
            })
            .setNegativeButton("no", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            }).create();


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override // on pressing the fab we will get an alert dialog box where we can add title and description and with the option to add it or not 
        public void onClick(View view) {
            alertDialog.show();
        }
    });
    recyclerView.setAdapter(notesAdapter);
    linearLayoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
    recyclerView.setLayoutManager(linearLayoutManager);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    final MenuItem searchItem = menu.findItem(R.id.search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setOnQueryTextListener(this);
    return true;
}``

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.search) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onQueryTextSubmit(String s) {
    notesAdapter.filter(s);
    return true;
}

@Override
public boolean onQueryTextChange(String s) {
    notesAdapter.filter(s);
    return true;
}

NotesAdapter.java

 public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.NotesHolder> {
    private ArrayList<Notes> arrayList;
    private ArrayList<Notes> arrayListCopy;
    Context c;
    NotesAdapter(Context context,ArrayList<Notes> list){
        this.arrayList=list;
        this.c=context;
         this.arrayListCopy=new ArrayList<>(list);//this is where I store identical list which I get from Adapter
    }
    public class NotesHolder extends RecyclerView.ViewHolder {
        TextView textView;
        public NotesHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.tv);
        }
    }

    @Override
    public NotesAdapter.NotesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new NotesHolder(LayoutInflater.from(c).inflate(R.layout.item_row,parent,false));
    }

    @Override
    public void onBindViewHolder(final NotesAdapter.NotesHolder holder, final int position) {
              final Notes currentNote=arrayList.get(position);
        holder.textView.setText(currentNote.getTitle());
        holder.textView.setOnLongClickListener(new View.OnLongClickListener() {
           @Override
           public boolean onLongClick(View view) {
               arrayList.remove(holder.getAdapterPosition());
               notifyItemRemoved(holder.getAdapterPosition());
               return true;
           }
       });
        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(c,AnotherActivity.class);
                intent.putExtra("NAME",currentNote.getDescription());
                c.startActivity(intent);
            }
        });
    }

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

     public void filter(String text){//This is my filter function 
        arrayList.clear();
        if(TextUtils.isEmpty(text)){
            arrayList.addAll(arrayListCopy);
        }
        else{
            text=text.toLowerCase();
            for(Notes note:arrayListCopy){
                if(note.getTitle().toLowerCase().contains(text)){
                    arrayList.add(note);
                }
            }
        }
        notifyDataSetChanged();//the data set is still not updated and instead it vanishes
    }
}

一旦我搜索某些内容,整个列表就会消失,我错过了什么?我应该如何修改适配器类中的过滤函数?

最佳答案

更改您的 getItemCount :

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

关于java - 使用搜索 View 时适配器未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48302067/

相关文章:

java - 如何使用简单的html表单从rest服务中获取值(value)?

java - Websphere Liberty + Vaadin + IntelliJ 中的新 Web 应用程序出现 UnableToAdaptException

java - LogCat 中的 FatalException 错误

android - 如何为 RecyclerView 添加可访问性?

android - 具有褪色边缘的水平回收 View

android - ?安卓 :attr/selectableItemBackground not working on white windowBackground

java - 如何在 Xcode 中更改构建的 SDK 版本?

java - Android 替换为正则表达式

android - 在 fragment 中滚动 recyclerview 会导致 View 被上推

android - FFmpeg 错误输出文件 #0 不包含任何流