How to pass model type lists using intents from the adapter to another activity

落花浮王杯 提交于 2020-01-17 01:18:06

问题


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 ?


回答1:


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

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

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

In Category Activity onCreate() method use below code

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



来源:https://stackoverflow.com/questions/53551671/how-to-pass-model-type-lists-using-intents-from-the-adapter-to-another-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!