2

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

<string name="app_name">Taxi Me</string>
<string-array name="taxi_array">
    <item>Barrys Taxi</item>
    <item>Boom Taxi</item>

</string-array>

What I was trying to do is create subitems for these so that i can show fields such as address and contact details etc. I made a customlistview a while back but cant work out how I can do it using the strings.xml file? Are there any particular tags I need to use so they show up in the list view?

Main Activity Code:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final String[] taxi = getResources().getStringArray(R.array.taxi_array);
    final String[] address = getResources().getStringArray(R.array.taxi_add);

    setListAdapter(new ArrayAdapter<String>(this, R.layout.listtaxi, taxi));

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    for (int i = 0; i < taxi.length; i++) {
        lv.add(new ListTaxi (taxi[i], address[i]));
    }

 /*lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        // When clicked, show a toast with the TextView text
        Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
            Toast.LENGTH_LONG).show();
      }
    });
   */
        lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> a, View v, final int position, long id)
       {   
            final int selectedPosition = position;
            AlertDialog.Builder adb=new AlertDialog.Builder(ListTaxi.this); 
             adb.setTitle("Taxi Booking");
             adb.setMessage("You Have Selected: "+lv.getItemAtPosition(position)); 
             adb.setPositiveButton("Book", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                     Intent intent = new Intent(getApplicationContext(), Booking.class);
                     intent.putExtra("booking",  taxi[selectedPosition]);
                     startActivity(intent);
                 }
             });
             adb.setNegativeButton("Cancel", null); 
             adb.show(); 
         }
     });
3

3 Answers 3

7

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"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:padding="10dp"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/taxi_name"
        />
    <TextView
        android: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"?>
<ListView
    xmlns: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;

public class TaxiMain extends ListActivity {
    /** Called when the activity is first created. 
     * @return */

    class Taxi {
        private String taxiName;
        private String taxiAddress;

        public String getName() {
            return taxiName;
        }

        public void setName(String name) {
            taxiName = name;
        }

        public String getAddress() {
            return taxiAddress;
        }

        public void setAddress(String address) {
            taxiAddress = address;
        }

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

    public class TaxiAdapter extends ArrayAdapter<Taxi> {
        private ArrayList<Taxi> items;
        private TaxiViewHolder taxiHolder;

        private class TaxiViewHolder {
            TextView name;
            TextView address; 
        }

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

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

            Taxi taxi = items.get(pos);

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

            return v;
        }
    }

    @Override
    public void onCreate(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 = new ArrayList<Taxi>();

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

        setListAdapter(new TaxiAdapter(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:

class Taxi {
    String taxiName;
    String taxiAddress;
    String taxiPhone;

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

private List<Taxi> taxiList;

@Override
public void onCreate(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 = new ArrayList<Taxi>();

    for (int i = 0; i < taxiNames.length; i++) {
        taxiList.add(new Taxi(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).

12
  • im looking to move it onto a db eventually but atm just wanted to kind of pull it from strings. how would i go about doing this with a csv file? is it easier? Thanks
    – Oli
    Jan 3, 2011 at 19:39
  • It'd be easier in my opinion, because you could do the whole "database" in Excel (or OO Calc). You basically just set up the spreadsheet, and export it to CSV (comma-separated values). Of course, this is assuming that your content will not contain any commas. Since you have an address field, you'll likely need commas, in which case you could use something like a pipe (|) for a delimiter. I don't think Excel lets you choose your delimiter, though...but you could always use tab-delimited and then do a find > replace tabs with pipes. Then... Jan 3, 2011 at 19:49
  • you'd just have to read in the file from your assets folder, and then process it manually (read until you reach a pipe, discard the pipe, read the next item, etc.). So no, probably not EASIER than this method, haha, but less prone to accidental error (different arrays in different orders, or something like that). Jan 3, 2011 at 19:51
  • Hi. ive given it a go but cant work out whats up with it. ive added my code for the class to see if u can see what im doing wrong. i didnt think i needed to declare the strings in the code as they already exist in the strings.xml file?
    – Oli
    Jan 3, 2011 at 21:21
  • oh btw theres an error at line: for (int i = 0; i < taxi.length; i++) { lv.add(new ListTaxi (taxi[i], address[i])); } Cheers
    – Oli
    Jan 3, 2011 at 21:36
3

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!!

    <string name="app_name">Taxi Me</string>
    <string-array name="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>
0

Are you looking for some sort of nested Lists?
Have a look at ExpandableListView:
http://developer.android.com/reference/android/widget/ExpandableListView.html
and
http://mylifewithandroid.blogspot.com/2008/05/expandable-lists.html

2
  • No not one of those. if you take a look at the link i posted on a comment further up you'll see what i meant. sorry if i wasnt clear.
    – Oli
    Jan 3, 2011 at 21:49
  • Ahh, okay - if you're still having trouble, check out androidza.co.za/working-with-listview-part-1
    – jcuenod
    Jan 4, 2011 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.