简体   繁体   中英

android listview checkbox can't click

i have this method vector elements in listview with checkboxes:

public class sensor_choose extends Activity {
    public ListView mainListView;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sensors);

    SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    List<Sensor> listSensor = sensorManager.getSensorList(Sensor.TYPE_ALL);

    List<String> arrStr = new Vector<String>();

    // for (Sensor s : listSensor) {
    //
    // arrStr.add(s.getName());
    // }

    arrStr.add("Vector Object 1");
    arrStr.add("Vector Object 2");
    arrStr.add("Vector Object 3");
    arrStr.add("Vector Object 4");
    arrStr.add("Vector Object 5");

    for (int i = 0; i < arrStr.size(); i++) {
        // System.out.println("Vector Element " + i + " :" + arrStr.get(i));
        Log.i("Vector Element ", arrStr.get(i));
    }

    ListView listView = (ListView) findViewById(R.id.sensor_list);
    // a listát berakni aegy arraylist-be
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.simple_sensor, android.R.id.text1, arrStr);

    // ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    // android.R.layout.simple_list_item_multiple_choice, arrStr);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View item,
                int position, long id) {
            Toast.makeText(sensor_choose.this,
                    "megnyomott:    "+ adapter.getItem(position),
                    Toast.LENGTH_SHORT).show();
        }
    });

}
}

and the xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Aze elérhető szenzorok:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ListView
        android:id="@+id/sensor_list"
        android:layout_marginTop="20pt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" 
        android:clickable="true">
    </ListView>

</RelativeLayout>

and the costum row xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />


    <TextView
        android:id="@android:id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/checkBox1"
        android:padding="5pt"
        android:paddingBottom="3dip"
        android:paddingRight="5dip"
        android:paddingTop="2dip"
        android:textSize="8pt"
        android:clickable="true" />

</RelativeLayout>

my problem is that i can't click on the textview, like it set clickable:false... i can click on the checkbox but nothing else.

You should create Your own Adapter and in the getView() method set onClick Listener to the TextView

example:

public View getView(int position, View convertView, ViewGroup parent) {
.....
.....
....
...
TextView text = (TextView) convertView.findViewById(android.R.id.text1);
text.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Log.d("TAG", "hello from textView");

                }
            });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM