2


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?

Christian Ruppert
  • 3,749
  • 5
  • 47
  • 72
  • Think I found it, have to check this post http://stackoverflow.com/questions/2265661/how-to-use-arrayadaptermyclass – Christian Ruppert Apr 01 '11 at 11:48
  • Yes, this works... Use the post above if you run into the same problems. With a custom adapter you can add more textfields and adress them likewise, pretty simple.. – Christian Ruppert Apr 01 '11 at 11:58

2 Answers2

1

Have a look at ListActivity and/or ListView.

edit regarding your update: use SimpleAdapter instead of ArrayAdapter

user634618
  • 3,573
  • 1
  • 24
  • 17
  • It worked nearly, I am able to use the list, but only with one string. Could you have another look at the question. Thanks for your help, if you know what to look for, its not that hard :-) – Christian Ruppert Apr 01 '11 at 11:45
0

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.

Community
  • 1
  • 1
Thane Anthem
  • 4,093
  • 4
  • 26
  • 24