user2523388
user2523388

Reputation: 112

Unable to set onItemClickListener in listview

My layout code is as shown below:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relRingtone"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
> 
            <ListView
               android:focusable="false"
             android:id="@+id/list_notification"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"   
             android:choiceMode="singleChoice"
             android:layout_marginLeft="3dip" 
             android:textSize="2dp" />
             <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="match_parent" >
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingBottom="40dp" >

            <Button
                android:id="@+id/btn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="40dp"
                android:background="@drawable/button"
                android:padding="5dp"
                android:text="Start Working"
                android:textColor="#FFF"
                android:textSize="20sp"
                android:textStyle="bold" />

            <Chronometer
                android:id="@+id/chronometer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/btn1"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="30dp"
                android:padding="5dp"
                android:text="Vehicle  Details"
                android:textColor="#FFF"
                android:textSize="20sp"
                android:textStyle="bold"
                android:visibility="gone" />

            <Button
                android:id="@+id/btn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/chronometer"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="30dp"
                android:background="@drawable/button"
                android:padding="5dp"
                android:text="Take Leave"
                android:textColor="#FFF"
                android:textSize="20sp"
                android:textStyle="bold" />
        </RelativeLayout>
    </ScrollView>

     </RelativeLayout>

and below is my java code:

list_notification.setVisibility(View.VISIBLE);
        String s[] = new String[] { "Your leave request has been accepted",
                "test notification" };
        ArrayAdapter<String> adpt = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, s);
        list_notification.setAdapter(adpt);
        list_notification.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                Toast.makeText(getApplicationContext(),
                        "list item clicked", Toast.LENGTH_SHORT).show();
            }
        });`

Upvotes: 1

Views: 214

Answers (2)

hoangnh
hoangnh

Reputation: 249

The problem is layout, add android:layout_below="@id/list_notification"

The ScrollView will not overlay the listview

<ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_below="@id/list_notification"
        >

Upvotes: 1

Tenfour04
Tenfour04

Reputation: 93609

I think this line in <ListView> might be the culprit:

android:focusable="false"

assuming that your error is simply that clicking the items doesn't do anything.

Your UI might have some other issues...stacking a list view on top of a scroll view is going to create problems when your list is taller than the screen.

Upvotes: 0

Related Questions