android,阵列适配器使用中的不良后果
我有一个字符串数组,我想在列表中显示它的项。 我有一个row.xml,其中包含一个表,每行有三个TextView。 我想要的是,在每行中显示每三个数组项 示例:android,阵列适配器使用中的不良后果,android,android-arrayadapter,listadapter,Android,Android Arrayadapter,Listadapter,我有一个字符串数组,我想在列表中显示它的项。 我有一个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"?> <TableLay
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>
这是我的适配器:
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);
}
}
类MyListAdapter扩展了ArrayAdapter{
MyListAdapter(){
super(msuntible.this、R.layout.list_untible、tokens);
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
//字符串str=llData.get(位置);
//字符串[]标记=str.split(“,”);
if(行==null){
LayoutInflater充气机=getLayoutInflater();
row=充气机。充气(R.layout.list\u未完成,父项,false);
}
TextView tvContDate=(TextView)row.findViewById(R.id.untible_contractdate);
TextView tvContNo=(TextView)row.findViewById(R.id.untible_contractno);
TextView tvContAmount=(TextView)row.findViewById(R.id.Understanding_contractamount);
tvContDate.setText(令牌[0]);
tvContNo.setText(令牌[1]);
tvContAmount.setText(令牌[2]);
返回(行);
}
}
我的第一个问题是,我没有看到我想要的结果。第二个是,我在类的顶部(private static string[]tokens={“”,“};
)将“tokens”定义为字符串数组,这样,当我运行应用程序时,它会显示三行相同的结果。
但如果我更改定义,例如私有静态字符串[]标记代码>程序崩溃。
我头痛:(如果可能的话,告诉我哪里是我的错
感谢您使用以下功能:
static String[] tokens;
而不是:
static String[] tokens = { "", "", "" };
令牌未初始化,这意味着令牌[n]为空,导致崩溃。
在您的注释中,使用标记[position*count+1]将很快超出数组的界限-如果您使用标记={“”,“”},如上所述,数组中只有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);
...
}
公共类合同{
字符串日期;
串号;
字符串数量;
合同(字符串d、字符串n、字符串a){
日期=d;
no=n;
金额=a;
}
}
合同[]合同={新合同(“1”、“2”、“3”)、新合同(“4”、“5”、“6”)};
类MyListAdapter扩展了ArrayAdapter{
MyListAdapter(){
super(msuntible.this、R.layout.list\u untible、合同);
}
}
...
公共视图getView(int位置、视图转换视图、视图组父视图){
...
合同c=getItem(职位);
tvContDate.setText(c.date);
tvContNo.setText(c.no);
tvContAmount.setText(c.amount);
...
}
对于第一种解决方案,我认为您应该尝试类似的方法,if(position=0){count=0}tvContDate.setText(count);tvContNo.setText(count+1);tvContAmount.setText(count+2);count+=3;并将count声明为int count=0;作为类(您的活动)数据成员。如果我错了,请告诉我。谢谢:-)谢谢。那是个狡猾的主意。我通过应用程序进行了测试。我编写了代码:if(position==0)count=0;tvContDate.setText(令牌[位置*计数]);tvContNo.setText(令牌[位置*计数+1]);tvContAmount.setText(令牌[位置*计数+2]);计数=3;我认为我们不能在每次迭代后加上3个计数,但如果我们给它指定3,就可以了。那坠机报告是什么?表示什么异常?java,lang.arrayindexoutofboundexception并指向此行:tvContDate.setText(标记[position*count]);
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);
...
}