Reading text files when item is clicked in ListView- Android -
i'm deveoping android app listing songs. when song( item in listview) clicked, lyrics( text file should displayed). when click on listview item, starts new activity displays nothing. i'm not sure what's causing this. because i'm not passing bundles or using them correctly? problem can't pass if statement( in second activity) read txt file. appreciated.
thanks in advance.here's code
first activity
public class listofsongs extends activity { private listview songslistview; private arrayadapter<string> listadapter; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list_of_songs); final string song_one = "songone"; final string song_two = "songtwo"; // find listview resource. songslistview = (listview) findviewbyid(r.id.listofsongs); // create , populate list of songs names. final string[] songs = new string[] { "song1", "song2", "song3", "song4", "song5", "song6", "song7", "song8" }; arraylist<string> songtlist = new arraylist<string>(); songtlist.addall(arrays.aslist(songs)); // create arrayadapter using songs list. // each row in listview textview. textview defined // in file (res/layout/simplerow.xml). listadapter = new arrayadapter<string>(this, r.layout.simplerow, songtlist); // set arrayadapter listview's adapter. songslistview.setadapter(listadapter); songslistview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent startreadingthelyrics = new intent(listofsongs.this, readingthelyrics.class); if (position == 0) { system.out.println("position zero"); startreadingthelyrics.putextra("song1", song_one); startactivity(startreadingthelyrics); } else if (position == 1) { system.out.println("position one"); startreadingthelyrics.putextra("song2", song_two); startactivity(startreadingthelyrics); } } }); }
second activity:
public class readingthelyrics extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.simplerow); intent intent = getintent(); string song1 = intent.getstringextra("song1"); string song2 = intent.getstringextra("song2"); //it's not able pass if statement able read text file if (intent.hasextra(song1)) { try { // create input stream read file inputstream songone = getresources().openrawresource( r.raw.songone); // assign string method down below string lyricone = inputstreamtostring(songone); // textview textview lyrictextview = (textview) findviewbyid(r.id.rowtextview); // set text lyrictextview.settext(lyricone); } catch (ioexception e) { log.e("debug", "inputstreamtostring failure"); } }// end of reading song 1 if (intent.equals(song2)) { system.out.println("chachawee mo bentna"); try { // create input stream read file inputstream songtwo = getresources().openrawresource( r.raw.songtwo); // assign string method down below string lyrictwo = inputstreamtostring(songtwo); // textview textview lyrictextview = (textview) findviewbyid(r.id.rowtextview); // set text lyrictextview.settext(lyrictwo); } catch (ioexception e) { log.e("debug", "inputstreamtostring failure"); } }// end of reading song 1 }// end of oncreate method private string inputstreamtostring(inputstream is) throws ioexception { // create buffer stringbuffer sbuffer = new stringbuffer(); datainputstream dataio = new datainputstream(is); string strline = null; while ((strline = dataio.readline()) != null) { sbuffer.append(strline + "\n"); } dataio.close(); is.close(); return sbuffer.tostring(); } }
activity_list_of_songs.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:background="@android:color/black" tools:context=".listofsongs" > <listview android:layout_width="fill_parent" android:layout_height="fill_parent" android:textcolor="@android:color/white" android:textsize="25sp" android:id="@+id/listofsongs"> </listview> </relativelayout>
simplerow.xml
<textview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rowtextview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10dp" android:textsize="16sp" > </textview>
the if statement incorrect here.
intent intent = getintent(); string song1 = intent.getstringextra("song1"); string song2 = intent.getstringextra("song2"); //it's not able pass if statement able read text file if (intent.hasextra(song1))
when intent.getstringextra("song1")
, song1="songone"
, not key available in intent, intent.hasextra()
returns false , if condition not satisfied.
more info on intents refer here : http://developer.android.com/reference/android/content/intent.html#hasextra(java.lang.string)
Comments
Post a Comment