【问题标题】:I'm using a list and OnItemClickListener but when I click the item,it doesn't go anywhere我正在使用列表和 OnItemClickListener 但是当我单击该项目时,它不会去任何地方
【发布时间】:2015-03-08 18:33:10
【问题描述】:

这是我的 xml 代码,我希望它可以帮助你看到它是一个相对布局..

<ListView
    android:layout_width="wrap_content"
    android:layout_height="350dp"
    android:id="@+id/listView2"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Delete"
    android:id="@+id/deletebutton12"
    android:layout_above="@+id/listView2"
    android:layout_centerHorizontal="true" />

【问题讨论】:

  • onClick 应用停止或没有任何反应?
  • 您是否将您的listviewonItemClickListener 设置为您的listListener
  • 调试你的代码,看看真正的 onItemClick() 方法运行
  • @MHP 它没有运行。可能是什么问题??

标签: java android genymotion


【解决方案1】:

使用getItemAtPosition():

AdapterView.OnItemClickListener listListener = new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                            long arg3) {

        ApartmentPhoto item = (ApartmentPhoto) arg0.getItemAtPosition(arg2);

        Intent intent = new Intent(ViewYourApartments.this,
                MainActivityTest.class);
        intent.putExtra("ID",String.valueOf(item.id));
        startActivity(intent);

    }
};

如果它不去任何地方,请检查 ViewYourApartments.this 是否是您正在运行侦听器的活动,MainActivityTest.class 是目标。

更新

您确定要在 ListView 上调用侦听器吗?

类似的东西:

ListView lv = (ListView) view.findViewById(R.id.listView);
lv.setOnItemClickListener(listListener);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
  • 2022-11-23
相关资源
最近更新 更多