【发布时间】: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