android - Dynamically put values to a text view in a Layout and put that layout inside another layout. -
i tried , getting java:null pointer exception @ line try put 1 layout another.
this mainactivity.java:
import java.util.arraylist; import java.util.set; import android.os.bundle; import android.app.activity; import android.content.context; import android.content.sharedpreferences; import android.content.sharedpreferences.editor; import android.view.layoutinflater; import android.view.menu; import android.view.view; import android.widget.spinner; import android.widget.tablelayout; import android.widget.textview; public class mainactivity extends activity { private sharedpreferences sp; private tablelayout enterstocktablelayout; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); arraylist<itemdata> list= new arraylist<itemdata>(); list.add(new itemdata("indian rupee",r.drawable.india)); list.add(new itemdata("us dollar",r.drawable.usa_dollar)); spinner sp1=(spinner) findviewbyid(r.id.spinner1); spinneradapter adapter=newspinneradapter(this,r.layout.spinnerlayout,r.id.txt,list); sp1.setadapter(adapter); spinner sp2=(spinner) findviewbyid(r.id.spinner2); sp2.setadapter(adapter); sp= getsharedpreferences("currlist",mode_private); insertintosp(sp); updatecurrlist(sp); } public void updatecurrlist(sharedpreferences spd) { string[] curr=spd.getall().entryset().toarray(new string[0]); for(int i=0;i<curr.length;i++) { string yqlquerryfirst=""; string yqlquerrysecond=""; string yqlfinalquery=yqlquerryfirst+curr[i]+yqlquerrysecond; insertintolayout(curr[i],i); } } private void insertintolayout(string string, int i) { layoutinflater inflater=(layoutinflater) getsystemservice(context.layout_inflater_service); view newrow=inflater.inflate(r.layout.currencylistview,null); textview newrowtextview=(textview) newrow.findviewbyid(r.id.currtextview); textview newrowrateview=(textview) newrow.findviewbyid(r.id.changetextview); textview newrowotherview=(textview) newrow.findviewbyid(r.id.valuetextview); newrowtextview.settext(string); enterstocktablelayout.addview(newrow,i); } public void insertintosp(sharedpreferences spd) { editor edt=spd.edit(); edt.putstring("eur/usd","eurusd"); edt.putstring("usd/jpy","usdjpy"); edt.putstring("gbp/usd","gbpusd"); edt.putstring("usd/chf","usdchf"); edt.putstring("usd/cad","usdcad"); edt.putstring("eur/jpy","eurjpy"); edt.putstring("aud/usd","audusd"); edt.commit(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
here activity_main.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:background="#126180" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <tablerow android:id="@+id/tablerowhead" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginbottom="5dp" android:layout_alignparentleft="true" android:layout_alignparenttop="true" > <textview android:id="@+id/curenheadtextview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/currencycon" android:textappearance="?android:attr/textappearancelarge" android:textcolor="#ffffff" /> </tablerow> <scrollview android:id="@+id/scrollview1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignleft="@+id/tablerowhead" android:layout_below="@+id/tablerowhead" android:background="@drawable/splash" > <relativelayout android:layout_width="fill_parent" android:layout_height="700dp" android:layout_margintop="14dp" android:background="@android:color/transparent" > <tablerow android:id="@+id/tablerow1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_margintop="29dp" > <edittext android:id="@+id/edittext1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="7" android:inputtype="numberdecimal" android:textcolor="#ffffff" /> <spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:entries="@array/countrylist1" /> </tablerow> <tablerow android:id="@+id/tablerow2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/tablerow1" android:layout_margintop="38dp" > <edittext android:id="@+id/edittext2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="7" android:inputtype="numberdecimal" android:textcolor="#ffffff" /> <spinner android:id="@+id/spinner2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffff" android:entries="@array/countrylist1" /> </tablerow> <tablerow android:id="@+id/tablerow3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/tablerow2" android:layout_margintop="11dp" android:background="#126180" > <textview android:id="@+id/currtextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="15dp" android:layout_margintop="5dp" android:layout_marginbottom="5dp" android:text="@string/currenncieslabel" android:textappearance="?android:attr/textappearancemedium" android:textcolor="#ffffff" /> </tablerow> <tablelayout android:id="@+id/enterstocktablelayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/tablerow3" android:padding="5dp" android:stretchcolumns="yes" > </tablelayout> </relativelayout> </scrollview> </relativelayout>
and currencylistview.xml
<?xml version="1.0" encoding="utf-8"?> <tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/currencylistviewlayout" android:layout_width="match_parent" android:layout_height="match_parent" > <tablerow android:id="@+id/tablerow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <textview android:id="@+id/currtextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/valuetextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" /> <textview android:id="@+id/changetextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#d3d3d3" android:padding="5dp" /> </tablerow> <textview android:id="@+id/desctextview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </tablelayout>
i want insert values dynamically currencylistview.xml , insert table layout in currencylistview.xml activity_main.xml.
your variable enterstocktablelayout not initialized. should following in oncreate after setcontentview:
enterstocktablelayout = (tablelayout)findviewbyid(r.id.enterstocktablelayout);
Comments
Post a Comment