Android:如何使用适配器设置标签

时间:2016-07-10 10:30:38

标签: java android switch-statement getter-setter

我遇到以下情况:我有PostBaseAdapter我初始化我的观点和MainActivity我要用getTag()设置带有切换参数的Checkbox,但我不知道如何{ {1}}我的复选框和位置。

setTag()

和MainActivity:

public class PostBaseAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;
    private ArrayList<PostValue> postValueArrayList;

    public PostBaseAdapter(Context context, ArrayList<PostValue> postValueArrayList) {
        this.layoutInflater = LayoutInflater.from(context);
        this.postValueArrayList = postValueArrayList;
    }

    @Override
    public int getCount() {
        return postValueArrayList.size();
    }

    @Override
    public PostValue getItem(int position) {
        return postValueArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_item_post, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        PostValue postValue = getItem(position);

        viewHolder.tvTitle.setText(postValue.getBrand());
        viewHolder.tvPublishDate.setText(postValue.getDate());
        viewHolder.cost.setText(postValue.getPrice());
        viewHolder.descriptionOfItem.setText(postValue.getDescription());
        viewHolder.deliveryTime.setText("Cрок доставки: "+postValue.getDeliveryTime()+" дней");
        viewHolder.checkBox.setChecked(false);

        return convertView;
    }

    private class ViewHolder {
        Button order;
         CheckBox checkBox;
        TextView tvTitle, tvPublishDate, cost, deliveryTime, descriptionOfItem;

        public ViewHolder(View item) {

            tvTitle = (TextView) item.findViewById(R.id.tvTitle);
            tvPublishDate = (TextView) item.findViewById(R.id.tvPublishDate);
            cost = (TextView) item.findViewById(R.id.cost);
            deliveryTime = (TextView) item.findViewById(R.id.delivery_time);
            descriptionOfItem = (TextView) item.findViewById(R.id.description);
            checkBox = (CheckBox) item.findViewById(R.id.checkbox);
            order = (Button) item.findViewById(R.id.order);
        }
    }
}

0 个答案:

没有答案