如何在listView

时间:2016-07-05 03:36:04

标签: java android listview

我只需要在listView中为我的产品定价,它们需要按如下方式对齐,

  • 左侧的产品名称,以及;
  • 右边的价格

这是代码:

public ListView lstMenuItems;
public String[] menuItems = {"Fillet On The Bone", "Rump", "Sirloin", "French Cut Pork Cutlets", "Spatchcock Chicken", "Sticky Smoked Paprika Chicken Wings", "Craft Beer Battered Fish", "Butter Chicken", "Pomegranate & Chilli Marinated Lamb Skewer", "Pork Belly Skewer", "Harissa Basted Chicken Skewer"};
public double[] itemPrice = {135, 99, 99, 99, 89, 79, 60, 75, 99, 95, 69};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
    TitleBar(waiterName);
    GetListViewValue();
    menuItems = AddPad(menuItems, itemPrice);
    MenuList();

}

    public void MenuList() {

    lstMenuItems = (ListView)findViewById(R.id.lvProducts);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.category_list, menuItems);
    lstMenuItems.setAdapter(adapter);

}


public String[] AddPad(String[] menu, double[] price) {
    NumberFormat format = NumberFormat.getCurrencyInstance(Locale.getDefault());
    for (int i = 0; i < menu.length; i++) {
        menu[i] = String.format("%-60s %s",menu[i], format.format(price[i]));
    }
    return menu;
}

String.format课程似乎无法正常工作。有没有办法做到这一点?

请帮助

2 个答案:

答案 0 :(得分:1)

创建自定义适配器。在getView中,您可以自定义所需的行。您可以为它扩展BaseAdapter。

答案 1 :(得分:0)

您应该编写一个自定义listview适配器来实现此目的:

public class myCustomAdapter extends ArrayAdapter<ProductClass> {

    public myCustomAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public myCustomAdapter(Context context, int resource, List<ProductClass> pc) {
        super(context, resource, pc);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;

        if (view == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            view = vi.inflate(R.layout.custom_list_row, null);
        }

        ProductClass p = getItem(position);

        if (p != null) {
            TextView product_name = (TextView) v.findViewById(R.id.product_name);
            TextView product_price = (TextView) v.findViewById(R.id.product_price);
            product_name.setText(p.getProductName());
            product_price.setText(p.getCategory().getPrice());

        }
        return view;
    }

}

在您的活动布局中添加ListView

<ListView
    android:id="@+id/ProductListView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
</ListView>

创建XML布局并将其命名为custom_list_row.xml。这适用于ListView行。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

  <TextView
              android:id="@+id/product_name"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:gravity="right"/>

 <TextView
              android:id="@+id/product_price"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:gravity="left"/>

</LinearLayout>

在你的活动中:

ListView ProductListView = (ListView) findViewById(R.id.ProductListView);
List<ProductClass> lst = myProductList();
myCustomAdapter productAdapter = new myCustomAdapter(this, R.layout.custom_list_row, lst);
ProductListView.setAdapter(productAdapter);

注意:lst是您的产品列表,其中包含产品名称及其价格。

编辑:您的产品类必须如下所示

public class ProductClass {
        public String product_name;
        public float price;

        public String getProductName()
        {
            return product_name;
        }
        public String getPrice()
        {
            return String.ValueOf(price);
        }
}

希望这会有所帮助。