onSelectedItem() returns null on listview.onSelectedItem

198 Views Asked by At

Java Code:

PhoneListAdapter adapter = new PhoneListAdapter(this,brands,models,price);
lv=(ListView)findViewById(R.Id.listView1);    
lv.setAdapter(adapter);        
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> Parent,View view, int position,
                long id) {
            // TODO Auto-generated method stub

                Toast.makeText(getApplicationContext(),"listview clicked", Toast.LENGTH_LONG).show();

        }
    });

XML:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="80dp" 
        android:clickable="true">
    </ListView>

</RelativeLayout></LinearLayout>

I have done everything, if i push a toast with hardcoded string, it appears, but any methods of listview return null. what am i missing???

2

There are 2 best solutions below

0
On

Hey Use somethid like below, if brands,models,price are arrayLists.

lv.setAdapter(adapter);        
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> Parent,View view, int position,
                long id) {
            // TODO Auto-generated method stub
                Assign the String or integer what ever you want. = brands .get(position).get if you have getter and setter methods. 

                Toast.makeText(getApplicationContext(),"listview clicked", Toast.LENGTH_LONG).show();

        }
    });
3
On

@Piyush Gupta

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#33ffffff" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/imageView1"
    android:layout_alignParentTop="true"
    android:layout_marginTop="10dip"
    android:layout_toRightOf="@+id/imageView1"
    android:text="Textview"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#000000" />