Skip to content Skip to sidebar Skip to footer

Adding Subitem To A Listview In Android

I currently have a listview which contains a couple of strings. These are called from a string array in strings.xml Taxi Me

Solution 1:

EDIT: Okay, just for kicks, I threw this together. It compiles and functions correctly, see if you can adapt it for your particular needs:

layout/taxi_list_item.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="100dp"android:padding="10dp"android:orientation="vertical"
    ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/taxi_name"
        /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/taxi_address"
        /></LinearLayout>

layout/main.xml

<?xml version="1.0" encoding="utf-8"?><ListViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+android:id/list"android:layout_width="match_parent"android:layout_height="match_parent" 
    />

TaxiMain.java

package com.test.taxi;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

publicclassTaxiMainextendsListActivity {
    /** Called when the activity is first created. 
     * @return */classTaxi {
        private String taxiName;
        private String taxiAddress;

        public String getName() {
            return taxiName;
        }

        publicvoidsetName(String name) {
            taxiName = name;
        }

        public String getAddress() {
            return taxiAddress;
        }

        publicvoidsetAddress(String address) {
            taxiAddress = address;
        }

        publicTaxi(String name, String address) {
            taxiName = name;
            taxiAddress = address;
        }
    }

    publicclassTaxiAdapterextendsArrayAdapter<Taxi> {
        private ArrayList<Taxi> items;
        private TaxiViewHolder taxiHolder;

        privateclassTaxiViewHolder {
            TextView name;
            TextView address; 
        }

        publicTaxiAdapter(Context context, int tvResId, ArrayList<Taxi> items) {
            super(context, tvResId, items);
            this.items = items;
        }

        @Overridepublic View getView(int pos, View convertView, ViewGroup parent) {
            Viewv= convertView;
            if (v == null) {
                LayoutInflatervi= (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.feed_view, null);
                taxiHolder = newTaxiViewHolder();
                taxiHolder.name = (TextView)v.findViewById(R.id.taxi_name);
                taxiHolder.address = (TextView)v.findViewById(R.id.taxi_address);
                v.setTag(taxiHolder);
            } elsetaxiHolder= (TaxiViewHolder)v.getTag(); 

            Taxitaxi= items.get(pos);

            if (taxi != null) {
                taxiHolder.name.setText(taxi.getName());
                taxiHolder.address.setText(taxi.getAddress());
            }

            return v;
        }
    }

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] taxiNames = getResources().getStringArray(R.array.taxi_name_array);
        String[] taxiAddresses = getResources().getStringArray(R.array.taxi_address_array);

        ArrayList<Taxi> taxiList = newArrayList<Taxi>();

        for (inti=0; i < taxiNames.length; i++) {
            taxiList.add(newTaxi(taxiNames[i], taxiAddresses[i]));
        }

        setListAdapter(newTaxiAdapter(this, R.layout.taxi_list_item, taxiList));      
    }
}

_____END EDIT_______

You'd probably be better off using a database for something like this, to keep the records tied together. If you're set on using arrays, one thing you could do is make a separate array for each item you need (e.g. taxi_array, taxi_address_array, taxi_phone_array) then make a Taxi object in your code:

classTaxi {
    String taxiName;
    String taxiAddress;
    String taxiPhone;

    publicTaxi(String name, String address, String phone) {
        taxiName = name;
        taxiAddress = address;
        taxiPhone = phone;
    }
}

privateList<Taxi> taxiList;

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    String[] taxiNames = getResources().getStringArray("taxi_array");
    String[] taxiAddresses = getResources().getStringArray("taxi_address_array");
    String[] taxiPhones = getResources().getStringArray("taxi_phone_array");
    taxiList = newArrayList<Taxi>();

    for (int i = 0; i < taxiNames.length; i++) {
        taxiList.add(newTaxi(taxiNames[i], taxiAddresses[i], taxiPhones[i]));
    }
}

(This is uncompiled code, some tweaks may be needed) But then you'll have a List of Taxi items, containing all of the compiled information from the different arrays. A database would still be a much better option (or even a CSV file with the data, in your assets).

Solution 2:

I had same problem and I solved myself like this: you can simply add subitem like this code and you don't need so much coding!!

<stringname="app_name">Taxi Me</string><string-arrayname="taxi_array"><item><item>Barrys Taxi</item><item>adress</item><item>contact</item><item>ANY THING...</item></item><item><item>Boom Taxi</item><item>adress</item><item>contact</item><item>ANY THING...</item></item></string-array>

Solution 3:

Post a Comment for "Adding Subitem To A Listview In Android"