android - 如何使 ListView 中的项目可点击?

标签 android listview popupwindow clickable onitemclicklistener

我一直在尝试寻找解决方案,但收效甚微。 我必须显示一个包含项目列表的弹出窗口。 我能够显示该窗口,但在单击 ListView 中的项目时未调用onitemclicklistener。对此问题的任何帮助将不胜感激。

谢谢

编辑1:

public class PopUpWindowActivity extends Activity {

    /** Called when the activity is first created. */
    String[] countries = new String[] {
        "India", "USA", "Canada"
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout ll = new LinearLayout(this);
        ListView lv = new ListView(this);
        lv.setAdapter(new ArrayAdapter < String > (this, android.R.layout.simple_list_item_1, countries));
        lv.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView <? > arg0, View arg1, int arg2,
                    long arg3) {
                    Log.v("clicked", (String)((TextView) arg1).getText());
                }

            });
        ll.addView(lv);
        setContentView(ll);
    }
}

在上面的代码中,我尝试创建一个布局,在其中添加了一个 ListView 。这使得 ListView 不再可点击。我必须这样做,因为我正在尝试实现一个弹出窗口,其中应该有多个项目以及一个 ListView 。

最佳答案

列表和列表中的项目是否设置为可点击?以编程方式...

ListView myList = (ListView) findViewById(R.id.list_view_id);
myList.setClickable(true);

或者在 XML 中...

   <ListView xmlns:android="http://schemas.android.com/apk/res/android"
       android:clickable="true">
   ...
   </ListView>

我假设你这样做了,但有时我们甚至错过了显而易见的 :)

编辑:

来自Android Tutorial以下是如何以编程方式设置 onItemClickListener。

 ListView lv = getListView();
 lv.setOnItemClickListener(new OnItemClickListener() {

   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       // When clicked perform some action...
   }
 });

编辑 2:

这是我的 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/list" />

</LinearLayout>

这是我的代码

 public class HelloAndroidActivity extends Activity {

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         String[] countries = getResources().getStringArray(R.array.countries_array);

         ListView lv = (ListView) findViewById(R.id.list);
         lv.setAdapter(new ArrayAdapter < String > (this, R.layout.list_item, countries));
         lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                 public void onItemClick(AdapterView <? > arg0, View view, int position, long id) {
                     // When clicked, show a toast with the TextView text
                     Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
                         Toast.LENGTH_SHORT).show();

                 }

             });
     }
 }

关于android - 如何使 ListView 中的项目可点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9596663/

相关文章:

android - 分页库使数据源无效

java - 从 Android 应用程序消耗 JSON 的奇怪异常

c# - ListView 的 GroupStyleSelector

android - 如何在特殊位置显示 PopupWindow?

java - 在 Android 的 Java 中将此字符串分解为键/值字符串的最简单方法是什么?

java - 如何在没有dex的情况下在Android上加载类

android - 在哪里放置 notifyDataSetChanged();在 customAdapter 中,因此 listView 不会在滚动时丢失数据

android - 检查 ListView 内的 EditText 对象中的条目

android - 如何在android中显示与Android市场使用的相同的弹出窗口?

android - 来自 ListView 的 setOnItemClickListener 的 PopupWindow 未显示