android,数组适配器不显示行的内容
在我的应用程序中,我有一个字符串数组,我在其中存储字符串。
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 行没有信息:(
In my application i have a String array that i store my strings in it.
private static String[] tokens = new String[1024];
Format of my row which i create it in xml file is:
<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>
In onCreate(), i defined:
lvOutstanding = (ListView) findViewById(R.id.outstanding_list);
myListAdapter = new MyListAdapter();
lvOutstanding.setAdapter(myListAdapter);
and this is my adapter:
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);
}
}
as you saw, in each row i have three textView and i want to show each three items of array in each row. "arrayLoop" is and int variable that saved number of items in the "tokens[]" array.
Now, when i run the application, emulator shows 1024 rows without any data. where is my mistakes?
when i check the logcat (Thanks google for new beautiful design of logCat!), there is no problem and first 12 items in array has parameters and there for (12/3=4), i should see 4 rows with information. However, i have 1024 rows without information :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果tokens tokens数组不是字符串类型,则需要这样写。
因为当您使用此
tvContAmount.setText(tokens[position * count]);
它会认为它是资源ID。它不会得到这个,因此不会显示任何输出。
You need to write as follow if tokens tokens array is not string type.
Because when you use this
tvContAmount.setText(tokens[position * count]);
it will think that it is resource id. It will not get this and hence it won't show any output.
您的 arrayLoop 变量必须为 0 或更小。我认为你的问题在于你将 arrayLoop 放在你打算放置 count 的 if 语句中。但我看不到你在哪里定义了 arrayLoop 或你分配给它的内容。
Your arrayLoop variable must be 0 or less. I think that your problem is where you've put arrayLoop in the if statement you meant to put count. But I can't see where you've defined arrayLoop or what you've assigned to it.