Я новичок в программировании и сейчас разрабатываю приложение для домашней работы.
Мое приложение похоже на фильм в жанре списка, при щелчке на котором оно отображается в фильме со списком других действий в соответствии с жанром. Получение жанра, фильма по ссылке в формате JSON с использованием Volley.
Затем анализирует 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);
}
}