2012-01-10 3 views
2

カスタムアダプターを作成するには、このサンプル(http://www.ezzylearning.com/tutorial.aspx?tid=1763429 & q = custom-withroid-listview-items-with-custom-arrayadapter)を使用します。 2つのレイアウトファイルwhatson.xmlとwhatsonlist.xmlを使用して、リストビューのメイン構造とサブ構造を作成し、項目をカスタマイズされた形式で表示します。アイテムのパラメータで新しいアクティビティを開始するカスタムアダプタのonItemClickListenerを設定するにはどうすればよいですか?

リストビューごとにリスナーを追加したいので、アイテムをクリックすると、このリストアイテムに関する詳細情報や詳細を示す新しいアクティビティが開始されます。どのファイルをクリックしたかを調べるには、詳細アクティビティを開始するときに可変パラメータを使用します。しかし、私はこのプロセスの間違いを続けています。

主な活動のページのコードは次のようである:

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 

+1

どのようなエラーが表示されますか? – dmaxi

+0

例外スタックトレースをLogcatに送信します。 – yorkw

+0

私は、元の投稿にスタ​​ックトレースを追加しました。ありがとうございました –

答えて

1

私は理由を知ったと思います。

私は

listView.setAdapter(adapter); 

も活動WhatsOnItemDetailsActivityのAndroidManifest.xml後onItemClickListenerを移動する必要があります。

0

アクティビティ "WhatsOnItemDetailsActivity"にAndroidManifest.xmlのエントリがあることを確認してください。

+0

はい、私はエラートラックを投稿した後ではなく、これを見つけましたが、質問をしてから8時間以内に回答を投稿することはできません。 –

関連する問題