我一直在尝试寻找解决方案,但收效甚微。 我必须显示一个包含项目列表的弹出窗口。 我能够显示该窗口,但在单击 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/