如何在android中设置onListItemClick for listview
hi 我需要为列表视图执行onListItemClick事件。我做了以下,但没有工作。我粘贴我的代码和logcat。请帮帮我。错误是:(了java.lang.RuntimeException:你的内容必须有一个ListView其id属性为 'android.R.id.list')如何在android中设置onListItemClick for listview
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();
}
}
我的日志文件的猫:
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)
我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
要求ListView
与编号android.R.id.list
。它为您提供其他方法来管理您的ListView
及其适配器。所以,你不应该使用这样的代码
list1 = (ListView) findViewById(R.id.ListView01);
使用 list1 = getListView()
代替。
但是,您可以像Kartik建议的那样去使用,这意味着您可以使用常规Activity
而不是ListActivity
并使用您最初发布的代码。
你这里的问题是,你必须确保您的ListView
有这个ID:@android:id/list
(哟你目前正在使用ListView01
)。事实上,这就是错误是试图告诉你:
<ListView
android:id="@android:id/list"
....
在你的XML,改变ListView0
只是list
的ID。
在你的名为main.xml的xml布局文件中,看起来你有一个ID为ListView01的列表。这应该挂在android.R.id.list上。因此,在你的XML ...
<ListView android:id="@android:id/list"
...
/>
而且,你并不需要创建一个引用来设置适配器,你可以调用setListAdapter方法ListActivity。这一翻译的
public class MyListView extends ListActivity
和收听项的点击
嗨如何在我的活动中使用@android:id/list list1 =(ListView)findViewById(R.id ....)我没有从R.id得到它。请帮帮我。 – 2011-04-19 13:13:39
public class MyListView extends Activity
,使用此:
list1.setOnItemClickListener(
new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
//Take action here.
}
}
);
所以你完整的源代码将是这样的:
public class MyListView extends Activity {
/** Called when the activity is first created. */
ListView list1;
private String array[] = { "Iphone", "Tutorials", "Gallery", "Android","item 1", "item 2", "item3", "item 4" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list1 = (ListView) findViewById(R.id.list);
list1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));
list1.setOnItemClickListener(
new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
// TODO Auto-generated method stub
Object o = list1.getItemAtPosition(position);
String pen = o.toString();
Toast.makeText(getApplicationContext(), "You have chosen the pen: " + " " + pen, Toast.LENGTH_LONG).show();
}
}
);
}
}
Edi特德按ernazm答案:,如果你想使用
public class MyListView extends ListActivity
您必须删除
setContentView(R.layout.main);
我看到有一个可绘制在XML文件中 你仍然可以去用这个添加图像头使用listview.addHeader():
Resources res=getResources();
Drawable d1=res.getDrawable(R.drawable.yourimage);
textview.setBackgroundDrawable(d1);
listview.addHeaderView(tv1);
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Builder builder = new AlertDialog.Builder(this);
builder.setMessage(item[position] + " is clicked.");
builder.setPositiveButton("OK", null);
builder.show();
}
我在main.xml中将ListView更改为list和list1 =(ListView)findViewById(R.id.list);再次我得到同样的错误。 – 2011-04-19 13:03:37
你可以发布你的XML吗? – 2011-04-19 13:04:42
嗨,我通过编辑我的问题粘贴我的XML(最后)请帮助我。 – 2011-04-19 13:08:08