0

我只是填充ListView了一个字符串数组,然后在onClickListener()一个按钮中,我想用新的字符串数组重新填充该列表视图。

我怎样才能做到这一点?

4

4 回答 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

于 2012-10-09T07:19:21.353 回答
0

您可以将新列表设置为列表视图(myListView)并调用myListView.invalidateViews();

于 2012-10-09T06:14:28.303 回答
0

我假设您正在使用带有 ArrayAdapter 的 ListView,并且 ArrayAdapter 构造函数需要一个数组或一个数组列表作为参数:

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

用这个构造函数创建ArrayAdapter,当你想改变数据时,只需改变引用数组的值,调用notifyDatasetChanged()方法。

于 2012-10-09T06:14:38.943 回答
0

使用从适配器中删除数据adapter.clear()并将新数据填充到适配器并调用

adapter.notifyDataSetChanged() 
于 2012-10-09T06:19:20.843 回答