如何使用意图将模型类型列表从适配器传递到另一个活动 [英] How to pass model type lists using intents from the adapter to another activity

查看:57
本文介绍了如何使用意图将模型类型列表从适配器传递到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个部门列表,其中包含部门名称,例如,男装,女士等.现在,如果我单击女士",则应显示下一个屏幕,其中包含与该部门相关的类别列表女士们 . 使用我目前的代码,仅显示类别列表中的第一项.这是我的部门适配器代码:

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

}

这是我的类别课程:

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.

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

在这里您要初始化 item字符串数组,其大小为model_list **.这没有任何意义.您的到达人数应为**可用类别的大小,在这里绝对没有人知道 category 会持续多久 model_list可能有多少匹配类别.因此,简单数组不是一个好主意.您需要使用ArrayList.自定义模式的ArrayList.这里的自定义模型"将是您的类别模式.就像下面提到的

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

List<CategoryModel> categoryList = new ArrayList();

在单击偶数时,您需要获取有关单击哪个位置项的位置.然后,您需要获取该位置的模型对象.获取模型后,您需要找到实际的类别.对于类别,您需要选择该类别的一个属性,例如名称或ID" .现在,这将是您的过滤点.现在过滤您的 model_list (如果这也是包含您的类别的列表.我可以看到它是部门列表".如果此部门列表也包含类别,那么您需要使用列表来过滤列表) .要过滤,您需要使用以下代码.

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.

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

现在,您可以对列表进行任何操作了.

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

这篇关于如何使用意图将模型类型列表从适配器传递到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆