检索RecyclerView内的数据

时间:2017-08-20 14:32:54

标签: android android-recyclerview android-adapter recycler-adapter recyclerview-layout

我想在Recycler View中创建此卡片视图。

我遇到了这个问题。我无法检索卡片视图中的数据。

我希望每当Check Box为真时,获取数据(此示例中的膳食,价格和计数(1))并添加到我的主要活动中的列表中。

我坚持了6个小时。如果有人知道解决方案请帮助。我在这里死了。

CardView

MainActivity:

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    CardData data;
    List<CardData> list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Hawk.init(getApplicationContext()).build();

        data = new CardData("Meal","1","12","http");
        list.add(data);

        recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setHasFixedSize(true);

        RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        final RecyclerView.Adapter adapter=new MyAdapter(getApplicationContext(),list);
        recyclerView.setAdapter(adapter);



    }
}

适配器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private List<CardData> data;


    private Context context;
    public MyAdapter(Context context,List<CardData> data){
        this.data=data;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_contents,viewGroup,false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder myViewHolder, final int i) {



        myViewHolder.Meal.setText(data.get(i).Yemek);
        myViewHolder.price.setText(data.get(i).Qiymet);
        //Glide.with(context).load(data.get(i).url).into(myViewHolder.img);

        myViewHolder.count.getText();

        myViewHolder.card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                myViewHolder.checkBox.setChecked(!myViewHolder.checkBox.isChecked());
                data.get(i).checked = myViewHolder.checkBox.isChecked();
            }
        });


        myViewHolder.add.setFocusable(true);
        myViewHolder.remove.setFocusable(true);

        myViewHolder.add.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                String countS = myViewHolder.count.getText().toString();
                int countI = Integer.valueOf(countS);
                countI += 1;

                String countN = String.valueOf(countI);
                myViewHolder.count.setText(countN);

            }
        });

        myViewHolder.remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String sayS = myViewHolder.count.getText().toString();
                int sayI = Integer.valueOf(sayS);
                if (sayI>0){
                sayI -= 1;}

                String sayN = String.valueOf(sayI);
                myViewHolder.count.setText(sayN);

            }
        });


    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{

        TextView Meal;
        TextView price;
        TextView count;
        CardView card;

        ImageView img;

        CheckBox checkBox;

        Button add;
        Button remove;
        MyViewHolder(View view){
            super(view);
            this.Meal = (TextView) view.findViewById(R.id.Meal);
            this.price = (TextView) view.findViewById(R.id.Cost);
            this.count = (TextView) view.findViewById(R.id.Count);
            this.add = (Button) view.findViewById(R.id.addButton);
            this.remove = (Button) view.findViewById(R.id.removeButton);
            this.checkBox = (CheckBox) view.findViewById(R.id.checkBox);
            this.img = (ImageView) view.findViewById(R.id.img);
            this.card = (CardView) view.findViewById(R.id.card_view);


        }
    }
}

数据:

public class CardData {

    public CardData() {
    }

    public String Meal;
    public String Portion;
    public String Cost;
    public String url;
    public Boolean checked;

    public CardData(String meal, String portion, String cost, String url) {
        Meal = meal;
        Portion = portion;
        Cost = cost;
        this.url = url;
    }

    public Boolean isChecked() {
        return checked;
    }

    public void setChecked(Boolean checked) {
        this.checked = checked;
    }


    public String getMeal() {
        return Meal;
    }

    public void setMeal(String meal) {
        Meal = meal;
    }

    public String getPortion() {
        return Portion;
    }

    public void setPortion(String portion) {
        Portion = portion;
    }

    public String getCost() {
        return Cost;
    }

    public void setCost(String cost) {
        Cost = cost;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

0 个答案:

没有答案