0

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?

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

0

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

adapter.notifyDataSetChanged() 

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.