通过单击项目

时间:2019-01-12 15:06:56

标签: java android mysql android-recyclerview filtering

我是编程方面的新手,现在正在为我的家庭作业开发一个应用程序。

我的应用程序就像一个列表类型的电影,单击时它会根据类型显示在其他活动列表电影中。使用Volley从JSON格式的链接中获取流派,电影。

然后解析JSON并在屏幕顶​​部显示电影列表和EditText,以便搜索电影。单击流派电影,然后转到另一个活动并根据流派单击显示电影列表,一切都很好。

我的问题是,当使用edittext搜索电影类型时,当我单击搜索结果时,在下一个活动中显示的数据与单击的内容不匹配。

Project.java

public class Problem {
    private int id;
    private String masalah;
    private String project;
    private String gambar_error;

    public Problem(int id, String masalah, String project, String gambar_error) {
        this.id = id;
        this.masalah = masalah;
        this.project = project;
        this.gambar_error = gambar_error;
    }


    public int getId() {
        return id;
    }

    public String getMasalah() {
        return masalah;
    }

    public String getProject() {
        return project;
    }

    public String getGambar_error() {
        return gambar_error;
    }

}

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);
    }

}

ProjectAdapter.java

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);

                        }
                    }
                }
            });

        }
    }

}

ListproblemActivity.java

import static com.bangun.androidregisterandlogin.ListprojectActivity.project_select;

public class ListproblemActivity extends AppCompatActivity {




    EditText editTextCari;

    //a list to store all the products
    List<Problem> problemList;


    private String Listproblem;

    //the recyclerview
    RecyclerView recyclerView;

    ProblemsAdapter adapter;

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

        Intent intent = getIntent();
        String project = intent.getStringExtra(project_select);


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

        textViewProject.setText(project);
        String  URL_PRODUCTS = "http://192.168.43.245/android_register_login/single_data.php?project=" + project;

        //getting the recyclerview from xml
        recyclerView = findViewById(R.id.recylcerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        editTextCari = findViewById(R.id.EditTextCari);

        //initializing the productlist
        problemList = new ArrayList<>();
        List<Problem> output = new ArrayList<Problem>();


            /*
             * 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 problem = array.getJSONObject(i);

                                    //adding the product to product list

                                    problemList.add(new Problem(
                                            problem.getInt("id"),
                                            problem.getString("masalah"),
                                            problem.getString("project"),
                                            problem.getString("gambar_error")
                                    ));
                                }

                                //creating adapter object and setting it to recyclerview
                                ProblemsAdapter adapter = new ProblemsAdapter(ListproblemActivity.this, problemList);
                                recyclerView.setAdapter(adapter);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    });

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


    }

0 个答案:

没有答案