Pergunta

I've created a custom ListView using my own version of ArrayAdapter. My problem is that it is not responding to any onItemClickListeners. Here are my code snippets:

final ArrayAdapter<Agency> aa=new myCustomAdapter();
    lv.setAdapter(aa);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View myView, int pos,
                long id) {
            TextView clickedTV=(TextView)myView.findViewById(R.id.rowtext1);
            Toast.makeText(getBaseContext(), clickedTV.getText().toString(), Toast.LENGTH_LONG).show();


        }
    });

Adapter implementation:

private class myCustomAdapter extends ArrayAdapter<Agency> {

    public myCustomAdapter() {
        super(getBaseContext(), R.layout.layout_row, AgencyList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView=convertView;
        if (itemView==null)
            itemView=getLayoutInflater().inflate(R.layout.layout_row, parent, false);
        TextView rowText=(TextView)itemView.findViewById(R.id.rowtext1);
        Agency currentAgency=AgencyList.get(position);
        rowText.setText(currentAgency.getAgency().toString());
        return itemView;
    }   
}

I've created a custom xml layout for each row then inflated in my adapter. The custom row layout has TextView and a CheckBox:

    <?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" >

    <TextView
        android:id="@+id/rowtext1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="21dp"
        android:text="Row Item"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <CheckBox
        android:id="@+id/rowcheckBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/rowtext1"
        android:layout_alignBottom="@+id/rowtext1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="26dp"
        android:text="Select this" />

</RelativeLayout>

Can't I have a clickable row in a listView where there's already a clickable widget (like checkboxes)? I've seen this implementation in many phones (Sony's Timescape UI for example). Please suggest a work around! Thanks for your help!

Foi útil?

Solução 2

Must be sure whenever you performing with Custom ListView and onItemClick event not be triggered you have to set this properties for your custom layout UI elements. So set this for TextView and Checkbox

android:focusable="false"
android:focusableInTouchMode="false" 

Outras dicas

Set this in your Check-box of custom layout..

 android:focusable="false"
 android:focusableInTouchMode="false" 

So your full code will look something like this

 <CheckBox
    android:id="@+id/rowcheckBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/rowtext1"
    android:layout_alignBottom="@+id/rowtext1"
    android:layout_alignParentRight="true"
    android:layout_marginRight="26dp"
    android:focusable="false"
    android:focusableInTouchMode="false" 
    android:text="Select this" />

Looks like your check-box is causing this issue...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top