为什么项目选择在普通列表的recyclerview中不起作用,而在搜索列表中起作用?

问题描述 投票:0回答:1

我已经为存储在Firebase数据库中的项目创建了arraylist。我能够在recyclerview中列出项目。甚至搜索也可以。但是,在单击recyclerview列表中的项目时,该单击在搜索列表中有效,但在常规列表中无效。代码在下面给出

public class MainActivity extends AppCompatActivity implements Adapter.SelectedProject {

    DatabaseReference mDatabase;
    ArrayList<projects> list;
    RecyclerView PList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PList = findViewById(R.id.ProjList);
        PList.setHasFixedSize(true);
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Projects");
    }

    @Override
    protected void onStart() {
        super.onStart();

        if (mDatabase != null) {

            mDatabase.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    if (dataSnapshot.exists()) {

                        list = new ArrayList<>();
                        for (DataSnapshot ds : dataSnapshot.getChildren()) {
                            list.add(ds.getValue(projects.class));
                        }
                        Adapter adapter = new Adapter(list, null);
                        PList.setAdapter(adapter);
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                    Toast.makeText(MainActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();

                }
            });
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_home, menu);
        MenuItem item = menu.findItem(R.id.search);
        SearchView searchView = (SearchView) item.getActionView();

        if (searchView != null) {

            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }

                @Override
                public boolean onQueryTextChange(String newText) {
                    search(newText);
                    return true;
                }
            });
        }

        return super.onCreateOptionsMenu(menu);
    }

    private void search(String str) {

        ArrayList<projects> sList = new ArrayList<>();

        for (projects object : list) {

            if (object.getPname().toLowerCase().contains(str.toLowerCase())) {

                sList.add(object);
            }

        }
        Adapter adapter = new Adapter(sList, this);
        PList.setAdapter(adapter);

    }

    @Override
    public void selectedProject(projects projects) {
        startActivity(new Intent(MainActivity.this, InfoActivity.class));
    }
}

适配器类

public class Adapter extends RecyclerView.Adapter < Adapter.ProjViewHolder > {

    private ArrayList < projects > list;
    private SelectedProject selectedProject;

    public Adapter(ArrayList < projects > list, SelectedProject selectedProject) {

        this.list = list;
        this.selectedProject = selectedProject;
    }

    @NonNull
    @Override
    public ProjViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.proj_list, parent, false);
        return new ProjViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ProjViewHolder holder, int position) {

        holder.prname.setText(list.get(position).getPname());
        holder.prlocation.setText(list.get(position).getPlocation());

    }

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

    public interface SelectedProject {
        void selectedProject(projects projects);
    }

    class ProjViewHolder extends RecyclerView.ViewHolder {

        TextView prname, prlocation;

        public ProjViewHolder(@NonNull View itemView) {
            super(itemView);

            prname = itemView.findViewById(R.id.FPName);
            prlocation = itemView.findViewById(R.id.FPLocation);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    selectedProject.selectedProject(list.get(getAdapterPosition()));
                }
            });
        }
    }
}

该选择在搜索适配器中被接受。

Adapter adapter = new Adapter(sList,this);

但是普通适配器显示以下代码错误。

Adapter adapter = new Adapter(list,this);

但是,如果我将其更改为null,如下所示,则错误消失。

Adapter adapter = new Adapter(list,null);

但是当我单击时,该应用程序已关闭。

请让我知道如何纠正错误?

stackrace详细信息

 java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.volq.Adapter$SelectedProject.selectedProject(com.example.volq.projects)' on a null object reference
    at com.example.volq.Adapter$ProjViewHolder$1.onClick(Adapter.java:68)
    at android.view.View.performClick(View.java:5610)
    at android.view.View$PerformClick.run(View.java:22265)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
android android-recyclerview adapter
1个回答
0
投票

注意如何设置适配器以进行搜索与普通设置之间的区别

Adapter adapter = new Adapter(sList, this); // `this`as the second parameter/listener is ok

但是对于另一个:

Adapter adapter = new Adapter(list, null);

这里侦听器为空,此错误是有道理的:

java.lang.NullPointerException:尝试调用接口方法'无效com.example.volq.Adapter $ SelectedProject.selectedProject(com.example.volq.projects)'在null对象引用

而不是null,只需在Activity的MainActivity.this回调中传递onStart()

Adapter adapter = new Adapter(list, MainActivity.this);

但是普通适配器显示以下代码错误。

Adapter adapter = new Adapter(list,this);

是因为this没有在MainActivity回调中引用onDataChange(),而是引用了ValueEventListener

© www.soinside.com 2019 - 2024. All rights reserved.