如何为自定义适配器设置onItemClickListener以使用项目上的参数启动新活动?

时间:2012-01-10 20:45:52

标签: android listview adapter onitemclicklistener

我使用此示例(http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter)来创建我的自定义适配器。 我有2个布局文件,whatson.xml和whatsonlist.xml,用于创建列表视图的主结构和子结构,以自定义格式显示项目。

我的问题是,对于列表视图的每个项目,我想添加一个监听器,因此当我单击该项目时,它会启动一个新活动,该活动显示有关此列表项的更多信息/详细信息。为了找出我点击了哪一个,我想在开始详细信息活动时使用变量参数。但是我一直在为这个过程收到错误。

主活动页面的代码如下:

package com.learn.whatson;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class WhatsOnActivity extends Activity
{
    private ListView listView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.whatson);

        WhatsOnItemData whatsOnItemData[] = new WhatsOnItemData[] { 
            new WhatsOnItemData(R.drawable.a1_icon, "a1Title", "a1Body"), 
    new WhatsOnItemData(R.drawable.a2_icon, "a2Title", "a2Body"),
    new WhatsOnItemData(R.drawable.a3_icon, "a3Title", "a3Body")};

        CustomAdapter adapter = new CustomAdapter(this,R.layout.whatsonlist, whatsOnItemData);

        listView = (ListView) findViewById(R.id.listview_whatsonlist);

        listView.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Intent whatsOnItemDetailsIntent = new Intent(WhatsOnActivity.this, WhatsOnItemDetailsActivity.class);

                CustomAdapter ca = (CustomAdapter)parent.getAdapter();
                WhatsOnItemData woid = (WhatsOnItemData)ca.getItem(position);

                whatsOnItemDetailsIntent.putExtra("whatsonitemdetails", woid.title);
                startActivity(whatsOnItemDetailsIntent);    
            }
        });



        listView.setAdapter(adapter);

    }

    public class CustomAdapter extends ArrayAdapter<WhatsOnItemData>
    {

        Context context;
        int layoutResourceId;
        WhatsOnItemData data[] = null;

        public CustomAdapter(Context context, int layoutResourceId, WhatsOnItemData[] data)
        {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View row = convertView;
            WhatsOnItemDataHolder holder = null;

            if (row == null)
            {
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(layoutResourceId, parent, false);

                holder = new WhatsOnItemDataHolder();
                holder.imgIcon = (ImageView) row.findViewById(R.id.imageview_whatsonitemicon);
                holder.txtTitle = (TextView) row.findViewById(R.id.whatsonitemtitle);
                holder.txtBody = (TextView) row.findViewById(R.id.whatsonitembody);

                row.setTag(holder);
            }
            else
            {
                holder = (WhatsOnItemDataHolder) row.getTag();
            }

            WhatsOnItemData whatsOnItemData = data[position];
            holder.txtTitle.setText(whatsOnItemData.title);
            holder.imgIcon.setImageResource(whatsOnItemData.icon);
            holder.txtBody.setText(whatsOnItemData.body);

            return row;
        }

        public class WhatsOnItemDataHolder
        {
            ImageView imgIcon;
            TextView txtTitle;
            TextView txtBody;
        }
    }

    public class WhatsOnItemData
    {
        public int icon;
        public String title;
        public String body;

        public WhatsOnItemData()
        {
            super();
        }

        public WhatsOnItemData(int icon, String title, String body)
        {
            super();
            this.icon = icon;
            this.title = title;
            this.body = body;
        }
    }
}

我的问题是,如果在onCreate方法中添加onItemClickListener是正确的地方和正确的代码吗?

listView.setOnItemClickListener(new OnItemClickListener()
{
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        Intent whatsOnItemDetailsIntent = new Intent(WhatsOnActivity.this, WhatsOnItemDetailsActivity.class);

        CustomAdapter ca = (CustomAdapter)parent.getAdapter();
        WhatsOnItemData woid = (WhatsOnItemData)ca.getItem(position);

        whatsOnItemDetailsIntent.putExtra("whatsonitemdetails", woid.title);
        startActivity(whatsOnItemDetailsIntent);    
    }
});

whatson.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" >
    <ImageView
        android:id="@+id/imageview_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:src="@drawable/whatson_title"
        android:contentDescription="@string/whatson" />
    <ListView
        android:id="@+id/listview_whatsonlist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

whatsonitemlist.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="horizontal" >
    <ImageView
        android:id="@+id/imageview_whatsonitemicon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp" 
        android:contentDescription="@string/whatsonitemicon" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >        
        <TextView
            android:id="@+id/whatsonitemtitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:textStyle="bold"  />
        <TextView
            android:id="@+id/whatsonitembody"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:textSize="10sp"  />
    </LinearLayout>
</LinearLayout>

谢谢。

我收到了这个错误:

W/ResourceType(621): No package identifier when getting value for resource number 0x00000000
D/AndroidRuntime(621): Shutting down VM
W/dalvikvm(621): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(621): FATAL EXCEPTION: main
E/AndroidRuntime(621): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.learn.whatson/com.learn.whatson.WhatsOnItemDetailsActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
E/AndroidRuntime(621):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime(621):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(621):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(621):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(621):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(621):  at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(621):  at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(621):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(621):  at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(621):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(621):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(621):  at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(621): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
E/AndroidRuntime(621):  at android.content.res.Resources.getText(Resources.java:201)
E/AndroidRuntime(621):  at android.widget.TextView.setText(TextView.java:2817)
E/AndroidRuntime(621):  at com.learn.whatson.WhatsOnItemDetailsActivity.onCreate(WhatsOnItemDetailsActivity.java:28)
E/AndroidRuntime(621):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(621):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(621):  ... 11 more

我认为最重要的部分是:

E/AndroidRuntime(621): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.learn.whatson/com.learn.whatson.WhatsOnItemDetailsActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0

我认为这与我添加的听众有关。

2 个答案:

答案 0 :(得分:1)

我想我发现了原因。

我需要在

之后移动onItemClickListener
listView.setAdapter(adapter);

还活动WhatsOnItemDetailsActivity AndroidManifest.xml。

答案 1 :(得分:0)

确保您的活动“WhatsOnItemDetailsActivity”在AndroidManifest.xml中有一个条目。