Android 从 ArrayAdapter 刷新 ListView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11786050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:51:34  来源:igfitidea点击:

Refresh ListView from ArrayAdapter

androidandroid-listviewandroid-arrayadapter

提问by 113408

I'm using ArrayAdapterto bind my data from my ArrayListto my ListViewand i use an AlertDialogto insert data into my Arraylist. My problem is that i'm unable to refresh my ListViewafter the changes done.

我正在使用ArrayAdapter将我的数据从 my 绑定ArrayList到 my ListView,我使用 anAlertDialog将数据插入到我的 Arraylist. 我的问题是我无法ListView在更改完成后刷新我的。

Code

代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.config_hidden);
    listView=(ListView) findViewById(R.id.hiddenList);
    xmlFileManager=new XmlFileManager(this);
    addNumber=(Button) findViewById(R.id.addNum);

    addNumber.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

            LayoutInflater factory = LayoutInflater.from(HiddenCall.this);
            final View alertDialogView = factory.inflate(R.layout.add_number, null);
            AlertDialog.Builder adb = new AlertDialog.Builder(HiddenCall.this);
            adb.setView(alertDialogView);
            adb.setTitle(R.string.dialog_title);
            adb.setIcon(R.drawable.phone);

            final AlertDialog alertDialog = adb.create();

            adb.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    numberToAdd=(EditText) alertDialogView.findViewById(R.id.numberToAdd);
                    String number = numberToAdd.getText().toString();
                    if(number.length()>0){
                        xmlFileManager.addNumberToXml(number , HIDDEN_NUMBER_TYPE);
                        adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());
                        adapter.setNotifyOnChange(true);
                        adapter.notifyDataSetChanged();
                    }
                } });

            adb.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    alertDialog.dismiss();
                } });
            adb.show();
        }
    });
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());
    adapter.notifyDataSetChanged();
    adapter.setNotifyOnChange(true);
    listView.setAdapter(adapter);
}

回答by Ixx

You are instantiating a new adapter each time. What you have to do is put the line where you instantiate the adapter before the click listener, and in the click listener modify that adapter and call notifyDataSetChanged() on it. You of course have to add some setters to your adapter in order to modify the data.

您每次都在实例化一个新的适配器。您需要做的是将实例化适配器的行放在单击侦听器之前,然后在单击侦听器中修改该适配器并在其上调用 notifyDataSetChanged()。您当然必须向适配器添加一些 setter 才能修改数据。

Has to look similar to this:

必须看起来类似于:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.config_hidden);
listView=(ListView) findViewById(R.id.hiddenList);

//instantiate the adapter (just one time)
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());

//assign the adapter to the listview
listView.setAdapter(adapter);

xmlFileManager=new XmlFileManager(this);
addNumber=(Button) findViewById(R.id.addNum);

addNumber.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {

        LayoutInflater factory = LayoutInflater.from(HiddenCall.this);
        final View alertDialogView = factory.inflate(R.layout.add_number, null);
        AlertDialog.Builder adb = new AlertDialog.Builder(HiddenCall.this);
        adb.setView(alertDialogView);
        adb.setTitle(R.string.dialog_title);
        adb.setIcon(R.drawable.phone);

        final AlertDialog alertDialog = adb.create();



        adb.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                numberToAdd=(EditText) alertDialogView.findViewById(R.id.numberToAdd);
                String number = numberToAdd.getText().toString();
                if(number.length()>0){
                    xmlFileManager.addNumberToXml(number , HIDDEN_NUMBER_TYPE);
                    //adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());
                    //adapter.setNotifyOnChange(true);

                    //set the changed data
                    adapter.setData(xmlFileManager.getHiddenNumbers());

                    //notify that the model changed
                    adapter.notifyDataSetChanged();
                }
            } });

        adb.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();
            } });
        adb.show();
    }
});

//adapter.notifyDataSetChanged();
//adapter.setNotifyOnChange(true);

回答by Miguel Risco-Castillo

If you have a small ArrayList, to force update a ListView when the ArrayList changes you can use this code snippet:

如果您有一个小的 ArrayList,要在 ArrayList 更改时强制更新 ListView,您可以使用以下代码片段:

ArrayAdapter.clear();
ArrayAdapter.addAll(ArrayList);
ArrayAdapter.notifyDataSetChanged();

回答by Georgy Gobozov

Every click you create new adapter. You don't need to do this. You can modify existing adapter data and call notifyDataSetChanged(). In your case you should call listView.setAdapter(adapter) in onClick method.

每次点击都会创建新的适配器。你不需要这样做。您可以修改现有的适配器数据并调用 notifyDataSetChanged()。在您的情况下,您应该在 onClick 方法中调用 listView.setAdapter(adapter) 。

回答by garbagecollector

Inside your adb.setPositiveButton()'s onClick(), you should not create a new adapter. Remove this line:

在您的adb.setPositiveButton()'s 中onClick(),您不应创建新的适配器。删除这一行:

adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, xmlFileManager.getHiddenNumbers());

Simply do this:

只需这样做:

if(number.length()>0){
    xmlFileManager.addNumberToXml(number , HIDDEN_NUMBER_TYPE);
    adapter.notifyDataSetChanged();
}