2011-01-03 16 views
1

Ich habe versucht, einer Listview Subrows hinzuzufügen. Ich habe eine Taxi-App, die derzeit eine Liste von Taxi-Firmennamen zeigt, ich möchte in der Lage sein, einige Unterzeilen hinzufügen, in denen Adresse, Postleitzahl etc. zeigenAndroid - Hinzufügen von Subrows zu einem ListView!

Ich hatte einige Versuche, aber keine waren erfolgreich . Ich rufe meine Strings von einem Array in der Datei strings.xml ab. Mein Code ist im Moment:

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

    final String[] taxi = getResources().getStringArray(R.array.taxi_array); 
    setListAdapter(new ArrayAdapter<String>(this, R.layout.listtaxi, taxi)); 

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

     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(); 
     } 
    }); 

Wenn jemand mich um dieses Problem zu umgehen helfen würde es sehr geschätzt werden.

Die ursprüngliche Frage i ist hier gefragt: Android - Adding Subitem to a listview

Dank

Antwort

3

Whoops, nur diese eine bemerkt. Ich füge meine bearbeitete Antwort hier ein:

Okay, nur für Tritte, ich warf das zusammen. Es kompiliert und korrekt funktioniert, finden Sie, wenn Sie es für Ihre speziellen Bedürfnisse anpassen können:

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.taxi_list_item, 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));  
    } 
} 
+0

Sie sind ein Held! danke – Oli

+0

Tippfehler meinerseits. :) jetzt behoben, sollte taxi_list_item sein. Habe etwas Code von einem anderen meiner Projekte eingefügt. – kcoppock

+0

Entschuldigung, ein Schmerz zu sein, aber es läuft nicht, nachdem ich es gebaut habe. sagt dann ein Fehler beim Laden? Quelle nicht gefunden, wenn ich es im Debug-Modus ausführen. Gibt es irgendeine Chance, dass du als Krug exportieren kannst und mir irgendwo Zugriff darauf hast? – Oli

Verwandte Themen