Dawood Abbasi
Dawood Abbasi

Reputation: 11

Fill android listview with a new string array

I just fill ListView with a string array and then in onClickListener() of a button, I want to refill that list view with new String Array.

How can I do this?

Upvotes: 0

Views: 212

Answers (4)

Ngathet
Ngathet

Reputation: 96

You should use ListActivity instead of ListView. See Example

//List Activity Class

  public class YourClass extends ListActivity 
    {
    public void onCreate(Bundle savedInstanceState)

    {

      super.onCreate(saveInstanceState);

       setContentView(R.layout.alertresult);

       showInList();


    }

    public void showInList()
    {

     ArrayAdapter adapter=new yourAdapter();

     setListAdapter(adapter);
    }


    }

//Sample XML Layout for alertresult

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    android:orientation="vertical" >


    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:fastScrollEnabled="true" >
    </ListView>

</LinearLayout>

You just need to change ListView id to @android:id/list and setListAdapter

Upvotes: 2

G_S
G_S

Reputation: 7110

delete the data from adapter using adapter.clear() and fill the new data to the adapter and call

adapter.notifyDataSetChanged() 

Upvotes: 0

jeet
jeet

Reputation: 29199

I assume you are using ListView with ArrayAdapter, and ArrayAdapter constructor needs an array, or an arraylist as parameter:

public void ArrayAdapter(Context context, int resId, Object[] array);

Create ArrayAdapter with this constructor, and when you want to change the data, just change value of reference array, and invoke notifyDatasetChanged() method.

Upvotes: 0

sampathpremarathna
sampathpremarathna

Reputation: 4064

you can set new list to list view(myListView) and call myListView.invalidateViews();

Upvotes: 0

Related Questions