我有一个部门列表,其中包含部门名称,例如,男士,女士等。现在,如果我单击“女士”,则下一个屏幕应显示与该部门相关的类别列表女士们。 使用我当前的代码,仅显示类别列表中的第一项。这是我的部门适配器代码:
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);
}
}
}
这是我的类别课程:
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();
}
});
}
}
单击“部门”列表的特定位置时如何传递所有“类别”列表项?
答案 0 :(得分:1)
我可以在您的代码中看到逻辑问题。这是代码创建问题的一部分。
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);
在这里,您要初始化大小为model_list **的项字符串数组。这没有任何意义。您的到达人数应为**可用类别的大小,在这里绝对没有人知道 category 会持续多久 model_list可能有多少匹配类别。因此,简单数组不是一个好主意。您需要使用ArrayList。自定义模式的ArrayList。自定义模型将作为您的类别模式。就像下面提到的
List<CategoryModel> categoryList = new ArrayList();
在单击偶数时,您需要获取有关单击哪个位置项的位置。然后,您需要获取该位置的模型对象。获取模型后,您需要找到实际的类别。对于类别,您需要选择该类别的一个属性,例如“名称或ID” 。现在,这将是您的过滤点。现在过滤您的 model_list (如果这也是包含您的类别的列表。我可以看到它是“部门列表”。如果此部门列表也包含类别,那么您需要使用列表来过滤列表) 。要过滤,您需要使用以下代码。
Intent intent = new Intent(context, Categories.class);
List<CategoryModel> categoryList = new ArrayList<>();
//you need to get position of clicked item and that is stored in variable 'i'
Department_model selectedModel = model_list.get(i);
//here you have option you can filter with or the id let's suppose you are filtering with name then
// get name of category
String filterTag = selectedModel.Category_Name;
for(int j=0; j< model_list.size(); j++) {
//you need make category model with getter and setter properties
if (filterTag.equalsIgnoreCase(selectedModel.Category_Name)) {
CategoryModel currentCategory = new CategoryModel();
currentCategory.set(Category_Name);
currentCategory.set(Category_ID);
currentCategory.set(Sub_Cat_Name);
currentCategory.set(Sub_Cat_ID);
currentCategory.set(Article_ID);
currentCategory.set(Article_Number);
currentCategory.set(ArticleWSP);
currentCategory.set(Department_Name);
//here you have mapped category stuff in category model now you need to add in the list
categoryList.add(currentCategory);
}
}
Log.e("list_size",categoryList.size() );
//after loop, you need to pass "categoryList" to the next activity and then use the same categoryList
//to new activity of category adapter and now you are done ... phewww
//you can use below code to pass list via intent
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)categoryList);
intent.putExtra("BUNDLE",args);
context.startActivity(intent);
在类别活动的onCreate()方法中,使用以下代码
Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");
List<CategoryModel> list = args.getSerializable("ARRAYLIST");
现在,您可以对列表进行任何操作了。
更新,在完成循环后,我正在检查列表大小。您需要检查尺寸,并让我知道