onSelectedItem()在listview.onSelectedItem上返回null

时间:2014-11-17 07:23:40

标签: android listview onitemclick

Java代码:

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>

我已经做了所有事情,如果我用硬编码的字符串推出一个吐司,它会出现,但listview的任何方法都返回null。我错过了什么???

2 个答案:

答案 0 :(得分:0)

嘿,如果品牌,型号,价格是数组列表,请使用下面的内容。

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();

        }
    });

答案 1 :(得分:0)

@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" />