Reputation: 63
I am using standard ListView and Adapter as ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, itemList).
The list structure as follows: LOOOOOOOOOOOONG ITEM NAME 1
SHORT ITEM NAME 2
I need to add quantity to the text1 line. I can concatenate it with ITEM NAME,so it looks like
LOOOOOOOOOOOONG ITEM NAME1 845
SHORT ITEM NAME2 132
How to push quantity to the right, so all of them will be aligned and right justified? Can someone help?
Upvotes: 0
Views: 220
Reputation: 13019
You have to use a custom Adapter that inflate a custom composite View for each cell.
In a new CustomAdapter.java (Item is your data POJO: see below)
public class CustomAdapter extends BaseAdapter {
private final Context mContext;
private final List<Item> mItemList;
public CustomAdapter(Context context, List<Item> itemList) {
mContext = context;
mItemList = itemList;
}
@Override
public int getCount() {
return mItemList.size();
}
@Override
public Item getItem(int position) {
return mItemList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
CustomViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.custom_cell, parent, false);
CustomViewHolder holder = new CustomViewHolder();
holder.itemNameTextView = (TextView) convertView.findViewById(R.id.textview_item_name);
holder.itemQuantityTextView = (TextView) convertView.findViewById(R.id.textview_item_quantity);
convertView.setTag(holder);
} else {
holder = (CustomViewHolder) convertView.getTag();
}
Item item = getItem(position);
holder.itemNameTextView.setText(item.getName());
holder.itemQuantityTextView.setText(item.getQuantity());
return convertView;
}
private static class CustomViewHolder {
public TextView itemNameTextView;
public TextView itemQuantityTextView;
}
}
with custom_cell.xml being :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall">
<TextView
android:id="@+id/textview_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/textview_item_quantity"
android:singleLine="true"/>
<TextView
android:id="@+id/textview_item_quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
This is the Item POJO :
public class Item {
private final String mName;
private final String mQuantity;
public Item(String itemName, String itemQuantity) {
mName = itemName;
mQuantity = itemQuantity;
}
public String getName() {
return mName;
}
public String getQuantity() {
return mQuantity;
}
}
Use it with your ListView by doing :
listView.setAdapater(new CustomAdapter(this, itemList));
Upvotes: 1
Reputation: 1886
In this line:
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, itemList)
Create a new custom xml file in res/layout [say called your_layout_name.xml
] and replace the android.R.layout.simple_list_item_1
in the code above with "R.layout.your_layout_name". In that file, put in the following code:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical|right"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
Here is the original android.R.layout.simple_list_item_1
layout which, in that ArrayAdapter code, defines the layout of the row:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
All that needed to be changed was the "gravity" parameter. That defines the alignment of the text, and all you needed to add was "right" to it.
Note: The gravity parameter normally aligns all of its contents in any View. Say, for a LinearLayout, this would try to align all of its child elements. For a TextView, it aligns its text.
Upvotes: 0