I’am trying to make a ListView that contains two items (two textView’s)
Here is my code:
Header 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>
Hers is the row 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>
Here is the 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>
Here is the class that represents the object containing the two filed:
public class ShopingListItem
{
public String productName;
public String procuctAmount;
public ShopingListItem(String _productName,String _productAmount)
{
this.procuctAmount=_productAmount;
this.productName=_productName;
}
}
Here is the Adapter class:
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;
}
}
Here is the main activity:
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;
}
}
The result of the code is a list but with only one item (the name) the amount isnt add’d
Can anyone find the problem?
Your row layout’s first TextView fills the entire row, pushing the second TextView off of the screen. You need to change your row layout. Either use
wrap_content:Or you can incorporate
layout_weight.