我一直在努力寻找解决方案,但收效甚微。 我必须显示一个包含项目列表的弹出窗口。 我能够显示窗口,但单击列表视图中的项目时未调用 onitemclicklistener 。对此问题的任何帮助将不胜感激。
由于
EDIT1:
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);
}
}
在上面的代码中,我尝试创建一个布局,在其中添加了一个列表视图。这使列表视图不再可单击。我必须这样做因为,我正在尝试实现一个弹出窗口,其中应该有多个项目以及列表视图。
答案 0 :(得分:13)
列表中的列表和项目是否可以点击?以编程方式......
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();
}
});
}
}
答案 1 :(得分:2)
ListView myList;
myList = (ListView)findViewById(list_vew_id_in_xml)
myList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//Toast.makeText(BritishCouncilActivity.this, "" + position, 2).show();
}
});
答案 2 :(得分:1)
针对任何列表视图项
插入此属性android:onClick="buttonClicked"
然后在你的代码中
public void buttonClicked(View view)
{
//Do what you want to do..
}
答案 3 :(得分:0)
编辑答案
申请此样本请为您工作我已经测试过此代码
// ListviewActivity.java
package com.anl.lk;
public class ListviewActivity extends ListActivity {
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria", "American Samoa",
"Andorra", "Angola", "Anguilla", "Antarctica",
"Antigua and Barbuda", "Argentina", "Armenia", "Aruba",
"Australia", "Austria", "Azerbaijan", "Bahrain",
"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize",
"Benin", "Bermuda", "Bhutan", "Bolivia",
"Bosnia and Herzegovina", "Botswana", "Bouvet Island",
"Brazil", "British Indian Ocean Territory"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter < String > (this,
android.R.layout.simple_list_item_1, COUNTRIES));
getListView().setTextFilterEnabled(true);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
new AlertDialog.Builder(this)
.setTitle("Hello")
.setMessage("from " + getListView().getItemAtPosition(position))
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {}
})
.show();
Toast.makeText(ListviewActivity.this,
"ListView: " + l.toString() + "\n" +
"View: " + v.toString() + "\n" +
"position: " + String.valueOf(position) + "\n" +
"id: " + String.valueOf(id),
Toast.LENGTH_LONG).show();
}
}
//第一页
package com.anl.lk;
public class FirstPage extends Activity {
@override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sam = new Intent(FirstPage.this, ListviewActivity.class);
startActivity(sam);
}
});
}
}
//清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.anl.lk"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".FirstPage" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ListviewActivity" android:theme="@android:style/Theme.Dialog"></activity>
</application>
请使用此代码,它有您的解决方案