android - Change TextView when item is clicked on a ListAdapter? -
i have 2 classes , xml. first 1 creates listapter full of cocktail names bloody mary,margarita, ect. second class set can change textview , image displayed in xml. i'm having difficulty trying pass item i'v pressed in listadapter cocktaildetail class change values of textview , image. can help? runs opens bloody mary every item selected in list.
menu class
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("cocktail_name","bloodymary"); startactivity(ourintent); }; }
cocktaildetail 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 activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.cocktaildetail); imageview imageview1 = (imageview)findviewbyid(r.id.imagecocktail); imageview1.setimagedrawable(getresources().getdrawable(r.drawable.bloodymary)); button b1 = (button) findviewbyid(r.id.buttonyoutube); textview t1 = (textview)findviewbyid(r.id.textcocktailname); string cocktailname = "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"))); } }); } }
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>
in menu class, passing hard coded string :
ourintent.putextra("cocktail_name",**"bloodymary"**);
change value retrieved listview :
string selectedfromlist =(string) (l.getitematposition(position)); ourintent.putextra("cocktail_name",selectedfromlist);
also in cocktail class, oncreate
, again setting text hardcoded string. using bundle
, retrieve value , settext;
bundle bundle = getintent().getextras(); //extract data… string name = bundle.getstring(“cocktail_name”); textview t1 = (textview)findviewbyid(r.id.textcocktailname); t1.settext(name);
Comments
Post a Comment