dezull
dezull

Reputation: 1010

Creating a list with each item containing different elements in Android

I am new to Android development. I have created an Activity with the ListView like interface, but without using ListView as each item(row) in the list might have different elements. I have successfully created it by including LinearLayouts inside the ScrollView, but the XML looks messy. I have also added onClickListener to each of the inner LinearLayouts.

Is there a better way of doing this?

Thanks.

Regards,

dezull

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  android:layout_height="fill_parent"
  android:orientation="vertical" 
  android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
 <ScrollView 
  android:id="@+id/ScrollView01" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:layout_weight="1">
  <LinearLayout 
   android:id="@+id/LinearLayout02" 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" 
   android:orientation="vertical">
   <LinearLayout 
    android:id="@+id/wo_status_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"
    android:background="@color/desc_button"
    android:focusable="true"
    android:padding="3dp">
    <TextView
     android:id="@+id/wo_status"
     android:text="@string/wo_status"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <TextView
     android:id="@+id/wo_status"
     android:text="Waiting for Parts"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
   </LinearLayout>
   <LinearLayout 
    android:id="@+id/wo_prob_desc_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"
    android:background="@color/desc_button"
    android:focusable="true"
    android:padding="3dp">
    <TextView
     android:id="@+id/wo_prob_desc"
     android:text="@string/wo_status"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
    <TextView
     android:id="@+id/wo_prob_desc"
     android:text="New problem"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
   </LinearLayout>
  </LinearLayout>
 </ScrollView>
</LinearLayout>

Upvotes: 0

Views: 423

Answers (2)

James
James

Reputation: 36

I'm the lead developer for Ledgerist. What we did was create a subclass of Adapter that implemented getView as suggested above.

Inside the getView method, you'll create a ViewHolder object as I'm sure is shown in many tutorials across the web. You'll want to create an instance of each control that you may want to show inside the ViewHolder. When you get to implementing the cell, you'll want to set all the controls that you don't want to display to GONE using their setVisibility method.

Hope this is helpful,

James K., Lead Developer www.vervv.com | [email protected]

Follow us on Twitter: @vervv http://twitter.com/vervv

Upvotes: 2

asaad399
asaad399

Reputation: 31

you can subclass Adapter & implement getView(); Adapter lets you use more than one View and you can setOnClickListener for any; to retuen linearLayouts with children u like; android already has adapter subclasses for various purposes ; ArrayAdapter, SimpleAdapter ,CursorAdapter, ..etc; then ListView.setAdapter(yourAdapter);

another trick is to use adapter with scrollView; set LinearLayout as scrollView child then linearLayout.addview(adapter.getView(...));

hope yhis helps;

Upvotes: 0

Related Questions