Viney Dhiman
Viney Dhiman

Reputation: 305

how to display multiple data using ListView in android

On clicking Button ADD ANOTHER as in Screenshot It will add another medication after one another. So problem is that, I am unable to display the multiple data .

How can i show it using list view any help please.

Thank you in advance

enter image description here

Upvotes: 1

Views: 597

Answers (1)

karanatwal.github.io
karanatwal.github.io

Reputation: 3673

Simply ,Add data in your ArrayList which you are passing in adapter constructor. After then call adapter.notifyDataSetChanged() .If you face problem with listview height ,then set it dynamically on button click -

public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
    return;

int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
    view = listAdapter.getView(i, view, listView);
    if (i == 0)
        view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT));

    view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
    totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}

Upvotes: 1

Related Questions