单击过滤的RecyclerView项目时显示错误数据

时间:2019-04-01 04:56:45

标签: java android mysql android-recyclerview filtering

我有一个带有edittext的RecyclerView,可以在我的Android应用中进行搜索。当我搜索它并单击一个项目时,它显示了错误的数据。

我知道为什么会这样,但是我不知道如何解决。我已经尝试了很多东西,但是仍然有问题。我是编程新手,请帮助:)。

这是我的适配器的代码。

public class ProjectAdapter extends RecyclerView.Adapter<ProjectAdapter.ProjectViewHolder> {

    private Context mCtx;
    private List<Project> projectList;
    private OnItemClickListener mListener;


    public interface OnItemClickListener {
        void onItemClick(int position);

    }



    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }





    public ProjectAdapter(Context mCtx, List<Project> projectList) {
        this.mCtx = mCtx;
        this.projectList = projectList;
    }

    @Override
    public ProjectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.project_list, null);
        return new ProjectAdapter.ProjectViewHolder(view);
    }


    @Override
    public void onBindViewHolder(ProjectViewHolder holder, int position) {
        Project project = projectList.get(position);
        holder.textViewProject.setText(project.getProject());

    }


    @Override
    public int getItemCount() {

        return projectList.size();
    }



    class ProjectViewHolder extends RecyclerView.ViewHolder {

        TextView textViewProject;


        public ProjectViewHolder(View itemView) {
            super(itemView);

            textViewProject = itemView.findViewById(R.id.textViewProject);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null){
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION){
                            mListener.onItemClick(position);

                        }
                    }
                }
            });


        }
    }
}

这是我的ListprojectActivity.java

public class ListprojectActivity extends AppCompatActivity implements ProjectAdapter.OnItemClickListener {

    public static final String project_select = "project";

    private static final String URL_PRODUCTS = "http://192.168.43.245/android_register_login/Api_1.php";
    EditText editTextProject;

    //a list to store all the products
    List<Project> projectList;




    //the recyclerview
    RecyclerView recyclerView;

    ProjectAdapter adapter;

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

        //getting the recyclerview from xml
        recyclerView = findViewById(R.id.recylcerViewProject);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        editTextProject = findViewById(R.id.EditTextProject);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        DividerItemDecoration itemDecoration = new DividerItemDecoration(this, layoutManager.getOrientation());
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(itemDecoration);



        //initializing the productlist
        projectList = new ArrayList<>();


        editTextProject.addTextChangedListener (new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                final String query = s.toString().toLowerCase().trim();
                final ArrayList<Project> filteredList = new ArrayList<>();


                for (int i = 0; i < projectList.size(); i++) {

                    final String text = projectList.get(i).getProject().toLowerCase();
                    if (text.contains(query)) {
                        filteredList.add(projectList.get(i));
                    }
                }

                recyclerView.setLayoutManager(new LinearLayoutManager(ListprojectActivity.this));
                adapter = new ProjectAdapter(ListprojectActivity.this, filteredList);
                recyclerView.setAdapter(adapter);
                adapter.setOnItemClickListener(ListprojectActivity.this);
                adapter.notifyDataSetChanged();




            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });


        //this method will fetch and parse json
        //to display it in recyclerview
        loadProjects();
    }

    private void loadProjects() {

        /*
         * Creating a String Request
         * The request type is GET defined by first parameter
         * The URL is defined in the second parameter
         * Then we have a Response Listener and a Error Listener
         * In response listener we will get the JSON response as a String
         * */
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_PRODUCTS,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            //converting the string to json array object
                            JSONArray array = new JSONArray(response);

                            //traversing through all the object
                            for (int i = 0; i < array.length(); i++) {

                                //getting product object from json array
                                JSONObject project = array.getJSONObject(i);

                                //adding the product to product list

                                projectList.add(new Project(
                                        project.getInt("id_project"),
                                        project.getString("project")
                                ));
                            }

                            //creating adapter object and setting it to recyclerview
                            ProjectAdapter adapter = new ProjectAdapter(ListprojectActivity.this, projectList);
                            recyclerView.setAdapter(adapter);
                            adapter.setOnItemClickListener(ListprojectActivity.this);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        //adding our stringrequest to queue
        Volley.newRequestQueue(this).add(stringRequest);
    }


    @Override
    public void onItemClick(int position) {
        Intent detailMasalah = new Intent(this, ListproblemActivity.class);
        Project projectclick = projectList.get(position);

        detailMasalah.putExtra(project_select, projectclick.getProject());

        startActivity(detailMasalah);
    }
}

和project.java

public class Project {

    private int id_project;
    private String project;

    public Project (int id_project, String project) {
        this.id_project = id_project;
        this.project = project;
    }


    public int getId() {
        return id_project;
    }

    public String getProject() {
        return project;
    }


}

2 个答案:

答案 0 :(得分:0)

尝试

@Override
    public void onBindViewHolder(ProjectViewHolder holder, int position) {
        Project project = projectList.get(position);
        holder.textViewProject.setText(project.getProject());


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null){
                    int position = holder.getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION){
                        mListener.onItemClick(position);

                    }
                }
            }
        });

    }

答案 1 :(得分:0)

发生这种情况是因为您是在 ViewHolder 类中而不是在 onBindViewHolder 中设置了侦听器。由于Viewholder被回收,因此在滚动后不会创建任何新对象。创建的对象的点击侦听器绑定到首先创建它的项目。

如Vishrut所述,您应该将侦听器移到 onBindViewHolder