我正在尝试制作一个包含两个项目(两个 textView)的 ListView
这是我的代码:
标头 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/txtHeader"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#FFFFFF"
android:padding="10dp"
android:text="Shoping List"
android:background="#336699" />
</LinearLayout>
她的行是 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<TextView android:id="@+id/prudctName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textStyle="bold"
android:textSize="22sp"
android:textColor="#000000"
/>
<TextView android:id="@+id/productAmount"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textStyle="bold"
android:textSize="22sp"
android:textColor="#000000"
/>
</LinearLayout>
这是主屏幕 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<ListView
android:id="@+id/shoping_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
这是表示包含两个字段的对象的类:
public class ShopingListItem
{
public String productName;
public String procuctAmount;
public ShopingListItem(String _productName,String _productAmount)
{
this.procuctAmount=_productAmount;
this.productName=_productName;
}
}
这是适配器类:
public class ShopingListItemAdapter extends ArrayAdapter<ShopingListItem>
{
public Context context;
public int layoutResourceId;
public ShopingListItem[] items;
public ShopingListItemAdapter(Context context,int layoutResourceId, ShopingListItem[] objects)
{
super(context, layoutResourceId, objects);
this.context=context;
this.layoutResourceId=layoutResourceId;
this.items=objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ShopingListItemHolder holder = null;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ShopingListItemHolder();
holder.productName=(TextView)row.findViewById(R.id.prudctName);
holder.productAmount=(TextView)row.findViewById(R.id.productAmount);
row.setTag(holder);
}
else
{
holder = (ShopingListItemHolder)row.getTag();
}
ShopingListItem item = items[position];
String amount = item.procuctAmount;
String name = item.productName;
TextView v = holder.productAmount;
TextView vv = holder.productName;
holder.productAmount.setText(amount);
holder.productName.setText(name);
return row;
}
static class ShopingListItemHolder
{
TextView productName;
TextView productAmount;
}
}
以下是主要活动:
public class ShopingListActivity extends Activity
{
private ListView listView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shoping_list_screen);
//fill list with temp data
ShopingListItem[] items = fillData();
ShopingListItemAdapter adapter = new ShopingListItemAdapter(this, R.layout.listview_item_row, items);
listView1 = (ListView)findViewById(R.id.shoping_list);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
private ShopingListItem[] fillData()
{
ShopingListItem[] items = new ShopingListItem[]
{
new ShopingListItem("Bamba", "2"),
new ShopingListItem("Bisli", "12"),
new ShopingListItem("Shoko", "3"),
new ShopingListItem("Yello Cheese", "2"),
new ShopingListItem("Marak", "7"),
new ShopingListItem("Cola", "2"),
new ShopingListItem("Orez", "3"),
new ShopingListItem("Kaki", "9"),
new ShopingListItem("Battery", "1"),
new ShopingListItem("Bla", "1"),
new ShopingListItem("Bla bla", "100"),
new ShopingListItem("bbb", "200"),
new ShopingListItem("Red", "2"),
};
return items;
}
}
代码的结果是一个列表,但只有一个项目(名称),金额未添加
任何人都可以找到问题吗?