如何设置复选框为选中当我点击列表? [英] how to set checkbox to checked when i click on list?

查看:240
本文介绍了如何设置复选框为选中当我点击列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个名单,我有这个名单的每一行中的复选框所以我要的是,当我点击复选框应该相应地检查任何行

这是我的code ...

checkbox.xml

 <复选框的android:文本=
                  机器人:ID =@ + ID /复选框
                  机器人:layout_width =WRAP_CONTENT
                  机器人:layout_height =WRAP_CONTENT
                  机器人:可聚焦=假
                  />
 

点击监听我的列表

 保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){


         Toast.makeText(getApplicationContext(),您选择的项目没有。
                 +(位置+ 1)+,Toast.LENGTH_SHORT).show();

        super.onListItemClick(L,V,位置ID);
        selectedRow =位置;


        如果(V == NULL){
            Log.v(MyLog,V为空);
            LayoutInflater LIF =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            V = lif.inflate(R.layout.posting_detail_question_row,NULL);


        }
        / *如果(V!= NULL){

            checkedTextView =(CheckedTextView)v.findViewById(R.id.checkedTextBox);
            如果(checkedTextView.isChecked())

                checkedTextView.setChecked(假);
            其他
                checkedTextView.setChecked(真正的);

        } * /



        如果(checkedTextView.isChecked())
            Toast.makeText(getApplicationContext(),已历经投票,
                    Toast.LENGTH_SHORT).show();


        其他 {
            // checkedTextView.setChecked(真正的);

                       //我的code



从字符串= {选项,票} []; //值从取
                                                        //散列表
                INT []到= {R.id.option,R.id.votes}; // ID的视图

            SimpleAdapter ADP =新SimpleAdapter(getApplicationContext()
                    hashList,R.layout.posting_detail_question_row,从,到);
            Log.v(MyLog,简单的适配器后);

            setListAdapter(ADP);


            checkedTextView.setChecked(真正的);
        }

        }
 

posting_detail_question_row.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直机器人:layout_width =FILL_PARENT
    机器人:layout_height =40dp>

    < LinearLayout中的android:layout_height =FILL_PARENT
        机器人:方向=横向
        机器人:背景=@可绘制/ poll_border_top_bg机器人:layout_width =match_parent>

        < TextView的机器人:ID =@ + ID /选项机器人:文字颜色=#333333
            机器人:TEXTSIZE =15sp机器人:layout_width =0dp
            机器人:layout_height =FILL_PARENT机器人:layout_marginLeft =10dp
            3:机器人layout_weight =机器人:文本=答案1安卓重力=center_vertical/>

        < TextView的机器人:ID =@ + ID /票机器人:文字颜色=#333333
            机器人:TEXTSIZE =15sp机器人:layout_width =0dp
            机器人:layout_height =FILL_PARENT机器人:layout_marginLeft =10dp
            机器人:layout_weight =1安卓重力=center_vertical/>

             <复选框的android:文本=
                  机器人:ID =@ + ID /复选框
                  机器人:layout_width =WRAP_CONTENT
                  机器人:layout_height =WRAP_CONTENT
                  机器人:可聚焦=假
                  />




    < / LinearLayout中>


< / LinearLayout中>
 

我的适配器是简单的适配器

  SimpleAdapter ADP =新SimpleAdapter(getApplicationContext()
                    hashList,R.layout.posting_detail_question_row,从,到);
            Log.v(MyLog,简单的适配器后);
            setListAdapter(ADP);
 

解决方案

您需要使用findViewById获得一个指向你的复选框:

 保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){

    Toast.makeText(getApplicationContext(),您选择的项目没有。
                 +(位置+ 1)+,Toast.LENGTH_SHORT).show();

    super.onListItemClick(L,V,位置ID);

    如果(V!= NULL){
        复选框复选框=(复选框)v.findViewById(R.id.C​​heckbox);
        checkBox.setChecked(checkBox.isChecked()!);
    }
}
 

如果您正在recieving onClick事件再 v 不应该为空,但我穿上了相应的检查。

hi i have a list and i have a checkbox in every row of this list so what i want is when i click on any row the checkbox should checked accordingly

here is my code...

checkbox.xml

<CheckBox android:text="" 
                  android:id="@+id/CheckBox" 
                  android:layout_width="wrap_content" 
                  android:layout_height="wrap_content" 
                  android:focusable="false"
                  />

click listener for my list

protected void onListItemClick(ListView l, View v, int position, long id) {


         Toast.makeText(getApplicationContext(), "You have selected item no."
                 +(position+1)+"", Toast.LENGTH_SHORT).show();

        super.onListItemClick(l, v, position, id);
        selectedRow=position;


        if (v == null) {
            Log.v("MyLog", "v was null");
            LayoutInflater lif = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = lif.inflate(R.layout.posting_detail_question_row, null);


        }
        /*if (v!= null) {

            checkedTextView = (CheckedTextView) v.findViewById(R.id.checkedTextBox);
            if (checkedTextView.isChecked())

                checkedTextView.setChecked(false);
            else
                checkedTextView.setChecked(true);

        }*/



        if (checkedTextView.isChecked())
            Toast.makeText(getApplicationContext(), "Allready voted",
                    Toast.LENGTH_SHORT).show();   


        else {
            // checkedTextView.setChecked(true);

                       // My Code



String[] from = { "option", "votes"}; // values to fetch from
                                                        // hashmap
                int[] to = { R.id.option, R.id.votes}; // id's for the view

            SimpleAdapter adp = new SimpleAdapter(getApplicationContext(),
                    hashList, R.layout.posting_detail_question_row, from, to);
            Log.v("MyLog", "after simple adapter");

            setListAdapter(adp);


            checkedTextView.setChecked(true);
        }

        }

posting_detail_question_row.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="40dp">

    <LinearLayout android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:background="@drawable/poll_border_top_bg" android:layout_width="match_parent">

        <TextView android:id="@+id/option" android:textColor="#333333"
            android:textSize="15sp" android:layout_width="0dp"
            android:layout_height="fill_parent" android:layout_marginLeft="10dp"
            android:layout_weight=".3" android:text="Answer 1" android:gravity="center_vertical" />

        <TextView android:id="@+id/votes" android:textColor="#333333"
            android:textSize="15sp" android:layout_width="0dp"
            android:layout_height="fill_parent" android:layout_marginLeft="10dp"
            android:layout_weight="1" android:gravity="center_vertical" />

             <CheckBox android:text="" 
                  android:id="@+id/CheckBox" 
                  android:layout_width="wrap_content" 
                  android:layout_height="wrap_content" 
                  android:focusable="false"
                  />




    </LinearLayout>


</LinearLayout>

my adapter is simple adapter

SimpleAdapter adp = new SimpleAdapter(getApplicationContext(),
                    hashList, R.layout.posting_detail_question_row, from, to);
            Log.v("MyLog", "after simple adapter");
            setListAdapter(adp);

解决方案

You need to use findViewById to get a pointer to your checkbox:

protected void onListItemClick(ListView l, View v, int position, long id) {

    Toast.makeText(getApplicationContext(), "You have selected item no."
                 +(position+1)+"", Toast.LENGTH_SHORT).show();

    super.onListItemClick(l, v, position, id);

    if (v != null) {
        CheckBox checkBox = (CheckBox)v.findViewById(R.id.Checkbox);
        checkBox.setChecked(!checkBox.isChecked());
    }
}

If you are recieving the onClick event then v should never be null but I have put on the appropriate checks.

这篇关于如何设置复选框为选中当我点击列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆