多选扩展的recyclerview listview跟踪选择

时间:2016-07-20 21:37:55

标签: android listview checkbox android-recyclerview multi-select

我可以使用以下库创建扩展视图:https://github.com/bignerdranch/expandable-recycler-view

现在我已经拥有了这个,我在每个子视图中都创建了一个复选框。我可以轻松检查和取消选中,展开和收回。然而,下一部分是跟踪所做的所有选择并将其放在一个对象中。

例如,我想要玉米饼的食谱,其中含有牛肉,奶酪,生菜的成分。但我选择只想要奶酪和生菜。现在这些新的选择我的Taco成​​分我想把它作为一个对象。

所以我所做的就是在我的活动中,我创建了一个准备选择方法,当我选择了一个复选框时,将从我的视图中调用onClick。

起初我只想尝试一个简单的计数器并显示它只是为了知道它的工作原理。但是,当我单击复选框时,我收到空指针错误。

活动

public class CreateMyTeamActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {



    private RecyclerView mRecyclerView;
    private CustomRecyclerAdapterCreateTeam adapter;
    private RecipeAdapter mAdapter;



    List<Recipe> recipes = new ArrayList<>();
    ArrayList<Integer> selection_list = new ArrayList<>();
    int count = 0;






    Context context;

    private static final String TAG = "MyActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_my_team);

        context = this;

        mRecyclerView = (RecyclerView) findViewById(R.id.recycleView);

        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);

        Ingredient beef = new Ingredient("beef");
        Ingredient cheese = new Ingredient("cheese");
        Ingredient salsa = new Ingredient("salsa");
        Ingredient tortilla = new Ingredient("tortilla");
        Ingredient ketchup = new Ingredient("ketchup");
        Ingredient bun = new Ingredient("bun");


        Recipe taco = new Recipe("taco", Arrays.asList(beef, cheese, salsa, tortilla));
        Recipe quesadilla = new Recipe("quesadilla", Arrays.asList(cheese, tortilla));
        Recipe burger = new Recipe("burger", Arrays.asList(beef, cheese, ketchup, bun));



        recipes = Arrays.asList(taco, quesadilla, burger);



        mAdapter = new RecipeAdapter(this, recipes);






        mAdapter.setExpandCollapseListener(new ExpandableRecyclerAdapter.ExpandCollapseListener() {
            @Override
            public void onListItemExpanded(int position) {
                Recipe expandedRecipe = recipes.get(position);



                Toast.makeText(CreateMyTeamActivity.this,
                        expandedRecipe.getName(),
                        Toast.LENGTH_SHORT)
                        .show();
            }

            @Override
            public void onListItemCollapsed(int position) {
                Recipe collapsedRecipe = recipes.get(position);


                Toast.makeText(CreateMyTeamActivity.this,
                        "Collapse",
                        Toast.LENGTH_SHORT)
                        .show();
            }
        });
        mRecyclerView.setAdapter(mAdapter);




    }

   -------------Right here is my prepareSelection Method

    public void prepareSelection (View view, int position ){

            if (((CheckBox)view).isChecked()){

                selection_list.add(recipes.indexOf(position));
                count = count+1;

                Toast.makeText(CreateMyTeamActivity.this,
                        count,
                        Toast.LENGTH_SHORT)
                        .show();

            }

    }



}

成分观点持有人

    public class IngredientViewHolder extends ChildViewHolder implements View.OnClickListener{

        private TextView mIngredientTextView;
        private CheckBox button;
        CreateMyTeamActivity createMyTeamActivity;



        public IngredientViewHolder(View itemView) {
            super(itemView);
            mIngredientTextView = (TextView) itemView.findViewById(R.id.teamNameChild);
            button = (CheckBox)itemView.findViewById(R.id.checkBoxChild);

            button.setOnClickListener(this);
        }

        public void bind(Ingredient ingredient) {
            mIngredientTextView.setText(ingredient.getName());
        }


    @Override
    public void onClick(View v) {

        Toast.makeText(itemView.getContext(),mIngredientTextView.getText(),Toast.LENGTH_SHORT ).show();

        createMyTeamActivity.prepareSelection(v,getAdapterPosition() );


    }
}

Ingredient.java

public class Ingredient {

    private String mName;

    public Ingredient(String name) {
        mName = name;
    }

    public String getName() {
        return mName;
    }
}

0 个答案:

没有答案