【问题标题】:onSelectedItem() returns null on listview.onSelectedItemonSelectedItem() 在 listview.onSelectedItem 上返回 null
【发布时间】:2014-11-17 07:23:40
【问题描述】:

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>

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

【问题讨论】:

  • 使用你的位置从Adapter中获取选中的Item....
  • 你能把你的完整代码吗?
  • 请从您的 xml 中删除 。
  • listview的任何方法返回null的意思?你能解释更多吗?
  • ListView添加android:focusable="true"

标签: android listview onitemclick


【解决方案1】:

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

【讨论】:

  • 这并没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时评论自己的帖子,一旦您有足够的reputation,您就可以comment on any post
  • 纯代码答案会自动标记为低质量,因此不鼓励在 stackoverflow 上使用。将来请用细节修饰您的答案,并解释为什么它是问题的解决方案。这有助于其他用户。
  • @Zefnus 好的,下次再做,谢谢 :)
【解决方案2】:

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

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

        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-06
    • 2016-01-26
    • 2021-11-21
    • 2015-06-11
    • 2015-08-11
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多