views:

46

answers:

1

I have a custom list adapter contains a layout with controls in it. I was wondering how I can get each individual controls id? I am planning on changing the color of some textviews.

private void UpdateDisplay() {
    // define the list which holds the information of the list
    List<Map<String, Object>> resourceNames = new ArrayList<Map<String, Object>>();

    // define the map which will hold the information for each row
    Map<String, Object> data;

    if (appts.size() > 0) {
        for (int i = 0; i < appts.size(); i++) {
            data = new HashMap<String, Object>();

            data.put("apptListType", appts.get(i).getType().getName());
            data.put("apptListTime", DateFormat.getTimeInstance(
                    DateFormat.SHORT).format(appts.get(i).getStartDate())
                    + " - "
                    + DateFormat.getTimeInstance(DateFormat.SHORT).format(
                            appts.get(i).getEndDate()));
            data.put("apptListContactName", appts.get(i).getContact()
                    .getFullName());
            data.put("apptListContactCompany", appts.get(i).getContact()
                    .getCompany());
            resourceNames.add(data);
        }
    } else {

        data = new HashMap<String, Object>();

        data.put("apptListType", "No appointments found for today.");
        data.put("apptListTime", "");
        data.put("apptListContactName", "");
        data.put("apptListContactCompany", "");
        resourceNames.add(data);

    }

    apptDescription = new SimpleAdapter(this, resourceNames,
            R.layout.apptlistitem, new String[] { "apptListType",
                    "apptListTime", "apptListContactName",
                    "apptListContactCompany" }, new int[] {
                    R.id.apptListType, R.id.apptListTime,
                    R.id.apptListContactName, R.id.apptListContactCompany });
    ListView apptList = (ListView) findViewById(R.id.apptsList);
    apptList.setAdapter(apptDescription);

    apptList.setOnItemClickListener(AppointmentDailyView.this);

    apptList.setItemsCanFocus(true);

and here is my xml file for each listitem:

<?xml version="1.0" encoding="utf-8"?>

<TextView android:id="@+id/apptListType" android:textSize="20px"
    android:textColor="#333" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:textStyle="bold" />

<TextView android:layout_below="@+id/apptListType" android:id="@+id/apptListTime"
    android:textSize="15px" android:textColor="#666" android:layout_width="wrap_content"
    android:layout_height="wrap_content" ></TextView>
<TextView android:layout_below="@+id/apptListTime" android:id="@+id/apptListContactName"
    android:textSize="20px" android:textColor="#333" android:layout_width="wrap_content"
    android:layout_height="wrap_content"></TextView>

<TextView android:layout_toRightOf="@+id/apptListContactName"
    android:layout_below="@+id/apptListTime" android:id="@+id/apptListContactCompany"
    android:textSize="15px" android:textColor="#666"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:paddingLeft="5px" android:layout_marginTop="3px"></TextView>

Any help is appreciated, thanks.

+2  A: 

Step #1: Extend SimpleAdapter into your own class (referred to here as MyAdapter)

Step #2: Override getView() in MyAdapter

Step #3: Chain to the superclass (i.e., call super.getView()) from getView(), which returns the View for this row

Step #4: Call findViewById() on the row View to get at the widgets inside of it, and tailor those as you see fit

Step #5: Return the row View from getView() in MyAdapter

Step #6: Use MyAdapter where you are using SimpleAdapter

CommonsWare