Android-添加子行到ListView! [英] Android- Adding subrows to a ListView!

查看:153
本文介绍了Android-添加子行到ListView!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直都在尝试添加子行到ListView。我有一个出租车应用程序,它在present显示出租车公司名称的列表,我希望能够加入其中显示的地址,邮寄code等。

ive been attempting to add subrows to a listview. I have a taxi app which at present shows a list of taxi company names, i want to be able to add some sub rows in which show address, postcode etc.

我已经在这了一些尝试,但都没有成功。我从strings.xml档案数组叫我的琴弦。此刻我的code是:

I have had some attempts at this but none have been successful. I am calling my strings from an array in the strings.xml file. My code at the moment is :

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

如果有人能帮助我克服这个问题,那将是非常美联社preciated。

If anyone can help me get round this problem it would be very much appreciated.

原来的问题,我问是在这里: Android的 - 添加子项到ListView

The original question i asked is here: Android - Adding Subitem to a listview

感谢

推荐答案

哎呦,才注意到这一点。我将我的编辑答案贴到这里:

Whoops, just noticed this one. I'll paste my edited answer into here:

好吧,只是踢,我扔起来这个。它编译并运行正常,看看你是否能适应它为您的特定需求:

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

布局/ 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>

布局/ 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));      
    }
}

这篇关于Android-添加子行到ListView!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆