Hello,

This is the method I used to set the individual lines of a list view to
different colors.

At a high level this list contains a list of companion names for the swtor
game. Companions which are from the imperial side are listed in red,
republic are listed in blue.


==========================================
This is the code for the list array and view:

...
    CompanionName[] newValues; // will store an array.
...
    newValues = new CompanionName[numCursorRows]; // create an array of
proper size.
    c.moveToFirst();
    for (int i=0;i<numCursorRows;i++) {
     // todo: look into this html style later.
     //myTextView.setText(Html.fromHtml("<p style="color:red">" + someText
+ "</p>"));
//     values[i]=Html.fromHtml("<p style="color:red">" + "foo" + "</p>");
     newValues[i] = new CompanionName();
     newValues[i].name=c.getString(0); // This cursor has two columns
companion name and faction side
     newValues[i].faction=c.getString(1); // This cursor has two columns
companion name and faction side
     c.moveToNext();
    }

     // Build the listview
// The adapter determines how to draw on screen.  The listview will ask the
adapter to draw the view
        // with adapter's getView method.
MyAdapter<CompanionName> adapter = new
MyAdapter<CompanionName>(this,android.R.layout.simple_list_item_1,
newValues);

theList.setAdapter(adapter); // theList is the ListView
        theList.setOnItemClickListener(new MyOnItemClickListener(this));
=================
This is the code for the array
public class MyAdapter<T> extends ArrayAdapter<T> {

    private static String TAG = "adapter";
    CompanionName[] myobjects;

public MyAdapter(Context context, int textViewResourceId, T[] objects) {
super(context, textViewResourceId, objects);
myobjects = (CompanionName[]) objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
 View row = super.getView(position, convertView, parent);
Log.d(TAG, "GetView with position "+position);
    TextView text = (TextView) row.findViewById(android.R.id.text1);

    if (null != text) {
   if (myobjects[position].faction.equals("Imperial")) {
    text.setTextColor(Color.parseColor("#7b2325"));   // red
   } else {
    text.setTextColor(Color.parseColor("#64788e"));  // blue
   }
    }
return row;
}
===========================
This is the code for the companionName
public class CompanionName { public String name; public String
faction;     private
static String TAG = "CompanionName"; public CompanionName() { Log.d(TAG,
"constructor hit"); name = "not specified"; faction = "not specified"; }
public String toString() { return name; } }

-- 
John F. Davis

独树一帜

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to