Nick Robertson Nick Robertson - 5 months ago 34
Android Question

Change Height of a Listview dynamicallyAndroid

The format of my XML file is as following:

LinearLayout

ScrollView

RelativeLayout

LinearLayout

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_margin="5dip"
android:layout_weight="30" >
</ListView>

LinearLayout


Here is my BaseAdapter class

public class APIQuickCheckoutProductProvider extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;

public APIQuickCheckoutProductProvider(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
return data.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.product_item, null);

TextView brand = (TextView) vi.findViewById(R.id.brands);
TextView quantity_name = (TextView) vi.findViewById(R.id.quantity_name);

TextView price = (TextView) vi.findViewById(R.id.price);

HashMap<String, String> product = new HashMap<String, String>();
product = data.get(position);

// Setting all values in listview
brand.setText(product.get(APIQuickCheckout.KEY_BRAND));
quantity_name.setText(product.get(APIQuickCheckout.KEY_QUANTITY_NAME));
price.setText(product.get(APIQuickCheckout.KEY_PRICE));

return vi;
}
}


Here is the main class where I set the adapter:

adapter = new APIQuickCheckoutProductProvider(APIQuickCheckout.this, productList);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();


And finally here is the each individual row which i set to the listview

<?xml version="1.0" encoding="utf-8"?>




<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dip"
android:layout_weight="7"
android:weightSum="6" >

<TextView
android:id="@+id/brands"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="3" />

<TextView
android:id="@+id/quantity_name"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="3" />
</LinearLayout>

<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dip"
android:layout_weight="2"
android:weightSum="6" >

<TextView
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="3" />

<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="3" />
</LinearLayout>

</LinearLayout>


However, when I am trying the above mentioned code it displays the first product, and I can see only the top of the second product.And I can see nothing from then and on. So, how can I change the height of the
ListView
dynamically when I add a new product? In order to display all of them.

Answer

If you want to change the height of list view dynamically, you could use,

list.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));