如何从自定义列表视图中获取所选行

时间:2011-07-07 12:05:10

标签: android list view adapter

我创建了像这样的自定义适配器

公共类SettingsAdapter扩展了BaseAdapter {

ArrayList<String> Noptions = new ArrayList<String>();
Context mContext;   
LayoutInflater inf;

public SettingsAdapter(Context c, ArrayList<String> options){
    this.mContext = c;
    this.Noptions = options;
    inf = LayoutInflater.from(mContext);
}

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

@Override
public Object getItem(int position) {
    return Noptions.get(position);
}

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

static class ViewHolder2{
    TextView name;
    CheckBox cb;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder2 holder;
    if(convertView == null){
        convertView = inf.inflate(R.layout.settings_row, null);
        holder = new ViewHolder2();
        holder.name = (TextView)convertView.findViewById(R.id.optionName);
        holder.cb = (CheckBox)convertView.findViewById(R.id.chooseOptionName);
        convertView.setTag(holder);
    }
    else{
        holder = (ViewHolder2)convertView.getTag();
    }

    holder.name.setText(Noptions.get(position).toString());

    holder.cb.setChecked(false);


    return convertView;
}

}

有人可以向我解释如何仅在选中复选框的行中获取文本吗?!!!?任何帮助将非常感激。

1 个答案:

答案 0 :(得分:0)

请尝试以下代码

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder2 holder;
        if(convertView == null){
            convertView = inf.inflate(R.layout.settings_row, null);
            holder = new ViewHolder2();
            holder.name = (TextView)convertView.findViewById(R.id.optionName);
            holder.cb = (CheckBox)convertView.findViewById(R.id.chooseOptionName);
            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder2)convertView.getTag();
        }

        holder.name.setText(Noptions.get(position).toString());

        holder.cb.setChecked(false);

        holder.cb.setTag(Noptions.get(position).toString());
        holder.cb.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String txtvalue=  v.getTag().toString();
                // toast "txtvalue" value
            }
        });

        return convertView;
    }