Comment régler le onItemClickListener pour une carte personnalisée pour démarrer une nouvelle activité avec le paramètre sur l'élément?

StackOverflow https://stackoverflow.com/questions/8810291

Question

J'utilise cet échantillon (http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter) pour créer mon adaptateur personnalisé. J'ai 2 fichiers de mise en page, et whatson.xml whatsonlist.xml pour créer une structure principale et une structure sous la forme de liste aux éléments d'affichage dans un format personnalisé.

Mon problème est, pour chaque élément de la vue de la liste, je veux ajouter un écouteur, alors quand je clique sur l'élément, il démarre une nouvelle activité, qui montre plus d'informations / détails sur cet élément de liste. Pour en savoir qui je clique dessus, je veux utiliser un paramètre variable quand je commence l'activité de détails. Mais je continue à obtenir l'erreur pour ce processus.

Le code de la page principale activité est comme:

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;
        }
    }
}

Mon problème est de savoir si c'est le bon endroit et le code droit d'ajouter la onItemClickListener dans la méthode comme onCreate?

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);    
    }
});

Code 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>

Code 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>

Merci.

Je suis cette erreur:

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

Je pense que la partie la plus importante est:

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

que je pense qu'il est lié à l'auditeur je.

Était-ce utile?

La solution

Je pense avoir trouvé pourquoi.

Je dois déplacer le onItemClickListener après

listView.setAdapter(adapter);

Et aussi l'activité WhatsOnItemDetailsActivity AndroidManifest.xml.

Autres conseils

Assurez-vous que votre activité « WhatsOnItemDetailsActivity » a une entrée dans AndroidManifest.xml.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top