如何设置onListItemClick在Android的列表视图 [英] how to set onListItemClick for listview in android
问题描述
喜
我需要为列表视图做onListItemClick事件。我做了以下,但没有奏效。我贴我的code和logcat中。请帮帮我。错误是:(了java.lang.RuntimeException:您的内容必须有一个ListView的id属性是'android.R.id.list')
公共类MyListView扩展ListActivity {
/ **当第一次创建活动调用。 * /
// @覆盖
ListView的list1的;
私人字符串数组[] = {iPhone,教程,画廊,机器人,项目1,项目2,项目3,4项};
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
list1的=(的ListView)findViewById(R.id.ListView01);
list1.setAdapter(新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,数组)); }
保护无效onListItemClick(ListView中升,视图V,INT位置,长的id){
super.onListItemClick(L,V,位置ID);
对象o = this.getListAdapter()的getItem(位置)。
字符串笔= o.toString();
Toast.makeText(这一点,你选择了笔:++笔,Toast.LENGTH_LONG).show();
}
}
我的日志文件的猫:
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.sai.ui.listview / COM。 sai.ui.listview.MyListView}了java.lang.RuntimeException:您的内容必须有一个ListView的id属性是'android.R.id.list
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在android.app.ActivityThread.access $ 1800(ActivityThread.java:112)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1692)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在android.os.Handler.dispatchMessage(Handler.java:99)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在android.os.Looper.loop(Looper.java:123)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在android.app.ActivityThread.main(ActivityThread.java:3948)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在java.lang.reflect.Method.invokeNative(本机方法)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在java.lang.reflect.Method.invoke(Method.java:521)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:782)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在dalvik.system.NativeStart.main(本机方法)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):了java.lang.RuntimeException:致您的内容必须有一个ListView的id属性是'android.R.id.list
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在android.app.ListActivity.onContentChanged(ListActivity.java:236)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:321)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在android.app.Activity.setContentView(Activity.java:1631)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在com.sai.ui.listview.MyListView.onCreate(MyListView.java:21)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1132)
04-19 18:12:36.021:ERROR / AndroidRuntime(5162):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
我的main.xml:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:stackFromBottom =真
>
< ImageView的
机器人:ID =@ + ID / imageview1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =50像素
机器人:背景=@绘制/应用程序任务栏
机器人:layout_x =0像素
机器人:layout_y =0像素
>
< / ImageView的>
<的TextView
机器人:ID =@ + ID / textview1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =50像素
机器人:文字=TextView的
机器人:layout_x =0像素
机器人:layout_y =55像素
>
< / TextView的>
< ListView的机器人:ID =@ + ID /列表
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =10px的
机器人:layout_marginRight =10px的
机器人:layout_marginTop =35px
机器人:layout_marginBottom =40像素
机器人:paddingLeft =0像素
机器人:paddingRight =0像素/>
< / LinearLayout中>
ListActivity
要求的ListView
id为 android.R.id.list
。它为您提供更多的方法管理你的的ListView
和它的适配器。所以,你不应该使用code像
list1的=(的ListView)findViewById(R.id.ListView01);
使用
list1的= getListView()
来代替。
不过,你可以去为卡尔蒂克建议,意味着你可以使用普通的活动
而不是 ListActivity
和使用你最初发布code。
hi i need to do onListItemClick event for list view. i did the following but did not work. i paste my code and logcat. please help me. error is: (java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list')
public class MyListView extends ListActivity {
/** Called when the activity is first created. */
// @Override
ListView list1;
private String array[] = { "Iphone", "Tutorials", "Gallery", "Android","item 1", "item 2", "item3", "item 4" };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list1 = (ListView) findViewById(R.id.ListView01);
list1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String pen = o.toString();
Toast.makeText(this, "You have chosen the pen: " + " " + pen, Toast.LENGTH_LONG).show();
}
}
my log cat file:
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sai.ui.listview/com.sai.ui.listview.MyListView}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at android.os.Looper.loop(Looper.java:123)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at android.app.ActivityThread.main(ActivityThread.java:3948)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at java.lang.reflect.Method.invoke(Method.java:521)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at dalvik.system.NativeStart.main(Native Method)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at android.app.ListActivity.onContentChanged(ListActivity.java:236)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:321)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at android.app.Activity.setContentView(Activity.java:1631)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at com.sai.ui.listview.MyListView.onCreate(MyListView.java:21)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1132)
04-19 18:12:36.021: ERROR/AndroidRuntime(5162): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
my main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stackFromBottom="true"
>
<ImageView
android:id="@+id/imageview1"
android:layout_width="wrap_content"
android:layout_height="50px"
android:background="@drawable/applicationbar"
android:layout_x="0px"
android:layout_y="0px"
>
</ImageView>
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="50px"
android:text="TextView"
android:layout_x="0px"
android:layout_y="55px"
>
</TextView>
<ListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
android:layout_marginTop="35px"
android:layout_marginBottom="40px"
android:paddingLeft="0px"
android:paddingRight="0px" />
</LinearLayout>
ListActivity
requires ListView
with id android.R.id.list
. It provides you with additional methods to manage your ListView
and it's adapter. So you should not use code like
list1 = (ListView) findViewById(R.id.ListView01);
use
list1 = getListView()
instead.
However, you could go as Kartik suggested, means you can use a regular Activity
instead of ListActivity
and use your originally posted code.
这篇关于如何设置onListItemClick在Android的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!