android - How tochange color of a textView in onclick listener in a list fragment -
i trying alter color of text view in list fragment after onclick button. im using add together button(addbtn) add together contents list dynamically button(clrbtn) alter color of textview added list im getting nullpointerexception. can please allow me know going wrong
below code :
public class fragmentone extends listfragment { string[] order = new string[] {}; int[] qty; public fragmentone(){} /** declaring arrayadapter set items listview */ simpleadapter adapter1; // keys used in hashmap string[] = { "num","itm","price","qty" }; // ids of views in listview_layout int[] = { r.id.num,r.id.itm,r.id.price,r.id.qty}; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview; button addbtn,clrbtn; rootview = inflater.inflate(r.layout.fragment_1, container, false); addbtn = (button) rootview.findviewbyid(r.id.btnadd); clrbtn = (button) rootview.findviewbyid(r.id.btnclear); addbtn.setonclicklistener (new onclicklistener() { @override public void onclick(view v) { //listfragment fragment=new fragmentprodqtyprice(); fragment fragment=new fragmentexlistview(); bundle bundle = new bundle(); bundle.putint("fragmentid", 1); fragment.setarguments(bundle); fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.frame_container, fragment).commit(); } }); clrbtn.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { /*mainactivity.alist.clear(); adapter1=new simpleadapter(getactivity().getbasecontext(), mainactivity.alist, r.layout.listview_layout, from, to); setlistadapter(adapter1);*/ linearlayout tv=(linearlayout)arg0.findviewbyid(r.id.listtest); tv.setbackgroundcolor(color.parsecolor("#ff0000")); } }); adapter1=new simpleadapter(getactivity().getbasecontext(), mainactivity.alist, r.layout.listview_layout, from, to); setlistadapter(adapter1); homecoming rootview; } @override public void onlistitemclick(listview l, view v, int position, long id) { string prompt = "clicked item: " + getlistview().getitematposition(position).tostring(); toast.maketext(getactivity(),prompt , toast.length_short).show(); }}
edited:
my layout files :
fragment_1.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <linearlayout android:id="@+id/linearlayout02" android:layout_height="wrap_content" android:layout_width="fill_parent" > <button android:id="@+id/btnadd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="add" /> <button android:id="@+id/btnclear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="clr" /> </linearlayout> <listview android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/linearlayout02" /> <textview android:id="@android:id/empty" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/linearlayout02" android:gravity="center_horizontal" android:text="@string/txtempty" /> </relativelayout>
listview_layout.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listtest" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <textview android:id="@+id/num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingbottom="10dp" android:paddingright="10dp" android:paddingtop="10dp" /> <linearlayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" > <textview android:id="@+id/itm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="15dp" /> <textview android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="10dp" /> </linearlayout> <textview android:id="@+id/qty" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:paddingtop="10dp" android:textsize="15sp" /></linearlayout>
try solution: looking kid view in button element. button element doesn't have kid element.
clrbtn.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { layoutinflater inflater = layoutinflater.from(fragmentone.this.getactivity().getapplicationcontext()); view root = inflater.inflate(r.layout.listview_layout,null); linearlayout tv=(linearlayout)root.findviewbyid(r.id.listtest); tv.setbackgroundcolor(color.parsecolor("#ff0000")); } });
android listview android-fragments android-listfragment
No comments:
Post a Comment