I have one Activity in which I try to show some simple messages, and then a list of items. So as I understand, I need two views: one for the simple layout, and one for the list of items which will be handled by the adapter.
Here is my code:
ArrayAdapter<SolutionTopic> adapter;
ArrayList<SolutionTopic> problems = new ArrayList <SolutionTopic>( );
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Load your layout
setContentView(R.layout.solution);
SolutionTopic s = new SolutionTopic ();
s.setSolutionTopicName( "Hello" );
problems.add(s);
adapter = new ArrayAdapter<SolutionTopic>(this, R.layout.solution_topic_list,
R.id.label, problems);
TextView solution_title = (TextView) findViewById(R.id.solution_title);
TextView solution_description = (TextView) findViewById(R.id.solution_description);
setListAdapter (adapter);
}
and here is the solution.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/solution_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label1"
/>
<TextView
android:id="@+id/solution_description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Label2"
/>
</LinearLayout>
and here is the solution_topic_list.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
What I am not sure is how to get these two to render together. Ideally what I want to do is display the TextView messages first, and then the list of items.
Any idea how I can do that?
Thanks!
You shouldn’t remove the ListView from solution.xml, in that ListView your SolutionTopics will be displayed.
To get the big picture, your interface would have three Views:
-TextView: @+id/solution_title
-TextView: @+id/solution_description
-ListView: @android:id/list
The ListView contains an undefined number of entries of solution_topic_list.xml.
PD: The third parameter of ArrayAdapter (R.id.label) should be the id of the TextView in solution_topic_list.xml:
You also should implement toString method in your SolutionTopic class, to get the desired String.