onSelectedItem() 在 listview.onSelectedItem 上返回 null



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>

我已经完成了所有工作,如果我用硬编码字符串推送 toast,它会出现,但任何列表视图方法都返回 null。 我错过了什么???

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

嘿,

使用下面这样的东西,如果品牌,型号,价格是数组列表。

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

相关内容

  • 没有找到相关文章

最新更新