Android- Help changing XML items using setOnItemClickListener -
so have list of cocktails , want click 1 , change 3 items in xml, image,textview , link button. i'm having difficulty setting , changing these items. im experimenting 2 cocktails @ moment. xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/beer" android:id="@+id/cocktaildetail" > <textview android:id="@+id/textcocktailname" android:textstyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="cocktail name" android:textappearance="?android:attr/textappearancelarge" /> <imageview android:id="@+id/imagecocktail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/bloodymary" /> <textview android:textstyle="bold" android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="ingredients" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/textingredients" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="insert txt here" android:textappearance="?android:attr/textappearancesmall" android:textstyle="bold" /> <textview android:textstyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margintop="2dp" android:text="preparation" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/textprepration" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="insert txt here" android:textappearance="?android:attr/textappearancesmall" android:textstyle="bold" /> <button android:id="@+id/buttonyoutube" android:layout_width="200dp" android:layout_height="75dp" android:layout_gravity="center" android:layout_margintop="5dp" android:text="youtube clip" android:textsize="20dp" /> </linearlayout>
class
package com.drunktxtapp; import android.net.uri; import android.os.bundle; import android.widget.adapterview; import android.widget.button; import android.widget.imageview; import android.view.view.onclicklistener; import android.app.activity; import android.app.listactivity; import android.content.intent; import android.widget.textview; import android.view.view; import android.widget.listview; import android.widget.adapterview.onitemclicklistener; public class cocktaildetail extends listactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.cocktaildetail); listview cocktail = getlistview(); cocktail.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int position, long id) { imageview imageview1 = (imageview)findviewbyid(r.id.imagecocktail); if(position == 0){ imageview1.setimagedrawable(getresources().getdrawable(r.drawable.bloodymary)); button b1 = (button) findviewbyid(r.id.buttonyoutube); textview t1 = (textview)findviewbyid(r.id.textcocktailname); string cocktailname = getintent().getstringextra("bloody_mary"); t1.settext(cocktailname); b1.setonclicklistener(new button.onclicklistener() { public void onclick(view v) { startactivity(new intent(intent.action_view, uri.parse("http://www.youtube.com/watch?v=alt-ehdc3fc"))); } }); }else{ imageview1.setimagedrawable(getresources().getdrawable(r.drawable.bloodymary)); button b1 = (button) findviewbyid(r.id.buttonyoutube); textview t1 = (textview)findviewbyid(r.id.textcocktailname); string cocktailname = getintent().getstringextra("capirinha"); t1.settext(cocktailname); b1.setonclicklistener(new button.onclicklistener() { public void onclick(view v) { startactivity(new intent(intent.action_view, uri.parse("http://www.youtube.com/watch?v=alt-ehdc3fc"))); } }); } } }); } }
list code
package com.drunktxtapp; import android.app.listactivity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.arrayadapter; import android.widget.listview; public class menu extends listactivity{ string classes[] = {"bloody_mary", "capirinha", "cosmopolitan", "cuba_libre", "daiquiri", "mai_tai", "manhattan", "margarita", "martini", "mint_julep", "mojito", "old_fashoned", "pina_colada", "screwdriver", "singapore_sling", "tom_collins", "whiskey_sour", "white_russian"}; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setlistadapter(new arrayadapter<string>(menu.this,android.r.layout.simple_list_item_1, classes)); } @override protected void onlistitemclick(listview l, view v, int position, long id) { // todo auto-generated method stub super.onlistitemclick(l, v, position, id);; intent ourintent = new intent(menu.this, cocktaildetail.class); ourintent.putextra("cocktails", classes); startactivity(ourintent); } }
logcat
04-18 11:50:36.330: d/dalvikvm(32700): gc_for_alloc freed 40k, 7% free 12378k/13191k, paused 14ms, total 14ms 04-18 11:50:36.335: i/dalvikvm-heap(32700): grow heap (frag case) 12.992mb 358208-byte allocation 04-18 11:50:36.345: d/dalvikvm(32700): gc_for_alloc freed 1k, 7% free 12727k/13575k, paused 12ms, total 12ms 04-18 11:50:36.350: v/mediaplayer-jni(32700): native_setup 04-18 11:50:36.350: v/mediaplayer(32700): constructor 04-18 11:50:36.360: v/mediaplayer(32700): setlistener 04-18 11:50:36.360: v/mediaplayer-jni(32700): setdatasourcefd: fd 44 04-18 11:50:36.360: v/mediaplayer(32700): setdatasource(44, 18199, 64493) 04-18 11:50:36.370: v/mediaplayer(32700): setvideosurfacetexture 04-18 11:50:36.370: v/mediaplayer(32700): prepare 04-18 11:50:36.375: v/mediaplayer(32700): message received msg=5, ext1=0, ext2=0 04-18 11:50:36.380: v/mediaplayer(32700): new video size 0 x 0 04-18 11:50:36.380: v/mediaplayer(32700): callback application 04-18 11:50:36.380: v/mediaplayer(32700): callback 04-18 11:50:36.380: v/mediaplayer(32700): message received msg=1, ext1=0, ext2=0 04-18 11:50:36.380: v/mediaplayer(32700): prepared 04-18 11:50:36.380: v/mediaplayer(32700): signal application thread 04-18 11:50:36.380: v/mediaplayer(32700): callback application 04-18 11:50:36.380: v/mediaplayer(32700): prepare complete - status=0 04-18 11:50:36.380: v/mediaplayer-jni(32700): start 04-18 11:50:36.380: v/mediaplayer(32700): start 04-18 11:50:36.385: v/mediaplayer(32700): callback 04-18 11:50:36.635: i/mediaplayer(32700): don't send intent. msg.arg1 = 0, msg.arg2 = 0 04-18 11:50:40.750: v/mediaplayer(32700): message received msg=2, ext1=0, ext2=0 04-18 11:50:40.750: v/mediaplayer(32700): playback complete 04-18 11:50:40.750: v/mediaplayer(32700): callback application 04-18 11:50:40.750: v/mediaplayer(32700): callback 04-18 11:50:41.675: v/mediaplayer-jni(32700): release 04-18 11:50:41.675: v/mediaplayer(32700): setlistener 04-18 11:50:41.675: v/mediaplayer(32700): disconnect 04-18 11:50:41.680: v/mediaplayer(32700): destructor 04-18 11:50:41.680: v/mediaplayer(32700): disconnect 04-18 11:50:42.750: d/abslistview(32700): motionrecognitionmanager 04-18 11:50:43.980: d/dalvikvm(32700): gc_concurrent freed 233k, 7% free 12967k/13831k, paused 12ms+2ms, total 27ms 04-18 11:50:43.980: d/dalvikvm(32700): wait_for_concurrent_gc blocked 9ms 04-18 11:50:43.980: v/mediaplayer-jni(32700): native_finalize 04-18 11:50:43.980: v/mediaplayer-jni(32700): release 04-18 11:50:43.985: d/androidruntime(32700): shutting down vm 04-18 11:50:43.985: w/dalvikvm(32700): threadid=1: thread exiting uncaught exception (group=0x40d3c2a0) 04-18 11:50:43.990: e/androidruntime(32700): fatal exception: main 04-18 11:50:43.990: e/androidruntime(32700): java.lang.runtimeexception: unable start activity componentinfo{com.drunktxtapp/com.drunktxtapp.cocktaildetail}: java.lang.runtimeexception: content must have listview id attribute 'android.r.id.list' 04-18 11:50:43.990: e/androidruntime(32700): @ android.app.activitythread.performlaunchactivity(activitythread.java:2100) 04-18 11:50:43.990: e/androidruntime(32700): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2125) 04-18 11:50:43.990: e/androidruntime(32700): @ android.app.activitythread.access$600(activitythread.java:140) 04-18 11:50:43.990: e/androidruntime(32700): @ android.app.activitythread$h.handlemessage(activitythread.java:1227) 04-18 11:50:43.990: e/androidruntime(32700): @ android.os.handler.dispatchmessage(handler.java:99) 04-18 11:50:43.990: e/androidruntime(32700): @ android.os.looper.loop(looper.java:137) 04-18 11:50:43.990: e/androidruntime(32700): @ android.app.activitythread.main(activitythread.java:4898) 04-18 11:50:43.990: e/androidruntime(32700): @ java.lang.reflect.method.invokenative(native method) 04-18 11:50:43.990: e/androidruntime(32700): @ java.lang.reflect.method.invoke(method.java:511) 04-18 11:50:43.990: e/androidruntime(32700): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1006) 04-18 11:50:43.990: e/androidruntime(32700): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:773) 04-18 11:50:43.990: e/androidruntime(32700): @ dalvik.system.nativestart.main(native method) 04-18 11:50:43.990: e/androidruntime(32700): caused by: java.lang.runtimeexception: content must have listview id attribute 'android.r.id.list' 04-18 11:50:43.990: e/androidruntime(32700): @ android.app.listactivity.oncontentchanged(listactivity.java:243) 04-18 11:50:43.990: e/androidruntime(32700): @ com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:311) 04-18 11:50:43.990: e/androidruntime(32700): @ android.app.activity.setcontentview(activity.java:1924) 04-18 11:50:43.990: e/androidruntime(32700): @ com.drunktxtapp.cocktaildetail.oncreate(cocktaildetail.java:22)
you need extend listactivity
instead of activity
cocktaildetail
class , xml should have listview
id android:id="@+id/list"
Comments
Post a Comment