简体   繁体   中英

android listview change color but fail

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

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM