0
package com.in2em.hotel;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log; 
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("ParserError")

在列表视图中显示两个数组列表的活动

  public class OrderActivity extends ListActivity {
    ArrayList<String> data,rate;
    OrderAdapter orderAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order);
        Bundle extras = getIntent().getExtras(); 
        data = extras.getStringArrayList("list");
        rate = extras.getStringArrayList("rate");
        for(String str : data)  {
            Log.d("data", str);

        }
        for(String str1 : rate) {
            Log.d("data", str1);

        }

数据和速率是两个数组列表

        String[] captionArray = (String[]) data.toArray(
                new String[data.size()]);
        String[] rateArray = (String[]) rate.toArray(
                new String[rate.size()]);
        R.layout.order_list,R.id.itemname, captionArray));

如何在 listview 中设置两个数组列表?我只能设置一个数组列表?

     orderAdapter = new OrderAdapter(
                OrderActivity.this, R.layout.order_list,
                captionArray);

设置列表适配器

        setListAdapter(orderAdapter);

        return ;
    }

自定义列表适配器

         private class OrderAdapter extends BaseAdapter {
        String[] items;

        public OrderAdapter(Context context, int textViewResourceId,
                String[] items) {
            this.items = items;
        } 

返回视图

        public View getView(final int POSITION, View convertView,
                ViewGroup parent) {

            TextView tvNumber,tvName,tvRate;
            EditText etQty,etRemarks;

            View view = convertView;

膨胀

            if (view == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = vi.inflate(R.layout.order_list, null);

            } 
                tvName = (TextView) findViewById(R.id.itemname);
            tvRate = (TextView) findViewById(R.id.itemrate);

            tvName.setText(data.get(POSITION));
            tvRate.setText(rate.get(POSITION));


            return view; 
        }


        public int getCount() {
            // TODO Auto-generated method stub
            return items.length;
        }


        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }


        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    }

}
4

2 回答 2

1

更改您的构造函数以接受两个 ArrayList 并将它们存储在适配器中:

private class OrderAdapter extends BaseAdapter {
    ArrayList<String> data;
    ArrayList<String> rate;

    public OrderAdapter(Context context, ArrayList<String> data, ArrayList<String> rate) {
        this.data = data;
        this.rate = rate;
    } 

    // Use the same getView(), for now

    public int getCount() {
        return data.size(); // rate must be the same size as data!
    }

    public String getItem(int position) {
        return data.get(position);
    }

并像这样使用它:

 orderAdapter = new OrderAdapter(this, data, rate);

你也应该观看这个关于适配器和 ListViews 的Android 讲座以加快速度getView()

于 2012-11-18T08:22:14.060 回答
0
List<String> firstList = new ArrayList<String>();
List<String> secondList = new ArrayList<String>();
List<String> thirdList;
firstList.add("...");
secondList.add("...");

thirdList = new ArrayList<String>(firstList);
thirdList.addAll(secondList);

适用thirdList于您的适配器。

于 2012-11-18T12:34:29.013 回答