android,数组适配器不显示行的内容

时间:2011-10-20 02:21:40

标签: android android-arrayadapter listadapter

在我的应用程序中,我有一个String数组,我将我的字符串存储在其中。

private static String[] tokens = new String[1024];

我在xml文件中创建的行的格式为:

<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()中,我定义了:

lvOutstanding = (ListView) findViewById(R.id.outstanding_list);
        myListAdapter = new MyListAdapter();
        lvOutstanding.setAdapter(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;

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

            if(position == 0)
                count = 0;
            else
                count = 3;

            if(arrayLoop>0){
                tvContDate.setText(tokens[position * count]);
                Log.i("Count:", tokens[position * count]);

                tvContNo.setText(tokens[position * count + 1]);
                Log.i("Count:", tokens[position * count +1]);

                tvContAmount.setText(tokens[position * count + 2]);
                Log.i("Count:", tokens[position * count+2]);

                arrayLoop -= 3;
                Log.i("Count:", String.valueOf(arrayLoop));
            }

            return(row);
        }
    }

如你所见,在每行中我有三个textView,我想在每一行中显示每三个数组项。 “arrayLoop”是和int变量,它保存了“tokens []”数组中的项目数。

现在,当我运行应用程序时,模拟器显示1024行而没有任何数据。我的错在哪里? 当我检查logcat(感谢google为logCat的新设计!),没有问题,数组中的前12个项目有参数,并且(12/3 = 4),我应该看到4行信息。但是,我有1024行没有信息:(

enter image description here

2 个答案:

答案 0 :(得分:1)

如果令牌令牌数组不是字符串类型,则需要按如下方式编写。

    if(arrayLoop>0){

        tvContDate.setText(String.valueOf(tokens[position * count]));
        Log.i("Count:", tokens[position * count]);

        tvContNo.setText(String.valueOf(tokens[position * count]));
        Log.i("Count:", tokens[position * count +1]);

        tvContAmount.setText(String.valueOf(tokens[position * count]));
        Log.i("Count:", tokens[position * count+2]);

        arrayLoop -= 3;
        Log.i("Count:", String.valueOf(arrayLoop));
    }

因为当您使用此tvContAmount.setText(tokens[position * count]);时 它会认为它是资源ID。它不会得到这个,因此它不会显示任何输出。

答案 1 :(得分:0)

您的arrayLoop变量必须为0或更小。我认为你的问题在于你将arrayLoop放在你想要计数的if语句中。但是我无法看到你在哪里定义了arrayLoop或你分配给它的内容。