Reputation: 41
I just need to have a price next to my products in a listView and they need to be aligned as follows,
This is the code:
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;
}
The String.format
class doesn't seem to be working. Is there a way to do this?
Please Help
Upvotes: 2
Views: 177
Reputation: 1448
Create your custom adapter. In getView you'll be able to customize line you want. You can extend BaseAdapter for it.
Upvotes: 1
Reputation: 2271
you should write a custom listview
adapter to achieve this:
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;
}
}
Add a ListView
to your activity layout:
<ListView
android:id="@+id/ProductListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
Create a XML layout and name it custom_list_row.xml
. This is for ListView
rows.
<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>
And in your activity :
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);
Note: lst
is your product list which contains product name and it's price.
EDIT: Your product class must be look like this
public class ProductClass {
public String product_name;
public float price;
public String getProductName()
{
return product_name;
}
public String getPrice()
{
return String.ValueOf(price);
}
}
Hope this will help.
Upvotes: 0