Android列表视图仅显示两个中的一个项目

时间:2012-12-13 19:50:56

标签: java android xml

我正在尝试制作一个包含两个项目(两个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>

这是MAIN SCREEN 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;
    }

}

这是Adapter类:

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

代码的结果是一个列表,但只有一个项目(名称),数量没有添加

有人能找到问题吗?

1 个答案:

答案 0 :(得分:2)

您的行布局的第一个TextView填充整个行,将第二个TextView推离屏幕。您需要更改行布局。使用wrap_content

<TextView android:id="@+id/prudctName"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    ... />

或者您可以合并layout_weight

相关问题