user3622739
user3622739

Reputation:

How to implement a table layout inside a list view

I want to implement a layout which resembles Bank's PassBook. So I thought should I implement table layout inside a list view.

Here is a sample passbook example.

DATE PARTICULARS DEBIT CREDIT BALANCE

12/06/2014 Withdraw 200 10000

11/06/2014 Deposit 200 10200

Upvotes: 2

Views: 2557

Answers (2)

maddy d
maddy d

Reputation: 1536

create xml layout like below

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="horizontal"
    android:weightSum="3">

<TextView
    android:id="@+id/date"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:text="@string/hello_world" />
 <TextView
     android:id="@+id/action_type"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" 
    android:gravity="center"
    android:layout_weight="1"/>
  <TextView
    android:id="@+id/amount"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    android:gravity="center" 
    android:layout_weight="1"/>

 </LinearLayout>

use this layout as list item for your ListView.

Upvotes: 2

tknell
tknell

Reputation: 9047

Use an ArrayAdapter for your ListView.

In there, you can inflate the Layout for the items of the list, be it a TableLayout, or something other.

private class MyListAdapter extends ArrayAdapter<SomeObject> {

    public MyListAdapter(Context context, int resource, List<SomeObject> someObjectList) {

        super(context, resource, someObjectList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view;

        //view recycling
        if (convertView == null) {
            // inflate the single row with whatever layout you like
            view = getLayoutInflater().inflate(R.layout.table_row, parent, false);
        } else {
            view = convertView;
        }


        SomeObject item = getItem(position);

        //fill the view with data
        TextView date = (TextView) view.findViewById(R.id.date);
        date.setText(item.getDate());
        ...

        return view;
     }
     ...
}

Here's a little to read about ListViews and ArrayAdapter: https://github.com/thecodepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView1

Upvotes: 0

Related Questions