Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 单击项目时如何选中ListView的复选框?_Android_Checkbox_Android Listview - Fatal编程技术网

Android 单击项目时如何选中ListView的复选框?

Android 单击项目时如何选中ListView的复选框?,android,checkbox,android-listview,Android,Checkbox,Android Listview,单击项目时如何选中ListView的复选框 我有一个带有复选框、文本视图和按钮的列表视图 在这里,我想选中多行ListView和so used复选框。如果我单击一行,我希望选中其对应的复选框,并获取ListView中已单击项的RowID 若我点击按钮,我想打开一个弹出窗口或另一个屏幕(活动),但不能选中复选框 如何做到这一点?请帮忙 提前谢谢 编辑: 这就是我的布局 使用ListView的适配器代码很简单。 public class Adapter extends BaseAdapter

单击项目时如何选中ListView的复选框

我有一个带有复选框、文本视图和按钮的列表视图

在这里,我想选中多行ListView和so used复选框。如果我单击一行,我希望选中其对应的复选框,并获取ListView中已单击项的RowID

若我点击按钮,我想打开一个弹出窗口或另一个屏幕(活动),但不能选中复选框

如何做到这一点?请帮忙

提前谢谢

编辑:

这就是我的布局

使用ListView的适配器代码很简单。

   public class Adapter extends BaseAdapter{

    private Context context;
    Button btnView;
    CheckBox box;       
    ArrayList<String> altextView1;
    ArrayList<String> altextView2;


    public Adapter(Context c, Button view, CheckBox checkBox, ArrayList<String> textView1, ArrayList<String> textView2) {

        this.context=c;
        this.btnView=view;
        this.box=checkBox;
        this.altextView1=textView1;
        this.altextView2=textView2;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return altextView1.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub

        View view=arg1;
        view=LayoutInflater.from(context).inflate(R.layout.list7, null);

        TextView tv1=(TextView)view.findViewById(R.id.tV1);
        TextView tv2=(TextView)view.findViewById(R.id.tV2);
        btnView=(Button)findViewById(R.id.btnView);
        box=(CheckBox)findViewById(R.id.cB);

        tv1.setText(altextView1.get(arg0));
        tv2.setText(altextView2.get(arg0));


        return view;
    }

}
公共类适配器扩展了BaseAdapter{
私人语境;
按钮btnView;
复选框;
ArrayList altextView1;
ArrayList altextView2;
公共适配器(上下文c、按钮视图、复选框、ArrayList textView1、ArrayList textView2){
context=c;
this.btnView=视图;
this.box=复选框;
this.altextView1=textView1;
this.altextView2=textView2;
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回altextView1.size();
}
@凌驾
公共对象getItem(int arg0){
//TODO自动生成的方法存根
返回null;
}
@凌驾
公共长getItemId(int arg0){
//TODO自动生成的方法存根
返回0;
}
@凌驾
公共视图getView(int arg0、视图arg1、视图组arg2){
//TODO自动生成的方法存根
视图=arg1;
视图=LayoutFlater.from(上下文)。充气(R.layout.list7,空);
TextView tv1=(TextView)view.findViewById(R.id.tv1);
TextView tv2=(TextView)view.findViewById(R.id.tv2);
btnView=(按钮)findViewById(R.id.btnView);
框=(复选框)findViewById(R.id.cB);
tv1.setText(altextView1.get(arg0));
setext(altextView2.get(arg0));
返回视图;
}
}
  • 将复选框对象设置为行视图的标记,该行视图可能是适配器的getView()方法中的“convertView”

  • 在行视图上单击“编写侦听器”

  • 在该侦听器中,单击onClick方法中作为参数的视图中的行视图getTag,并将其强制转换为checkbox,然后将该checkbox对象的setChecked设置为true

  • 代码可能是这样的

            convertView.setTag(yourCheckBoxObject);
            convertView.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v.getTag();
                    cb.setChecked(true);
                }
            });
    


    你需要按照这个思路来学习。。只需阅读整个线程并尝试理解,然后实现。。这是你能得到的最好的帮助

    首先,您需要在Java代码中调用ListView上的setChoiceMode()来设置 选择模式,提供选择模式单或选择模式多作为 价值您可以通过getListView()从ListActivity获取ListView。你可以 还可以通过布局XML中的android:choiceMode属性来声明这一点

    然后,而不是使用android.R.layout.simple_list_item_1作为 列出ArrayAdapter构造函数中的行,您需要使用

        android.R.layout.simple_list_item_single_choice 
    

    单选还是单选 分别是多项选择列表

        <?xml version="1.0" encoding="utf-8"?>
        <ListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"
        android:choiceMode="multipleChoice"
        />
    
    
    
    java代码可以是

        package com.commonsware.android.checklist;
    
        import android.app.ListActivity;
        import android.os.Bundle;
        import android.widget.ArrayAdapter;
    
        public class ChecklistDemo extends ListActivity {
        private static final String[] items={"lorem", "ipsum", "dolor",
        "sit", "amet","consectetuer", "adipiscing", "elit", "morbi", "vel",
        "ligula", "vitae", "arcu", "aliquet", "mollis",
        "etiam", "vel", "erat", "placerat", "ante",
        "porttitor", "sodales", "pellentesque", "augue", "purus"};
    
        @Override
        public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
    
        setListAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_multiple_choice,items));
          }
        }
    
    package com.commonware.android.checklist;
    导入android.app.ListActivity;
    导入android.os.Bundle;
    导入android.widget.ArrayAdapter;
    公共类ChecklistDemo扩展了ListActivity{
    私有静态最终字符串[]项={“lorem”、“ipsum”、“dolor”,
    “坐”、“艾米特”、“康塞特图尔”、“阿迪皮西奇”、“精英”、“摩比”、“维尔”,
    “ligula”、“vitae”、“arcu”、“aliquet”、“mollis”,
    “etiam”、“vel”、“erat”、“Placelat”、“ante”,
    “波尔蒂托”、“苏打水”、“佩伦茨克”、“奥古斯”、“普卢斯”};
    @凌驾
    创建公共空间(捆绑冰柱){
    超级冰柱;
    setContentView(R.layout.main);
    setListAdapter(新阵列适配器)(此,
    android.R.layout.simple_list_item_多项选择,items);
    }
    }
    
    在自定义适配器中,您可以这样做

     @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
    
        Button textView = (Button) layoutInflater.inflate(R.layout.btn, null);
        textView.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                           //start activity or open pop-up
            }
        });
       ........
    
      }
    
    然后在OnlistItem中单击callback:

    protected void onListItemClick(ListView l, android.view.View v, int position, long id) {
       // using view v get check box and make it checked
       CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkbx);
    
    };
    

    希望这能起作用。对不起,我没有测试。请告诉我结果

    你有任何可用的代码吗?请仔细阅读我希望这将对你有所帮助:)示例:这个线程可能也会有帮助@Sravani,然后直接传递checkbox对象..很简单。!是的,明白了。但当我再次点击时,它不会再次被取消选中吗?在哪里更改逻辑?选择项目后,我无法获取项目位置。另外,当我向下滚动列表时,以前选中的复选框将被取消选中。@Sravani您需要按照此线程学习。。只需阅读整个线程并尝试理解,然后实现。。这是你能得到的最好的帮助。是的,它工作,您可能会在滚动时遇到问题,一旦单击它被选中,然后滚动它被取消选中。。如果这是问题所在。。您应该为所选值@YogGuru维护一个数组
    protected void onListItemClick(ListView l, android.view.View v, int position, long id) {
       // using view v get check box and make it checked
       CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkbx);
    
    };