change color of item in listview (without click)

99封情书 提交于 2019-12-08 10:26:11

问题


I've tried to change the background color of specific items in a ListView.

first, catch it from database:

   ListAdapter adapter = new ArrayAdapter(getApplicationContext(),
            android.R.layout.simple_list_item_1, db.getAllApps());

    final ListView list = (ListView) findViewById(R.id.ListViewApps);

    list.setAdapter(adapter);

then I will set all apps in different color, if they have the tag activated

// if app is activated in db --> set another colour in ListView
private void setAppCheck(ListView list) {

    List<String> apps = db.getAllApps();

    for (int i = 0; i < list.getCount(); i++) {
        if (db.appActivated(apps.get(i)).equals("activated")) {
            list.setBackgroundColor(0xffaaaaaa);  // it changes ALL items...

        } else {
            // do nothing
        }
    }
}

And there is Problem, with list.setItemChecked(i, true) I can change it with a specific position, but how do I change the Background color of the specific Item in the ListView?

Hope you can help me.


回答1:


The cleanest way to do what you're trying to do is writing your own CursorAdapter supporting two view types: activated apps and deactivated apps. Then in your getView method, when you're inflating your views, you can set the background color accordingly.

Having two item types will make the Android framework automatically pass only convert views of the correct type to getView, so the only time you need to check for the type is during creation.

You may find this answer helpful.

Adapter basics

In Android, Adapters are used to translate your data (in your case from an SQLite database) into Views that can be displayed in listviews, spinners, etc. (AdapterView to be specific). One of the most commonly used ones is the CursorAdapter which has basic infrastructure necessary when the associated data is supposed to be read from a cursor.

You will mainly need three methods in your adapter:
- getViewTypeCount which will tell the framework how many types of views your adapter knows. For you this will be two: activated and deactivated apps.

  • getItemViewType which, when passed a specific position in the data (here: the cursor), is able to decide which of those types that position falls into. For this, you will likely be able to reuse your db.appActivated code, at least in large parts.

  • getView, which, when passed a position, can turn the data associated with that position into a View for display. Let's look at that last part in more depth.

Android does some very nifty stuff to make sure your app is fast and slick and responsive. One of those things is, it will only keep enough views around for all positions in the list that are displayed. So, if you have a list that can display 10 items at a time, but your data holds a million records, it will still only keep 10 views around (well, actually, a few more from when stuff is scrolled off screen, but definitely not the one million it would require for every data record).

When the time comes to actually turn data into visible representations - getView - it will pass an old, previously visible but now off screen view (a recycled view) in as the convertView parameter for you to try adapting it to display the data that's been requeusted. This is because inflating new views is comparatively more expensive than just taking an existing one and changing its texts or images or whatever. The view types you have told it about will help it to only pass the type of convert view into getView that is appropriate for the position that's been requested.

This way, you need to only inflate a new view if the passed convert view is inappropriate somehow. And inappropriate, in this case, usually only means "if it is null". So, usually, what you end up with is something very close to this:

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = // inflate a new view
    }
    // bind the convert view to the data, i.e. set its text views, images, and - in your case - background color
}

A video says more than a thousand words

You may want to watch this Google I/O keynote for a more comprehensive explanation of how it all ties together.



来源:https://stackoverflow.com/questions/22659029/change-color-of-item-in-listview-without-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!