Reputation: 181
I have string array which is fetched from an external xml response. This string array can be presented in a listview. I want to append a string array with new xml data and add it to the listview everytime the user reaches the end element of string array in the listview. How can I achieve this? I've researched a lot on this topic.I could hardly find any examples related to this context. Any help will be much appreciated. Thank you.
ArrayList<String> get_msgList =MsgHandler.getMsgsList();
String[] msgList=new String[get_msgList.size()];
msgList=get_msgList.toArray(msgList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, get_msgList );
myListView.setAdapter(adapter);
This is how I get my string array and add it to the listview.
Upvotes: 3
Views: 7082
Reputation: 9284
-- Update, after looking more into documentation I think you still could use ArrayAdapter --
So if you look at the documentation for listview you see
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="countries_array">
<item>Bahrain</item>
<item>Bangladesh</item>
<item>Barbados</item>
<item>Belarus</item>
<item>Belgium</item>
<item>Belize</item>
<item>Benin</item>
</string-array>
</resources>
and to load those you use:
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries));
The problem here is you have to deal with figuring out when they get to the end of the list and how to load more data without blocking the UI.
Thus, to save yourself a lot of headache, I recommend using Commonsware Endless Adapter Do so like this:
import android.app.ListActivity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import com.commonsware.cwac.endless.EndlessAdapter;
import java.util.ArrayList;
public class YourListActivity extends ListActivity {
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
String[] countries = getResources().getStringArray(R.array.countries_array);
setListAdapter(new YourAdapter(countries));
}
class YourAdapter extends EndlessAdapter {
public YourAdapter(String[] list) {
super(new ArrayAdapter<String>(YourAdapter.this,
R.layout.row,
android.R.id.text1,
list));
}
@Override
protected View getPendingView(ViewGroup parent) {
View row=getLayoutInflater().inflate(R.layout.row, null);
return row.findViewById(android.R.id.text1);
}
@Override
protected boolean cacheInBackground() {
SystemClock.sleep(10000); // pretend to do work
//put logic here to fetch the data
//this happens in the background so you can't do anything with the UI
//return whether or not you have more data, true if you do false otherwise.
boolean haveMoreData = true;//this up to you to figure out
return haveMoreData;
}
@Override
protected void appendCachedData() {
//here is where you do the work to actually add the data obtained in cacheInBackground to the ArrayAdapter
ArrayAdapter<String> a=(ArrayAdapter<String>)getWrappedAdapter();
for (int i=0;i<25;i++) {
a.add(a.getCount());
}
}
}
}
}
here is another question that looks similar to your own on here: Android Endless List
-- Update: How to add multiple string arrays to an adapter -- There are multiple ways to do this, the most simple way would probably be to use the adapters add method..
Look at this: http://developer.android.com/reference/android/widget/ArrayAdapter.html and see the method add. Currently you have a variable named adapter:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, get_msgList);
Whenever you want to add more data to that adapter just call:
adapter.add("Some new data");
So if you had another String[] array of data and you wanted to add all of it to the current adapter, just use:
String[] someNewData = getStringArray();//no idea how you are getting the array, this is just an example
for (String s : someNewData) {
adapter.add(s);
}
After you've added the new data you may need to call adapter.notifyDataSetChanged but I'm not sure (it may happen for you).. I would try it first without it and see if everything works as expected and if not add it.
Now one thing you will probably want to do is either make the adapter variable an instance variable of your class so you can use it anywhere in the class.
Upvotes: 2
Reputation: 553
I wondering how much data this listview is going to show. From the info you've given sounds like you need to implement an onScrollListener. It will allow you to load more data when you get to the bottom of the list (Like the facebook app). Like the response from Matt about not messing with your ui loading you will need to setup a thread to load the data outside your ui thread.
When you say load from an external source. Is these via the internet? If so the above info should point you in the right direction. If by external you mean in another class you're probably better off loading them all together or storing them in a database and running a cursor to get what you need from it.
Upvotes: 0