android - Different text color each item of listview when populating using simple adapter -
i'm trying set different text color of each item inside listview when populating item pict below, can't make work,
the idea if number of "rata-rata" exceed 75 text color set black, if below set red.
here's code, i'm overriding getview method :
calonsiswa.add(map); list = (listview) findviewbyid(r.id.list); listadapter adapter = new simpleadapter(seleksinilai.this, calonsiswa, r.layout.activity_seleksi_nilai_single_item_view, new string[] { tag_no_urutan, tag_no_pendaftaran, tag_nama_lengkap, tag_jurusan, tag_rata_rata_nilai, tag_cara_seleksi }, new int[] { r.id.nomorurutan, r.id.nopendaftar, r.id.namapendaftar, r.id.jurusanpendaftar, r.id.rataratanilai, r.id.caraseleksi }) { @override public view getview(int position, view convertview, viewgroup parent) { view view = super.getview(position, convertview, parent); float ratarata = float.parsefloat(rata_rata); int posisi = position; int textcolorid = r.color.black; textview text; text = (textview) view .findviewbyid(r.id.nopendaftar); if (ratarata <= 75) { textcolorid = r.color.red; } else if (ratarata >= 75) { textcolorid = r.color.black; } text.settextcolor(getresources().getcolor( textcolorid)); return view; } };
here's pict if change order lowest highest number, seems problem whenever last number exceed or below 75, change item color inside listview, not specific position
here's complete code :
private class getdata extends asynctask < string, string, jsonobject > { private progressdialog pdialog; string nomor, no_pendaftaran, nama_lengkap, jurusan, rata_rata, cara_seleksi; @ override protected void onpreexecute() { super.onpreexecute(); nopendaftaran = (textview) findviewbyid(r.id.nopendaftar); namapendaftar = (textview) findviewbyid(r.id.namapendaftar); statusproses = (textview) findviewbyid(r.id.rataratanilai); pdialog = new progressdialog(seleksinilai.this); pdialog.setmessage("getting data ..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @ override protected jsonobject doinbackground(string...args) { // getting json url jsonobject json = jparser.getjsonfromurl(url); return json; } @ override protected void onpostexecute(jsonobject json) { pdialog.dismiss(); try { // getting json array url pendaftar = json.getjsonarray(tag_os); (int = 0; < pendaftar.length(); i++) { jsonobject c = pendaftar.getjsonobject(i); // storing json item in variable nomor = string.valueof(i + 1); no_pendaftaran = c.getstring(tag_no_pendaftaran); nama_lengkap = c.getstring(tag_nama_lengkap); jurusan = c.getstring(tag_jurusan); rata_rata = c.getstring(tag_rata_rata_nilai); cara_seleksi = c.getstring(tag_cara_seleksi); // adding value hashmap key => value hashmap < string, string > map = new hashmap < string, string > (); map.put(tag_no_urutan, nomor); map.put(tag_no_pendaftaran, no_pendaftaran); map.put(tag_nama_lengkap, nama_lengkap); map.put(tag_jurusan, jurusan); map.put(tag_rata_rata_nilai, rata_rata); map.put(tag_cara_seleksi, cara_seleksi); /*map.put(tag_status_proses, status_proses);*/ calonsiswa.add(map); list = (listview) findviewbyid(r.id.list); listadapter adapter = new simpleadapter( seleksinilai.this, calonsiswa, r.layout.activity_seleksi_nilai_single_item_view, new string[] { tag_no_urutan, tag_no_pendaftaran, tag_nama_lengkap, tag_jurusan, tag_rata_rata_nilai, tag_cara_seleksi }, new int[] { r.id.nomorurutan, r.id.nopendaftar, r.id.namapendaftar, r.id.jurusanpendaftar, r.id.rataratanilai, r.id.caraseleksi }) {@ override public view getview(int position, view convertview, viewgroup parent) { view view = super.getview(position, convertview, parent); float ratarata = float.parsefloat(rata_rata); int posisi = position; int textcolorid = r.color.black; textview text; text = (textview) view.findviewbyid(r.id.nopendaftar); if (ratarata <= 75) { textcolorid = r.color.red; } else if (ratarata >= 75) { textcolorid = r.color.black; } text.settextcolor(getresources().getcolor(textcolorid)); return view; } }; list.setadapter(adapter); } } catch (jsonexception e) { e.printstacktrace(); } } }
get "rata_rata" position in getview().
rata_rata = calonsiswa.get(position).get(tag_rata_rata_nilai);
Comments
Post a Comment