2

I want to create a scrollable list of progress bars. The number of progress bars and the content of each progress bar is decided dynamic and decided at run time. Is it possible to put create and put progress bars in a ListView? I couldn't find any relevant resources on the internet and hence wanted to confirm if this is even possible in the current android framework.

TheBlueNotebook
  • 1,204
  • 3
  • 15
  • 35

2 Answers2

2

Yes it is possible, you have to create a custom listView and can add items dynamically whenever you want to add

create a layout that will contain your listView

<ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

create an item layout which will represent your single item of the listView and create a listViewAdapter that will assign your values each of your item of the list

public class ListAdapter extends ArrayAdapter{

private final Context context;
private int layoutResourceID;
private ArrayList<YourDataModel> objects;


public ListAdapter(Context context, int layoutResourceID, ArrayList<YourDataModel> objects) {
    super(context, layoutResourceID, objects);

    this.context = context;
    this.layoutResourceID = layoutResourceID;
    this.objects = objects;
}

@Override
public View getView(int position, final View convertView, ViewGroup parent) {

    View row = convertView;

    final ListHolder listHolder;
    if(row == null) {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceID, parent, false);

        itemProgressBar = (ProgressBar) row.findViewById(R.id.item_progressbar);

        row.setTag(listHolder);

    } else {
        listHolder = (ListHolder) row.getTag();
    }


    final DataCategory list = objects.get(position);    // get your data object

    // assign values to your items of listView



    return row;
}

class ListHolder {
    ProgressBar itemProgressBar;
}

}

in the above code YourDataModel your java class containing your data and last in your main set all the things to make it work

ListAdapterHome listAdapter = new ListAdapter(YourActivity.this,
        R.layout.item, ArrayListOfYourDataModel);   // pass the arrayList of your dataModel there

ListView listView = (ListView) view.findViewById(R.id.listView);
listView.setAdapter(listAdapterHome);
Zubair Akber
  • 2,760
  • 13
  • 30
1

It is possible. You would have to write your own ListAdapter (i would recommend overwriting SimpleArrayAdapter, but it depends on your Data) and overwrite the getView Method to generate Views that contain a ProgressBar.

Check this question: Custom Adapter for List View

FYI: The ListAdapter is the class that generates the Views of the ListView and assigns their values. Usually if you generate a SimpleArrayAdapter you pass the Layout you want it to create. That works if you only want to set some text in the layout. However to work with a View as specific as a ProgressBar you would have to create your own ListAdapter to handle that specific view.

Community
  • 1
  • 1
findusl
  • 2,454
  • 8
  • 32
  • 51