How to set the onItemClickListener for a Custom Adapter to start a new activity with parameter on the item?
-
26-10-2019 - |
题
I use this sample(http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter) to create my Custom Adapter. I have 2 layout files, whatson.xml and whatsonlist.xml to create a main structure and a sub structure for the list view to display items in a customized format.
My problem is, for each item the list view, I want to add a listener, so when I click the item, it start a new activity, which shows more information/details about this list item. To find out which one I clicked, I want to use a variable parameter when I start the details activity. But I keep getting error for this process.
The Main Activity page's code is like:
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;
}
}
}
My problem is if it is the right place and right code to add the onItemClickListener in the onCreate method like?
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 code:
<?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 code:
<?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>
Thank you.
I got this error:
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
I think the most important part is:
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
which I think it is related to the listener I added.
解决方案
I think I found out why.
I need to move the onItemClickListener after
listView.setAdapter(adapter);
And also activity WhatsOnItemDetailsActivity AndroidManifest.xml.
其他提示
Make sure your Activity "WhatsOnItemDetailsActivity" has a entry in AndroidManifest.xml.