Kein
Kein

Reputation: 996

Android design practice - switch view

I'm new in android dev, actualy I'm web-developer. And question is similar to html+css classes.

In my application I have simple listView, with custom row view. For different state of row, I need to switch many child elements of row, like color, background color, visiblity of some elements.

Of cource I can do this in code of my CustomArrayAdapter. But it's look like dirty trick.

How can I do this "beautiful"? Have android API something similar to classes in HTML?

public View getView(int position, View convertView, ViewGroup parent) {
    // ...
    Integer items_count = merc.getItemsCount(); String item_count_label = "";
    if(0 < items_count){
        item_count_label = String.valueOf( items_count );
    } else {
        item_count_label = "";
    }
    countTextView.setText( item_count_label );

    return rowView;
}

<TextView
    android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Item label"
    android:focusable="false"
    android:textSize="18dp"
    android:textIsSelectable="true">
</TextView>
<TextView
    android:id="@+id/profit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="12dp"
    android:textColor="#00aa00"
    android:layout_weight="1"
    android:layout_marginLeft="15px">
</TextView>
<TextView
    android:id="@+id/count"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="6"
    android:textSize="18dp" >
</TextView>

Upvotes: 0

Views: 295

Answers (1)

Karakuri
Karakuri

Reputation: 38595

Normally you would override getViewTypeCount() and return the number of different row "types" that your ListView can have (let's say N types), and override getItemViewType() to return a value from 0 to (N - 1). Then in getView() you can call getItemViewType(position) and switch over the return value to inflate the proper row layout and do whatever you need to do.

I suggest you watch this video: The World of ListView

Upvotes: 1

Related Questions