0

I have a listview and listview has some data,
I hope these data have different color
EX: if this is Lesson 1 color blue and Lesson 2 is color red.....
so,I use

name.setTextColor(Color.BLUE);

but it can not change.
why?


this is my code:

public class Listening extends AppCompatActivity {
    ArrayList<Listenlist> listenlists = new ArrayList<Listenlist>();
    ListView listview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listening);
        listview = (ListView) findViewById(R.id.list);
        seraechsql2();
    }
    public void seraechsql2(){
        View v =null;
        String result = dblisten.executeQuery();

        try{

           TextView name = ((TextView) v.findViewById(R.id.name));
            JSONArray jsonArray = new JSONArray(result);
            length=jsonArray.length();
            for(int i =0; i < jsonArray.length(); i++) 
            {
                JSONObject jsonData = jsonArray.getJSONObject(i);
                String les=jsonData.getString("lesson");
                Listenlist team = new Listenlist("L"+les);
               if(les==.....){//my condition
              name.setTextColor(Color.BLACK);//I try to change it to black but fail
               }
                listenlists.add(team);
                final ListenlistAdapter adapter = new ListenlistAdapter(this, R.layout.listenlist, listenlists);
                listview.setAdapter(adapter);
                listview.setTextFilterEnabled(true);
                listview.setSelector(R.drawable.green);
            }
        }
        catch(Exception e){}
    }
}

thanks

samurai
  • 101
  • 1
  • 13

2 Answers2

1

if you use listview, you must create separate custom layout.simple_list_item_1 with your own customization and you would also need a class to control this layout (add data in it, create new elements etc). It is more complicated than RecyclerView.

Check more details here: https://www.simplifiedcoding.net/custom-listview-android/

0

I would suggest you to use RecyclerView to list display list of items. You can update based color based on the item position. refer this link - https://developer.android.com/guide/topics/ui/layout/recyclerview

More explaination - Changing color of items in ListView - Android

Rohit Padma
  • 603
  • 5
  • 15
  • I have saw that ,I can change color when I click but I want to show different color auto not click.... – samurai May 26 '20 at 02:10
  • @samurai That would be very easy if you can use the recycler view. – Rohit Padma May 26 '20 at 02:15
  • sorry,I still dont know how to use it,please said more clear,thanks – samurai May 26 '20 at 03:20
  • @samurai You can find an example of How to add recycler view list. You can color the item in the onBindViewHolder method (ViewHolder). Refer this link - https://developer.android.com/guide/topics/ui/layout/recyclerview It's pretty straight forward. First try to build the list using recycler view then adding color would be very easy. – Rohit Padma May 26 '20 at 14:13