在android中点击按钮动态添加新的cardview

时间:2015-11-05 08:30:17

标签: android

我正在使用带有recyclerview的cardview和硬编码值,例如字符串数组。但是现在我想在每个按钮点击时添加新的cardview,用户输入值和用户选择图像并保持应用程序退出时的所有cardview。我的意思是说,cardview应该只是逐个添加。任何解决方案PLZ指导我。< / p>

cardview适配器类

public class CardViewDataAdapter extends RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {

private static ArrayList<FeddProperties> dataSet;
private static Context context;

public CardViewDataAdapter(Context context, ArrayList<FeddProperties> os_versions) {
    this.context = context;
    dataSet = os_versions;
}


@Override
public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

    View itemLayoutView = LayoutInflater.from(viewGroup.getContext()).inflate(
            R.layout.card_view, null);

    // create ViewHolder

    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;
}

@Override
public void onBindViewHolder(CardViewDataAdapter.ViewHolder viewHolder, int i) {

    FeddProperties fp = dataSet.get(i);

    viewHolder.vehicleNumber.setText(fp.getTitle());
    viewHolder.iconView.setImageResource(fp.getThumbnail());
    viewHolder.feed = fp;
}

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


public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView vehicleNumber;
    public ImageView iconView;
    public FeddProperties feed;

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);

        vehicleNumber = (TextView) itemLayoutView
                .findViewById(R.id.vehiclenumber);
        iconView = (ImageView) itemLayoutView
                .findViewById(R.id.iconId);

        itemLayoutView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), InformingUser.class);
                v.getContext().startActivity(intent);

                ((MainActivity) context).getSupportFragmentManager().beginTransaction().replace
                        (R.id.containerView, new InformingUser()).commit();

                //Toast.makeText(v.getContext(), "os version is: " + feed.getTitle(), Toast.LENGTH_SHORT).show();
            }
        });
    }

}

Java文件....

private void initContrls()
{
    SharedPreferences prefs = context.getSharedPreferences(MY_PREFS_NAME, 0);
    vehiclecatory = prefs.getString("vehicle_category", "");
    vehicletype = prefs.getString("vehicle_type", "");

    String versions =  prefs.getString("city", "")+" "+prefs.getString("dis", "")+" "+prefs.getString("number", "");
    String vehicleCompany = prefs.getString("company", "")+" "+prefs.getString("model", "");

    if(vehiclecatory.equals("1"))
    {
        if (vehicletype.equals("1"))
        {
            icons = R.drawable.contwowheel;
        }
        else if (vehicletype.equals("2"))
        {
            icons = R.drawable.comfourwheel;
        }

        else if (vehicletype.equals("3"))
        {
            icons = R.drawable.comheavy;
        }
    }
    else if (vehiclecatory.equals("2"))
    {
        if (vehicletype.equals("1"))
        {
            icons = R.drawable.nontwowheel;
        }
        else if (vehicletype.equals("2"))
        {
            icons = R.drawable.nonfourwheel;
        }
        else if (vehicletype.equals("3"))
        {
            icons = R.drawable.nonheavy;
        }
    }

    os_versions = new ArrayList<FeddProperties>();
    for (int i = 0; i < 2; i++) {
        FeddProperties feed = new FeddProperties();

        feed.setTitle(versions);
        feed.setVehicleCompany(vehicleCompany);
        feed.setThumbnail(icons);
        os_versions.add(feed);
    }
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    mAdapter = new CardViewDataAdapter(context,os_versions);
    recyclerView.setAdapter(mAdapter);
}

1 个答案:

答案 0 :(得分:0)

here有一个例子可以动态添加cardview 在这个例子中,cardview是用for:

创建的
for (int index = 0; index < 20; index++) {
            DataObject obj = new DataObject("Some Primary Text " + index,
                    "Secondary " + index);
            results.add(index, obj);
        }

但你可以进入onClick动作:)