如何从android中的listview获取所选复选框上的文本

时间:2017-04-16 04:57:40

标签: android listview checkbox

我正在研究android示例,当我点击复选框时,它每次都会获得listview中第一项(位置)的textviews值。所以,但我想获得所选(位置)复选框textview值的值。如何解决它请帮助。我是一个更新鲜的人。谢谢你的进步。

BaseAdapter类中的一些代码

 public View getView(int position, View convertView, ViewGroup parent)
{
    ViewItem viewItem = null;
    if(convertView == null)
    {
        viewItem = new ViewItem();
        LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        //LayoutInflater layoutInfiater = LayoutInflater.from(context);
        convertView = layoutInfiater.inflate(R.layout.list_adapter_view, null);

        viewItem.txtTitle = (TextView)convertView.findViewById(R.id.inactivelistview);
       // viewItem.txtDescription = (TextView)convertView.findViewById(R.id.adapter_text_description);
        convertView.setTag(viewItem);
    }
    else
    {
        viewItem = (ViewItem) convertView.getTag();
    }

    viewItem.txtTitle.setText(valueList.get(position).username);
   // viewItem.txtDescription.setText(valueList.get(position).cources_description);

    return convertView;
}

活动中的一些代码

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

    listCollege = (ListView)findViewById(R.id.listCollege);
    proCollageList = (ProgressBar)findViewById(R.id.proCollageList);

    checkbox = (CheckBox)findViewById(R.id.checkbox_me);

    button  =(Button)findViewById(R.id.button1);
    new GetHttpResponse(this).execute();
}


public void onCheckboxClicked(View view) {

    boolean checked = ((CheckBox) view).isChecked();

    switch(view.getId()) {
        case R.id.checkbox_me:
            if (checked) {

                username = (TextView)findViewById(R.id.inactivelistview);

                Username =username.getText().toString();
                System.out.println("print username_=== "+Username);
                AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

                alertbox.setMessage("Do you want activate "+Username+"?");

                alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(getApplicationContext(), Username+" acivated", Toast.LENGTH_SHORT).show();
                    }

                });

                alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show();
                    }
                });
               alertbox.show();
            } else
                break;
    }

我每次都会在listview中获得第一项(位置)的textviews值。 请帮我解决。帮助我更新我的代码 非常感谢.......

3 个答案:

答案 0 :(得分:0)

请注意,复选框返回数组作为用户可以勾选多个元素。

ArrayList<String> selectedStrings = new ArrayList<String>();

answer详细描述了它。

答案 1 :(得分:0)

在onCheckboxClicked方法上使用gettag,以便您可以识别单击哪个行号复选框。那么你可以得到该行的textview使用行号

答案 2 :(得分:0)

如果CheckBox位于ListView,则无需在Activity中创建ClickListener。按照以下步骤从ListView获取所选文字。

首先创建一个模型/ pojo类,这将帮助您存储所选CheckBox的引用以及将在ListView中显示的值。

public class MyModel {

  private boolean isSelected;

  private String name;

   public MyModel(boolean isSelected, String name) {
      this.isSelected = isSelected;
      this.name = name;
    }

   public boolean isSelected() {
     return isSelected;
   }

   public void setSelected(boolean selected) {
    this.isSelected = selected;
   }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

创建一个可用作回调的界面。

public interface ItemSelectListener {
   void getSelectedItemText(String text);
}

重构你的BaseAdapter,现在它将接受我们的Model类列表以及回调监听器。

public MyAdapter extends BaseAdapter {

    private final List<MyModel> mDataItems;

    private final ItemSelectListener mItemListener;

    public MyAdapter(List<MyModel> dataItems, ItemSelectListener itemListener)
    mDataItems = dataItems;
    mItemListener = itemListener;
   }

   public View getView(int position, View convertView, ViewGroup parent)
   {
        ViewItem viewItem = null;
        if(convertView == null) {
            viewItem = new ViewItem();
            LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
           //LayoutInflater layoutInfiater = LayoutInflater.from(context);
           convertView = layoutInfiater.inflate(R.layout.list_adapter_view, null);

           viewItem.txtTitle = (TextView)convertView.findViewById(R.id.inactivelistview);

       // Add checkbox in your view item and confirm id of checkbox
       viewItem.checkBox = (CheckBox)convertView.findViewById(R.id. checkbox_me);
       }
       else {
           viewItem = (ViewItem) convertView.getTag();
       }
       final MyModel data = mDataItems.get(position);
       viewItem.txtTitle.setText(data.getName());
       viewItem.checkBox.setChecked(data.isSelected());
       viewItem.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton button, boolean checked) 
        {
            data.setSelected(checked);
            if(checked) {
             // Make sure you override this in your Activity
             mItemListener.getSelectedItemText(data.getName());
            }
        }
    });

   return convertView;
   }

}

ListView

中为Activity创建数据源
BaseAdapter adapter = new BaseAdapter(dataItems, itemListener);