【问题标题】:android, bad resaults in use of array adapterandroid,使用arrayadapter的结果不好
【发布时间】:2011-10-19 06:57:24
【问题描述】:

我有一个字符串数组,我想在列表中显示它的项目。 我有一个包含一个表的 row.xml,在每一行中我有三个 TextView。 我想要的是,在每一行中显示每三个数组项。

示例:String[] str = {"1", "2", "3", "4", "5", "6"}; 每一行应该是这样的:

str[1]----str[2]----str[3]
str[4]----str[5]----str[6]

我的xml代码是:

<?xml version="1.0" encoding="utf-8"?>

<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="*"  
android:stretchColumns="*" >
    <TableRow 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:layout_marginTop="10dip"
    android:layout_marginBottom="10dip" >
        <TextView
            android:id="@+id/outstanding_contractdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:textSize="15sp" />
        <TextView
            android:id="@+id/outstanding_contractno"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:textSize="15sp" />
        <TextView
            android:id="@+id/outstanding_contractamount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:textColor="#ffffff"
            android:textSize="15sp" />
    </TableRow>

</TableLayout>

在 OnCreate() 中我放了(我有一些其他视图和一个 listView):

lvOutstanding = (ListView) findViewById(R.id.outstanding_list);
lvOutstanding.setAdapter(new MyListAdapter());

这是我的适配器:

class MyListAdapter extends ArrayAdapter<String>{
        MyListAdapter(){
            super(MSOutStanding.this, R.layout.list_outstanding, tokens);
        }

        public View getView(int position, View convertView, ViewGroup parent){
            View row = convertView;

            //String str = llData.get(position);
            //String[] tokens = str.split(",");

            if(row == null){
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.list_outstanding, parent, false);
            }

            TextView tvContDate = (TextView) row.findViewById(R.id.outstanding_contractdate);
            TextView tvContNo = (TextView) row.findViewById(R.id.outstanding_contractno);   
            TextView tvContAmount = (TextView) row.findViewById(R.id.outstanding_contractamount);

            tvContDate.setText(tokens[0]);
            tvContNo.setText(tokens[1]);
            tvContAmount.setText(tokens[2]);

            return(row);
        }
    }

我的第一个问题是,我没有看到我想要的结果。第二个是,我以这种方式在我的班级(private static String[] tokens = {"", "", ""};)的顶部将“令牌”定义为字符串数组,当我运行应用程序时,它向我显示了具有相同结果的三行。 但是如果我更改定义,例如Private static String[] tokens;,程序就会崩溃。 我头疼:(如果可能的话,告诉我我的错在哪里?

谢谢

【问题讨论】:

  • 对于第一个解决方案,我认为您应该尝试类似 if(position=0){count=0} tvContDate.setText(count); tvContNo.setText(count+1); tvContAmount.setText(count+2);计数+=3;并将计数声明为 int count=0;作为类(您的活动)数据成员。如果我错了,请告诉我。谢谢。:-)
  • 谢谢。这是一个棘手的想法。我测试的应用程序崩溃了。我写了代码: if(position == 0) count = 0; tvContDate.setText(tokens[position * count]); tvContNo.setText(tokens[position * count + 1]); tvContAmount.setText(tokens[position * count + 2]);计数 = 3;
  • 我认为我们不能在每次迭代后添加 3 个计数,但如果我们为其分配 3 个,就可以了。再次感谢
  • 什么是崩溃报告?意味着什么异常?
  • java,lang.arrayindexoutofboundexception 并指向这一行:tvContDate.setText(tokens[position * count]);

标签: android android-arrayadapter listadapter


【解决方案1】:

使用时:

static String[] tokens;

代替:

static String[] tokens = { "", "", "" };

tokens 未初始化,这意味着 tokens[n] 为空,导致崩溃。 在您的 cmets 中,执行 tokens[position * count + 1] 将很快超出数组的范围 - 如果您如上所述使用 tokens = { "", "", "" },则数组内只有 3 个元素.

当您使用此行将令牌传递给超级构造函数时:

super(MSOutStanding.this, R.layout.list_outstanding, tokens);

您是说列表中应该有 3 行,因为数组中有 3 个元素。对于第 3 项,position = 3 * count = 3 == boom 进入数组。

一种解决方法: 将对象数组(或 ArrayList)传递给超级构造函数:

public class Contract {
        String date;
        String no;
        String amount;

        Contract(String d, String n, String a) {
            date = d;
            no = n;
            amount = a;
        }    
    }

Contract[] contracts = { new Contract("1", "2", "3"), new Contract("4", "5", "6") };
    class MyListAdapter extends ArrayAdapter<Contract>{
         MyListAdapter(){
                super(MSOutStanding.this, R.layout.list_outstanding, contracts);
            }
    }
    ...

    public View getView(int position, View convertView, ViewGroup parent){
        ...
        Contract c = getItem(position);
        tvContDate.setText(c.date);
        tvContNo.setText(c.no);
        tvContAmount.setText(c.amount);
...
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多