java - Colouring for Wearable ListView Items -
is there way change text colour of list items on android wear? i've seen before , hence done phones don't think same code can used wear.
activity code
public class mainactivity extends activity implements wearablelistview.clicklistener{ private wearablelistview mlistview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mycoloringadapter adapter = new mycoloringadapter(this,listitems); final watchviewstub stub = (watchviewstub) findviewbyid(r.id.watch_view_stub); stub.setonlayoutinflatedlistener(new watchviewstub.onlayoutinflatedlistener() { @override public void onlayoutinflated(watchviewstub stub) { mlistview = (wearablelistview) stub.findviewbyid(r.id.listview1); mlistview.setadapter(new myadapter(mainactivity.this)); mlistview.setclicklistener(mainactivity.this); } }); } private class mycoloringadapter extends arrayadapter<string> { private final context context; private final string[] values; public mycoloringadapter(context context, string[] values) { super(context, r.layout.row_simple_item_layout, values); this.context = context; this.values = values; } @override public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); view rowview = inflater.inflate(r.layout.row_simple_item_layout, parent, false); textview textview = (textview) rowview.findviewbyid(r.id.list_item); // set text textview.settext(values[position]); // set color depending on position int textcolorid = r.color.white; // default color switch (position) { case 0: textcolorid = r.color.red; break; case 1: textcolorid = r.color.yellow; break; case 2: textcolorid = r.color.green; break; } textview.settextcolor(getresources().getcolor(textcolorid)); return rowview; } } private static arraylist<string> listitems; static { listitems = new arraylist<string>(); listitems.add("item 1"); listitems.add("item 2"); listitems.add("item 3"); } @override public void onclick(wearablelistview.viewholder viewholder) { } @override public void ontopemptyregionclick() { } private class myadapter extends wearablelistview.adapter { private final layoutinflater minflater; private myadapter(context context) { minflater = layoutinflater.from(context); } @override public wearablelistview.viewholder oncreateviewholder(viewgroup parent, int viewtype) { return new wearablelistview.viewholder( minflater.inflate(r.layout.row_simple_item_layout, null)); } @override public void onbindviewholder(wearablelistview.viewholder holder, int position) { textview view = (textview) holder.itemview.findviewbyid(r.id.list_item); view.settext(listitems.get(position).tostring()); holder.itemview.settag(position); } @override public int getitemcount() { return listitems.size(); } } }
colouring adapter code
private class mycoloringadapter extends arrayadapter<string> { private final context context; private final string[] values; public mycoloringadapter(context context, string[] values) { super(context, r.layout.list_item, values); this.context = context; this.values = values; } @override public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); view rowview = inflater.inflate(r.layout.list_item, parent, false); textview textview = (textview) rowview.findviewbyid(r.id.list_item); textview.settext(values[position]); int textcolorid = r.color.white; // default color switch (position) { case 0: textcolorid = r.color.red; break; case 1: textcolorid = r.color.yellow; break; case 2: textcolorid = r.color.green; break; } textview.settextcolor(getresources().getcolor(textcolorid)); return rowview; } }
error
constructor mycoloringadapter in class mainactivity.mycoloringadapter cannot applied given types; required: context,string[] found: mainactivity,arraylist reason: actual argument arraylist cannot converted string[] method invocation conversion
warnings
for mycoloringadapter adapter = new mycoloringadapter(this,listitems);
variable 'adapter' never used
constructor mycoloringadapter in class mainactivity.mycoloringadapter cannot applied given types; required: context,string[] found: mainactivity,arraylist reason: actual argument arraylist cannot converted string[] method invocation conversion
the problem constructor of arrayadpater
. takes string[]
in activity passing arraylist<string>
change
private class mycoloringadapter extends arrayadapter<string> { private final context context; private final string[] values; public mycoloringadapter(context context, string[] values) { super(context, r.layout.list_item, values); this.context = context; this.values = values; }
to
private class mycoloringadapter extends arrayadapter<string> { private final context context; private final arraylist<string> values; public mycoloringadapter(context context, arraylist<string> values) { super(context, r.layout.list_item, values); this.context = context; this.values = values; }
instead of textview.settext(values[position]);
have textview.settext(values.get(position));
.
as side node should recycle convertview
edit: did notice have 2 adapter. need 1 adapter, myadapter
, in case. rid of mycoloringadapter
. if want change textcolor, update onbindview
@override public void onbindviewholder(wearablelistview.viewholder holder, int position) { textview view = (textview) holder.itemview.findviewbyid(r.id.list_item); view.settext(listitems.get(position).tostring()); holder.itemview.settag(position); int textcolorid = r.color.white; // default color switch (position) { case 0: textcolorid = r.color.red; break; case 1: textcolorid = r.color.yellow; break; case 2: textcolorid = r.color.green; break; } view.settextcolor(getresources().getcolor(textcolorid)); }
Comments
Post a Comment