2011-01-04 4 views
0

J'ai actuellement une liste montrant deux tableaux de chaînes. Je me demandais si quelqu'un savait s'il était possible d'ajouter un bouton à une rangée dans une liste, de sorte que lorsqu'on clique dessus, il charge l'écran de la carte?Android - Ajout d'un bouton à une liste pour afficher MapView

Mon code de la classe est suit -

class Taxi { 
    private String taxiName; 
    private String taxiAddress; 
    private String taxiDist; 

    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 String getDist() { 
     return taxiDist; 
    } 
    public void setDist(String dist){ 
     taxiDist = dist; 
    } 
    public Taxi(String name, String address, String dist) { 
     taxiName = name; 
     taxiAddress = address; 
     taxiDist = dist; 
    } 
} 

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

    private class TaxiViewHolder { 
     TextView name; 
     TextView address; 
     TextView dist; 
    } 

    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); 
      taxiHolder.dist = (TextView)v.findViewById(R.id.taxi_dist); 
      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()); 
      taxiHolder.dist.setText(taxi.getDist()); 
     } 

     return v; 
    } 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listtaxi); 

    final String[] taxiNames = getResources().getStringArray(R.array.taxi_name_array); 
    final String[] taxiAddresses = getResources().getStringArray(R.array.taxi_address_array); 
    final String[] taxiDist = getResources().getStringArray(R.array.taxi_array_dist); 
    ArrayList<Taxi> taxiList = new ArrayList<Taxi>(); 

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

    } 


    setListAdapter(new TaxiAdapter(this, R.layout.taxi_list_item, taxiList)); 

    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: "+taxiNames[selectedPosition]); 
      adb.setPositiveButton("Book", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        Intent intent = new Intent(ListTaxi.this, Booking.class); 
        intent.putExtra("booking", taxiNames[selectedPosition]); 
        intent.putExtra("address", taxiAddresses[selectedPosition]); 
        intent.putExtra("distance", taxiDist[selectedPosition]); 
        startActivity(intent); 
       } 
      }); 
      adb.setNegativeButton("Cancel", null); 
      adb.show(); 
     } 
    }); 



} 

}

essayé toute la nuit et n'ont pas réussi à trouver un moyen d'y arriver avec ma configuration

Toutes les idées ou conseils serait génial!

Merci à tous

Répondre

1

Vous aurez besoin d'ajouter votre propre ListAdapter qui peut personnaliser l'affichage pour chaque ligne de la liste. Consultez cet exemple:

http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/

l'intérieur de votre vue qui définit votre ligne que vous aurez un bouton à la place de l'image. Ensuite, lorsque vous gonflez la vue, ajoutez votre écouteur au bouton de l'adaptateur. C'est vraiment facile.

+0

merci l'homme. mal donner un coup de feu et vous faire savoir comment je reçois – Oli