Android的 - 添加子项到ListView [英] Android - Adding Subitem to a listview

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

问题描述

我现在有一个列表视图,其中包含了几个字符串。这些都是从字符串数组中的strings.xml名为

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>

我试图做的是创建子项为这些,这样我可以证明,如地址和联系方式等。我做了一个customlistview而回,但不能工作如何,我可以使用的strings.xml文件做场?是否有任何特定的标签我需要使用,使他们显示在列表视图?

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?

任何帮助将是很大的AP preciated。

Any help would be greatly appreciated.

主要活动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(); 
         }
     });

感谢

推荐答案

编辑:好的,只是踢,我扔了一起。它编译并运行正常,看看你是否能适应它的特殊需要:

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.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_的 _ __ _ __

_____END EDIT_______

您很可能会更好使用数据库来实现这样的事情,以保持捆绑在一起的记录。如果您使用数组上正在设置,有一件事你可以做的是让你需要的每一项单独的阵列(如taxi_array,taxi_address_array,taxi_phone_array),然后让出租车对象,在code:

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

(这是未编译code,一些调整可能需要)但随后你就会有出租车项目的列表,包含所有从不同阵列中的提供的信息编制。数据库仍然是一个更好的选择(甚至是CSV文件中的数据,在你的资产)。

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

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

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