Christian Ruppert
Christian Ruppert

Reputation: 3779

Create "Sub-Views" in Android (Newbie-Question)


sorry for the poor choice of words, I dont know exactly how the Android terminology is, therefore google wasn't much help either.

What I need to do seems simple: I have a table as part of my "main view" which is created when the activity launches. No problem here, I can get the table in code and e.g. add rows.

What I would like to do is "prepare" the row in a different XML file, basically creating the layout using XML and then creating an instance of that TableRow in code, adding it to my main table.

I know the alternative approach (to create the whole row in code), but this would be more cumbersome then using XML for most of the attributes, and only fill the ones I need later by code (label and image).

[UPDATE]

Ok, I managed to solve half of my problem using the ListActivity. There is one little problem remaining, I don't know how to tell Android to use more then one string.

The code looks like this:

public class ListScreenAlle extends ListActivity {

String[] listItems = {"exploring", "android", "list", "activities"};

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    // We'll define a custom screen layout here (the one shown above), but
    // typically, you could just use the standard ListActivity layout.
    setContentView(R.layout.list_screen_layout_alle);

    //setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, listItems));
    setListAdapter(new ArrayAdapter(this, R.layout.list_row_single, listItems));
}

}

And the XML used for the row looks like this:

<?xml version="1.0" encoding="utf-8"?><TextView android:id="@+id/text1"  xmlns:android="http://schemas.android.com/apk/res/android"
     android:textSize="16sp"
     android:textStyle="bold"
     android:layout_width="match_parent"
     android:layout_height="wrap_content" android:textColor="@color/text_color_list_label"/>

How do I change this code to support a more complex object than a single string. Eg a person with a thumbnail and firstname, lastname.

The ArrayAdapter in its simple form only allows a single textfield, how would I explain to it that my row has 3 fields, an image, two strings, and that it should "BIND" the image to property Thumbnail, and the textfields to firstname and lastname?

Upvotes: 2

Views: 734

Answers (2)

user634618
user634618

Reputation: 3593

Have a look at ListActivity and/or ListView.

edit regarding your update: use SimpleAdapter instead of ArrayAdapter

Upvotes: 1

Thane Anthem
Thane Anthem

Reputation: 4093

Example similar to what I understand you want to do:

http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

Another example that outputs two strings per row:

WANTED: TableLayout-Like ListView

EDIT: Ah, I see you found a solution.

Upvotes: 0

Related Questions