Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding dynamic String arrays to a dynamic listview in android

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.

like image 410
Dinesh Avatar asked Jul 26 '26 02:07

Dinesh


1 Answers

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

like image 95
Matt Wolfe Avatar answered Jul 28 '26 16:07

Matt Wolfe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!