3

I have an array of apps(PInfo) and I am wondering how do I add that array to a listview?

ArrayList<PInfo> info = appsGetter.listPackages();
    int number = 0;
    PInfo appInArray;
    while(number < info.size()){
        appInArray = info.get(number);
}

This is what I have at the moment, the listPackages() is a method that is getting the names of the apps from the device.

At the moment I am trying to get the information out of the array one by one and add it to the listview like that. Is that how I should do it our should I add the array straight to the listview? And how do you do that?

1

3 Answers 3

2

You can use an ArrayAdapter and initialize it like this:

ArrayAdapter<PInfo> adapter = new ArrayAdapter(context, 
                android.R.layout.simple_list_item_multiple_choice, 
                info);

Then you can you use ListView.setAdapter(adapter).

I'm not sure if this is what you're asking though. So please clarify further if this is not what you're asking

2

Try using an Adapter. For example (using just the String value of an object) you could do the following:

ListView listView = (ListView)findViewById( R.id.myListView );
final ArrayList<String> listItems = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, listItems );
listView.setAdapter( adapter );

Just a quick example, but I hope it gives you a starting place. Just make sure if you add values to your data source later (in this case the ArrayList) to call the adapter's "notifyDataSetChanged()" method so that it can be properly reflected in whatever has been bound to the adapter (in this case the ListView).

0

You need to use an ArrayAdapter. Just search for a ListView and ArrayAdapter sample online. It's quite simple once you see it done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.