I have a Department list that contains the department names, for example, Mens, Ladies, etc. Now if suppose, I click on 'Ladies', the next screen should be displayed with the categories list that are correlated to the Department Ladies . With my present code, only the first item on the category list is displayed. This is my code for my Department Adapter:
public class Department_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static Context context;
private ArrayList<Department_model> arraylist;
private List<Department_model> models_list;
public Department_Adapter(Context context, List<Department_model> department_modelList) {
this.models_list = department_modelList;
this.arraylist = new ArrayList<Department_model>();
this.arraylist.addAll(department_modelList);
this.context = context;
}
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.departments_childview, viewGroup, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
final ViewHolder myViewHolder = (ViewHolder)viewHolder;
final Department_model model = models_list.get(i);
myViewHolder.department_text.setText(model.getDepartment_Name());
myViewHolder.relative_lay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, Categories.class);
String items[] = new String[models_list.size()];
for(int j=0; j< items.length; j++) {
intent.putExtra("category_name", models_list.get(i).Category_Name);
intent.putExtra("category_id", models_list.get(i).Category_ID);
intent.putExtra("subCatName", models_list.get(i).Sub_Cat_Name);
intent.putExtra("subcatID", models_list.get(i).Sub_Cat_ID);
intent.putExtra("Article_ID", models_list.get(i).Article_ID);
intent.putExtra("Article_Number", models_list.get(i).Article_Number);
intent.putExtra("ArticleWSP", models_list.get(i).ArticleWSP);
intent.putExtra("department_name", models_list.get(i).Department_Name);
}
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return models_list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView department_text;
ImageView right_arrow;
RelativeLayout relative_lay;
public ViewHolder(@NonNull View itemView) {
super(itemView);
department_text = (TextView) itemView.findViewById(R.id.department_text);
right_arrow = (ImageView) itemView.findViewById(R.id.right_arrow);
relative_lay = (RelativeLayout) itemView.findViewById(R.id.relative_lay);
}
}
}
This is my Category class:
public class Categories extends AppCompatActivity {
RecyclerView category_recyclerView;
TextView category_textview;
ImageView back_arrow;
static Context ctx;
private static List<Category_model> category_modelList;
private static List<String> list;
private Category_Adapter category_adapter;
String cat_name, cat_id, title, subCatName, subcatID, Article_ID, Article_Number, ArticleWSP;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categories);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ctx = Categories.this;
Intent intent = getIntent();
cat_name = intent.getStringExtra("category_name");
cat_id = intent.getStringExtra("category_id");
subCatName = intent.getStringExtra("subCatName");
subcatID = intent.getStringExtra("subcatID");
Article_ID = intent.getStringExtra("Article_ID");
Article_Number = intent.getStringExtra("Article_Number");
ArticleWSP = intent.getStringExtra("ArticleWSP");
title = intent.getStringExtra("department_name");
category_recyclerView = (RecyclerView) findViewById(R.id.category_recyclerView);
category_textview = (TextView) findViewById(R.id.category_textview);
back_arrow = (ImageView) findViewById(R.id.back_arrow);
category_textview.setText(title);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
category_recyclerView.setLayoutManager(mLayoutManager);
category_recyclerView.setItemAnimator(new DefaultItemAnimator());
category_recyclerView.addItemDecoration(new DividerItemDecoration(Categories.this, DividerItemDecoration.VERTICAL));
category_modelList = new ArrayList<>();
Category_model obj1 = new Category_model();
obj1.setCategory_Name(cat_name);
obj1.setCategory_ID(cat_id);
category_modelList.add(obj1);
category_adapter = new Category_Adapter(Categories.this, category_modelList);
category_recyclerView.setAdapter(category_adapter);
back_arrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
How can I pass all the Category list items when clicked on the particular position of the Department list ?
I can see logical problem in your code. This is the part of the code creating problem.
Here you are initializing items string array with size of model_list **.This does not make sense. Your arrray size should be of the size of **Available Category And here definitely no one knows how long will be the last with category The long the model_list It may have any number of matching category. So, simple array is not a good idea. You need to use ArrayList. ArrayList of custom mode. Here Custom Model will be your category mode.Like mentioned below
And on the even of click, you need to get position as to what position item is clicked. And then you need to get model object of that position. And after getting model you need to find actual Category. And with category you need to select one property like 'name or id ' of that category . Now this will be your filter point. Now filter your model_list (if this is list that contains your category as well. As I can see it is Department List. If this department list also contain category then you need to use list to filter the list). To filter you need to use below code.
In Category Activity onCreate() method use below code
And now are done do whatever you want to do with this list.
update after finishing loop, I am checking list size. you need to check the size and please let me know