将子项目添加到 Android 中的列表视图

发布于 2024-10-09 20:14:50 字数 2226 浏览 3 评论 0原文

我目前有一个包含几个字符串的列表视图。这些是从 strings.xml 中的字符串数组调用的。

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

</string-array>

我想做的是为这些创建子项,以便我可以显示地址和联系方式等字段。我不久前做了一个自定义列表视图,但不知道如何才能使用 strings.xml 文件执行此操作吗?我需要使用任何特定标签才能将它们显示在列表视图中吗?

主要活动代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乞讨 2024-10-16 20:14:50

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

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_______

你可能会最好使用数据库来做这样的事情,将记录捆绑在一起。如果您决定使用数组,您可以做的一件事是为您需要的每个项目创建一个单独的数组(例如,taxi_array、taxi_address_array、taxi_phone_array),然后在代码中创建一个 Taxi 对象:(

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

这是未编译的代码,可能需要进行一些调整需要)但是您将拥有一个出租车项目列表,其中包含来自不同数组的所有已编译信息。数据库仍然是一个更好的选择(甚至是资产中包含数据的 CSV 文件)。

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).

夜清冷一曲。 2024-10-16 20:14:50

我有同样的问题,我这样解决自己:
您可以简单地添加像这样的代码的子项目,并且不需要那么多编码!

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文