Wie setze ich den OnitemclickListener für einen benutzerdefinierten Adapter ein, um eine neue Aktivität mit Parameter für das Element zu starten?
-
26-10-2019 - |
Frage
Ich benutze dieses Beispiel (http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter), um meinen benutzerdefinierten Adapter zu erstellen. Ich habe 2 Layout -Dateien, Whatson.xml und WhatsonList.xml, um eine Hauptstruktur und eine Unterstruktur für die Listenansicht zu erstellen, um Elemente in einem angepassten Format anzuzeigen.
Mein Problem ist, dass ich für jedes Element die Listenansicht einen Listener hinzufügen möchte. Wenn ich also auf das Element klicke, startet sie eine neue Aktivität, die weitere Informationen/Details zu diesem Listenelement anzeigt. Um herauszufinden, auf welches ich geklickt habe, möchte ich einen variablen Parameter verwenden, wenn ich die Detailaktivität starte. Aber ich erhalte immer wieder einen Fehler für diesen Prozess.
Der Code der Hauptaktivitätsseite ist wie:
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;
}
}
}
Mein Problem ist, ob es sich um den richtigen Ort und den richtigen Code handelt, um das OnitemclickListener in der OnCreate -Methode wie wie zu fügen?
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>
Vielen Dank.
Ich habe diesen Fehler bekommen:
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
Ich denke, der wichtigste Teil ist:
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
Was ich denke, es hängt mit dem Hörer zusammen, den ich hinzugefügt habe.
Lösung
Ich glaube, ich habe herausgefunden, warum.
Ich muss den OnitemclickListener danach bewegen
listView.setAdapter(adapter);
Und auch Aktivität WhatsonItemdetailsActivity AndroidManifest.xml.
Andere Tipps
Stellen Sie sicher, dass Ihre Aktivität "WhatSonitemDetailSactivity" in Androidmanifest.xml einen Einstieg hat.