更新列表视图在适配器的数据变化适配器、视图、数据、列表

2023-09-12 21:46:44 作者:晚风多情

在与阵列适配器相关的数据发生变化时,无效的列表视图足以显示更新的值?下面这段code不工作,我有没有误解的东西在这里?

 公共类ZeroItemListActivity延伸活动{
    私人的ArrayList<字符串> listItems中的=新的ArrayList<字符串>();
    私人的ListView mMyListView;
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        mMyListView =(ListView控件)findViewById(R.id.MyListView);
        mMyListView.setAdapter(新ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_1,listItems中));
    }
    公共无效的addItem(视图v){
        listItems.add(列表项目);
        mMyListView.invalidate();
    }
}
 

布局中使用:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
    < TextView的Andr​​oid的:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT机器人:文本=@字符串/你好/>
    < ListView的机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / MyListView>< / ListView控件>
    <按钮机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / AddItemsButton
        机器人:文本=添加项目安卓的onClick =的addItem>< /按钮>
< / LinearLayout中>
 
常用技巧 SqlServer 2012 数据库中给其它用户设置指定的表或视图的访问权限

解决方案

替代:

  mMyListView.invalidate();
 

有:

 ((BaseAdapter)mMyListView.getAdapter())notifyDataSetChanged()。
 

如果那不工作,请参阅此主题: Android的列表视图刷新

When the data associated with array adapter is changed, invalidating the listview is sufficient to show the updated values? Following piece of code is not working, did i misunderstood something here.?

public class ZeroItemListActivity extends Activity {
    private ArrayList<String> listItems=new ArrayList<String>();
    private ListView mMyListView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mMyListView=(ListView) findViewById(R.id.MyListView);
        mMyListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listItems));
    }
    public void addItem(View v){
        listItems.add("list Item");
        mMyListView.invalidate();
    }
}

Layout used :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />
    <ListView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/MyListView"></ListView>
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/AddItemsButton"
        android:text="Add Items" android:onClick="addItem"></Button>
</LinearLayout>

解决方案

substitute:

mMyListView.invalidate();

for:

((BaseAdapter) mMyListView.getAdapter()).notifyDataSetChanged(); 

If that doesnt work, refer to this thread: Android List view refresh