How do you add array to listview - android

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?

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

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).

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

Related

Listview adding to the same line

I have an android app that lists connected clients in a listview, but whenever someone connects, it just adds them to the same line, this is the code I use to add connected client. I am new to listview, and not sure how to do this properly, I looked at the android docs but hard to say what needs to be used. If anyone can help me out that would be great.
remoteip += socket.getInetAddress();
ArrayList<String> addclientlist = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainserverActivity.this,
android.R.layout.simple_list_item_1, android.R.id.text1, addclientlist);
addclientlist.add(remoteip);
adapter.notifyDataSetChanged();
listview.setAdapter(adapter)
I think you need to use this constructor
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainserverActivity.this, android.R.layout.simple_list_item_1, addclientlist);
Also no need to set adapter always just set it once and each time you have to add new item to adapter you can use either of these as in java Array objects are pass by reference, and then call notifydatasetchanged
adapter.add(remoteip);
//or addclientlist.add(remoteip);
adapter.notifyDataSetChanged(); // Dont forget this
You are updating the adapter initialization list, which is futile.
Instead - update the actual adapter:
adapter = new ArrayAdapter<String>(MainserverActivity.this,
android.R.layout.simple_list_item_1, android.R.id.text1, addclientlist);
listview.setAdapter(adapter)
...........
adapter.add(remoteip); // <----- instead of addclientlist.add()
adapter.notifyDataSetChanged();

Android null pointer exception on ArrayList for ListView

I have a listview that shows the contents of an arraylist. I'm using a simple adaptor to make this possible like so.
public static ArrayList<String> homeScreenContacts = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.home_screen_contacts_view, NewContact.homeScreenContacts);
The second line is giving me a null pointer exception. I thought about it and I decided it was because the arrayList is empty. So I added the following line between the arraylist declaration and the arrayadaptor declaration...
NewContact.homeScreenContacts.add("A Contact");
This solved the problem and my code worked fine but Now the list view shows "A Contact" and I dont want it to. Is there anyway to get rid of the null pointer exception problem but still have the arraylist empty? Because I want to populate it with user made contacts, not hard-coded, random strings. Thank you.
EDIT: Sorry, The arraylist is located in another class called NewContact, also, I am very beginner Android Programmer I just started.
Simple solution just don't initialize the ListView if there is no element in the ArrayList or the ArrayList is null.
if(NewContact.homeScreenContacts != null && NewContact.homeScreenContacts.size() > 0){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.home_screen_contacts_view, NewContact.homeScreenContacts);
listView.setAdapter(adapter);
}
Also you need to remember that if you you haven't initialize Adapter then dont initialize the ListView and before any operation on list view you should check is it null or not.
As you have said that you want to populate when user add some contact in the application then on add event only you need to populate or update the ListAdapter.
Hope this solution will resolve your problem.
Try this code
public class YourActivity extends Activity
{
private ListView lv;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.your_layout);
lv = (ListView) findViewById(R.id.your_list_view_id);
// Instanciating an array list (you don't need to do this, you already have yours)
ArrayList<String> your_array_list = new ArrayList<String>();
your_array_list.add("foo");
your_array_list.add("bar");
// This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, your_array_list);
lv.setAdapter(arrayAdapter);
}
}
Works fine for me:
if(arrayList.isEmpty())
{
listView.setAdapter(null);
}
else
{
listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(listAdapter);
}

Android ListView: don't show all strings

I show a listview from the following string:
String[] values = new String[] { test1, test2, test3 };
The variables:
private String test1 = "test";
private String test2 = "test";
private String test3 = "test3";
Now I don't want to show the strings that contain "test" in my listview.
Like this:
if (String == "test") {
*don't show in ListView*;}
And I want it to test all Strings at once if they contain "test"
How is it possible?
EDIT: Here the adapter code:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
Yup...
if (values[i].equals("test")) // i being the iterator of your for-loop
OR
if (values[i].contains("test"))
contains is a slow(er) string function though.
You might want to use an ArrayList though... that way you can just add all those objects to the array list, then iterate through it... and remove them as you go...
// Do something to add all items to your array list
...
// Iterate through the list, removing what doesn't need to be there
for (int i = 0; i < arrayList.size(); i++){
if (arrayList.get(i).contains("test"))
arrayList.remove(i);
}
...Then set 'arrayList' (or whatever you've called it) as the string list for your adapter. If you use the same constructor it'll look like this...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, arrayList.toArray());
all the time, if you want compare 2 strings use the equals methode
read this about The equals() method
You should be using a filter on your ListView. Look at these examples:
(Simple iterating) http://androidsearchfilterlistview.blogspot.com/2011/06/android-custom-list-view-filter.html
(getFilter()) Filtering ListView with custom (object) adapter
Like others have posted, you compare String objects using .equals(), but if you are trying to only display certain items in your ListView you should use the getFilter() method like described in the link I posted.
edit: I found you a nice SO example.

Spinner data sorting in Android

I have spinner with array list thats work fine, but i want to sort out the datas from a to z (example: apple,ball,cat,dog...)order. I submit my code below
ArrayList<String> SourceArray = new ArrayList<String>();
Spinner Sourcespinner;// = new Spinner(this);
Sourcespinner = (Spinner)findViewById(R.id.Spinner1);
ArrayAdapter<String> SourceArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,SourceArray);
SourceArrayAdapter.add("Chennai");
SourceArrayAdapter.add("Mumbai");
SourceArrayAdapter.add("Kolkatta");
SourceArrayAdapter.add("Delhi");
Sourcespinner.setAdapter(SourceArrayAdapter);`
I don't know how to do sorting for this
you can use this to sort your data
Collections.sort(SourceArray);
Try to add data to the ArrayList and just use the Collections class to sort for you:
Collections.sort(SourceArray);
If you need to add your own objects they need to implement the Comparable interface and implement the method compareTo(). When changing the ArrayList's data make sure to notify the adapter that new data might have been added by using this code:
SourceArrayAdapter.notifyDataSetChanged();

How to update SimpleAdapter in Android

Is it possible to update a SimpleAdapter? I have a list of data and a footer that says "See Next Results" When that list item is clicked I capture the event and get new data. I then want to replace the data in the ListView with this new data but I can't figure out how to do it. Any Ideas? I don't want to use an ArrayAdapter, cause as far as I can see the items can only hold one string where I need it to hold multiple strings and ints.
Update: According to del116, you can indeed give SimpleAdapter a mutable map and then manually call the adapter's notifyDataSetChanged method when you need the list to update. However, my point below stands about the documentation of SimpleAdapter specifying that it is for static data; using it for mutable data is going counter to its design, so if you use this technique I would be sure to check on whether it continues to work in new Android releases as they emerge.
(Original commentary follows:)
If you look at the SimpleAdapter description it says it is "An easy adapter to map static data to views defined in an XML file." I've added the emphasis -- put simply, SimpleAdapater isn't built for use with data that changes; it handles static data only. If you can't use an ArrayAdapter because your data has more than a single bit of text, then you will either have to build your own custom ListAdapter, or put your data in a DB and use one of the CursorAdapters.
As a last resort, if you don't need much performance, you could update a ListView backed by a SimpleAdapter by building a whole new SimpleAdapter instance any time your data changes and telling the list view to use it via setListAdapter.
ListView lv= (ListView) findViewById(R.id.loglist);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "time", "message" };
int[] to = { R.id.logtime, R.id.logmessage };
adapter = new SimpleAdapter(getApplicationContext(), list,R.layout.log_list_row, from,to);
lv.setAdapter(adapter);
Call this function each time to update the ListView. Keep in Mind that You have to update the list variable with new Data..
Hope this Helps..:)
SimpleAdapter is meant for static data, so your performance may vary. The best solution is probably to switch to a different type of adapter, such as ArrayAdapter, or make a new SimpleAdapter every time you change the dataset.
I was not able to get notifyDataSetChanged() to work on updating my SimpleAdapter, so instead I tried first removing all views that were attached to the parent layout using removeAllViews(), then adding the ListView, and that worked, allowing me to update the UI:
LinearLayout results = (LinearLayout)findViewById(R.id.results);
ListView lv = new ListView(this);
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
SimpleAdapter adapter = new SimpleAdapter( this, list, R.layout.directory_row,
new String[] { "name", "dept" }, new int[] { R.id.name, R.id.dept } );
for (...) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("dept", dept);
list.add(map);
}
lv.setAdapter(adapter);
results.removeAllViews();
results.addView(lv);

Categories

Resources