java - How can individual item colors be changed upon addition in a listview based on variables in a void function? -
i'm working on app interacts bluetooth device. bluetooth device sends string of data includes signal strength, pass/fall test results, , device id, separated spaces. have void function decodes string of data , adds decoded string arraylist, puts decoded data appropriate variables, , puts recent data different textview. want change textview color depending on results of "passed" variable decoded bluetooth string. want change listview items' colors according pass/fail results, every method i've tried changes of items in listview, or causes app crash.
how individually change listview colors of items based on decoded data different void function?
these chunks of code inside java activity: onviewcreated:
mreadinglog = (listview) view.findviewbyid(r.id.scanlogview); adapter = new arrayadapter<string>(getactivity(), r.layout.listview_layout, r.id.testresults, listitems) { @override public view getview(int position, view convertview, viewgroup parent) { view view = super.getview(position, convertview, parent); listitemshow = (textview) view.findviewbyid(r.id.testresults); if(didpass==1){ listitemshow.setbackgroundcolor(color.green); }else { listitemshow.setbackgroundcolor(color.red); } return view; } };//9/11/17
from decoding function:
listitems.add( "device: " + mconnecteddevicename + system.getproperty("line.separator") + "timestamp: " + currentdateandtime + system.getproperty("line.separator") + "location: " + latitude + " (lat) / " + longitude + " (lon)" + system.getproperty("line.separator") + "phone id: " + telephonymanager.getdeviceid() + system.getproperty("line.separator") + "valid:" + (passed ? "yes" : "no") + system.getproperty("line.separator") + "values: " + "t" + v1 + " / " + v2 + system.getproperty("line.separator") + // "=========================" + system.getproperty("line.separator"));//9/11/17 clickcounter++;//9/11/17 mshowdata.settext(savedata); //kg 8/25/17 if(passed == true) { mshowdata.setbackgroundcolor(color.green); didpass = 1; } else if (passed == false){ mshowdata.setbackgroundcolor(color.red); didpass = 0; } else { mshowdata.setbackgroundcolor(color.white); } adapter.notifydatasetchanged();//9/11/17
listview_layout.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="15dp" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:id="@+id/testresults" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout>
view_list.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/viewlist" android:layout_width="match_parent" android:layout_height="match_parent"> <relativelayout android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" android:paddingleft="4dp" android:paddingright="4dp" android:visibility="visible"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:id="@+id/button_holder" android:orientation="horizontal"> <button android:id="@+id/flipperreturn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="return"/> <button android:id="@+id/button_clear_log" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/clear_log"/> </linearlayout> <linearlayout android:id="@+id/listview_holder" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/button_holder" android:layout_alignparenttop="true" android:orientation="vertical"> <listview android:id="@+id/scanlogview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:divider="@android:color/black" android:dividerheight="1dp" android:footerdividersenabled="false" android:headerdividersenabled="false" android:maxlines="4096" android:paddingtop="15dp" android:scrollbars="vertical" android:text="@string/app_name" android:textappearance="?android:attr/textappearancesmall"/> </linearlayout> </relativelayout> </linearlayout>
this isn't method i've tried. followed other questions such these:
- method shown, still changed of items @ once here
- adding second textview xml file own id , different background colors, , conditionally setting arrayadapter appropriate id. still changed of contents same color time color never changed
- moving arrayadapater , following declarations inside decoding function. caused crash.
- using boolean variables, still change entire listview instead of individual items
- using android.r.layout.simple_list_item_1, android.r.id.text1 instead of custom listviews.
what doing wrong? have link tutorial describes how this? checked replies below nothing implemented worked described or had same problem having.
you need implement getview(int position, view convertview, viewgroup parent) of arrayadapter.
derive class arrayadapter, implement getview return different view based on needs , use new arrayadapter class.
Comments
Post a Comment