user3866661
user3866661

Reputation: 73

Android: set LinearLayout to be a value in a Listview

I have a ListView, titled myListView, that I would like to populate with 3 LinearLayout elements, titled layout1.xml, layout2.xml, and layout3.xml. All 3 LinearLayout elements are very similar; here is one of them:

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

    <TextView
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Los Angeles" />

    <TextView
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="California" />

</LinearLayout>

My goal is to populate a the ListView (myListView) with these three LinearLayout elements. Does anyone know how I would go about doing this?

Upvotes: 0

Views: 98

Answers (2)

HoppedUpDev
HoppedUpDev

Reputation: 71

As DDsix points out, you really should be using one layout that can handle whatever data you want, and then populate the fields using an adapter. If I had to guess, I'd bet the only difference between your layouts is the text for the city and state.

The documentation explains how to do this very well: http://developer.android.com/guide/topics/ui/layout/listview.html

Basically, you should create a private List<Location> mLocations; variable to hold your locations (Location would be a simple class you define with strings to hold city and state). Then, you can use the following in your adapter.

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = context.getLayoutInflater();
    View view = inflater.inflate(R.layout.my_layout_1, parent);
    Location location = mLocationList.get(position);
    TextView cityView = view.findViewById(R.id.city_view);
    TextView stateView = view.findViewById(R.id.state_view);
    cityView.setText(location.getCity());
    cityView.setText(location.getState());
    return view;
}

Upvotes: 0

DDsix
DDsix

Reputation: 1985

First of all, if all three linear layouts are alike, I suggest you to only use one.

Anyway, you have to use a custom adapter for your ListView. You create a class that extends ArrayAdapter for example. If you are not familiar with custom adapters, I suggest you take a look here.

In your getView method, you practically have to inflate a different *.xml, depending on your cell position. Thus:

@Override
puclic View getView (int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    switch (position) {
        case 0: view = inflater.inflate(R.layout.my_layout_1, null, true);
        //rest of my code
            break;
        case 1: view = inflater.inflate(R.layout.my_layout_2, null, true);
        //rest of my code
            break;
        case 2: view = inflater.inflate(R.layout.my_layout_3, null, true);
        //rest of my code
            break;
        default: break;
    //rest of my code

    return view;
}

Upvotes: 1

Related Questions