-3

Hi I'd like to made a 2 vertical listview to show some data from server. But it's getting a lot of errors.. Please help me to fix it.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
   <TextView android:id="@+id/data"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/data view"
    />

    <ListView
    android:id="@+id/date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:stackFromBottom="true" >
 </ListView>
<ListView
    android:id="@+id/result"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:stackFromBottom="true" >
</ListView>
</LinearLayout>

What I'm after:

https://i.stack.imgur.com/dZQNH.png

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
warmheart
  • 3
  • 2

1 Answers1

0

I will give you an answer that resembles what you want, but you are free to tweak it. Just to note: RecyclerView is mostly used nowadays, but since you asked for a ListView example I will give you an example with a ListView.

First of all you need to specify one ListView in your main activity's layout xml file. You also need a second layout for each item in the ListView. This second layout is going to have two TexViews. The first for Date and the second for Result. And finally you need an adapter for that ListView, which is responsible for inflating each item's (of the list) layoout, keep your list data, etc.

Here is a sample activity_main.xml layout. I added two views to resemble the lines between TextViews:

<RelativeLayout
    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"
    android:padding="16dp"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/relativeLayout" >

       <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Date"
            android:id="@+id/textViewDate"
            android:gravity="center"
            android:layout_toLeftOf="@+id/view2"
            android:layout_toStartOf="@+id/view2"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray"
            android:id="@+id/view1"
            android:layout_alignBottom="@+id/textViewDate" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:id="@+id/view2"
            android:layout_alignBottom="@+id/textViewDate" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Result"
            android:id="@+id/textViewResult"
            android:gravity="center"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_toRightOf="@+id/view2"
            android:layout_toEndOf="@+id/view2" />

    </RelativeLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:layout_below="@+id/relativeLayout">


    </ListView>

</RelativeLayout>

The list_item.xml layout for each item in the list:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Date"
        android:id="@+id/textViewDate"
        android:gravity="center"
        android:layout_toLeftOf="@+id/view"
        android:layout_toStartOf="@+id/view"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/view"
        android:layout_alignBottom="@+id/textViewDate" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Result"
        android:id="@+id/textViewResult"
        android:gravity="center"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_toRightOf="@+id/view"
        android:layout_toEndOf="@+id/view"
        android:layout_alignBottom="@+id/textViewDate" />

</RelativeLayout>

Then a class for your custom item that holds a date and a result.

public class MyItem {
    public GregorianCalendar date;
    public String result;

    public MyItem(GregorianCalendar date, String result){
        this.date = date;
        this.result = result;
    }
}

and the adapter (here ArrayAdapter):

public class MyAdapter extends ArrayAdapter<MyItem> {

    private final Context mContext;
    private MyItem[] mItems;

    public MyAdapter(Context context, MyItem[] items) {
        super(context, -1, items);
        mContext = context;
        mItems = items;
    }

    @Override
    public View getView(int position,View view,ViewGroup parent) {
        View rowView = view;
        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            rowView = inflater.inflate(R.layout.list_item, null, true);

        }

        TextView textViewDate = (TextView) rowView.findViewById(R.id.textViewDate);
        TextView textViewResult = (TextView) rowView.findViewById(R.id.textViewResult);

        textViewDate.setText(mItems[position].date.getTime().toString());
        textViewResult.setText(mItems[position].result);
        return rowView;
    }
}

Finally in your activity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyItem[] items = {new MyItem(new GregorianCalendar(2014, 1, 1), "Ok"),
                          new MyItem(new GregorianCalendar(2014, 2, 3), "Not ok"),
                          new MyItem(new GregorianCalendar(2015, 3, 3), "Ok"),
                          new MyItem(new GregorianCalendar(2015, 5, 7), "Warning"),
                          new MyItem(new GregorianCalendar(2016, 7, 1), "Ok")};

        MyAdapter adapter = new MyAdapter(this, items);

        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
    }
tchar
  • 838
  • 9
  • 12