Android- 向 ListView 添加子行!

标签 android listview rows

我一直在尝试向 ListView 添加子行。我有一个出租车应用程序,目前显示出租车公司名称列表,我希望能够添加一些显示地址、邮政编码等的子行。

我已经尝试过一些,但都没有成功。我从 strings.xml 文件中的数组调用我的字符串。我现在的代码是:

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

如果有人能帮我解决这个问题,我将不胜感激。

我最初问的问题在这里:Android - Adding Subitem to a listview

谢谢

最佳答案

哎呀,刚注意到这个。我会将编辑后的答案粘贴到此处:

好吧,只是为了好玩,我把它放在一起。它可以正确编译和运行,看看您是否可以根据您的特定需求调整它:

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

关于Android- 向 ListView 添加子行!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4589518/

相关文章:

android - Sherlock 操作栏缺少样式

wpf - WPF从一个ListView中选择项目,并在另一个ListView中显示关系数据

mysql - 重新排序 mysql 表行以提高速度

android - 通过参数将 Spannable 应用于 string.xml 中的字符串

android - Activity 的第一个实例永远不会被垃圾收集?

listview - Xamarin.Forms 多列表 GUI

java - 安卓-ListView : Checkboxes not staying checked

mysql - 计算表中的行数

objective-c - indexPath.row 返回什么?

android - 为什么 android 没有将 String 值放入 JSONObject 的函数?