ArrayAdapter,ListView控件,创建和更新内容 [英] ArrayAdapter, ListView, creating and updating contents

查看:158
本文介绍了ArrayAdapter,ListView控件,创建和更新内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我再次发布此,试图让这个问题/问题更清楚:

 的ListView LV;
清单<项目>项目;
localStorage的LS;
RemoteStorage RS;
ItemArrayAdapter适配器;...getShoppingList(); //< ---这种方法填充的项目ArrayList的
适配器=新ItemArrayAdapter(getApplicationContext(),R.layout.item_listitem,
                               项目); //< ---创建一个ArrayAdapter并插入
                                        // ArrayList中...
lv.setAdapter(适配器);

在code到目前为止填充ArrayList的项目,创建一个适配器,并增加了项目这一点,然后说明它在ListView LV。到目前为止,一切都很好。

然而,getShoppingList()方法被运行几次,更新该ArrayList物品。而不是每到这个发生时重新适配器,我想在ListView LV刚刚更新并显示更新的内容。

我想要是这样的:

OnClick方法为更新按钮:

 公共无效的onClick(视图v){
    如果(((按钮)V).getText()。等于(更新)){
        尝试{
            tmpitems = rs.getNewEntries(CDATE,latestitem);
            如果(tmpitems.size()大于0){
                updateFooter(tmpitems.size()+在服务器上找到了新的项目!);
                ls.UpdateLocalList(tmpitems);
                getShoppingList();
                updateLatestItem();
                adapter.setNotifyOnChange(真); //
                adapter.notifyDataSetChanged(); //这是不工作
                lv.setAdapter(适配器); //
            }其他{
                updateFooter(服务器上找不到新项目。);
            }
        }赶上(NPE的NullPointerException){
            Log.e(DEBUG_TAGNPException我的onClick():+ npe.toString());
            updateFooter(服务器上找不到新项目。);
        }

然而,用于更新适配器(项目已被更新后)线也绝对没有什么。我知道,项目更新,因为它也被写入文件,所以这个问题是不存在的。此外,如果我取代更新与重新创建线适配器的线,它工作得很好。

更新(2012年9月16日):

 私人无效getShoppingList(){
    项目= ls.getShoppingList(SHOPPINGLIST_FILE,CDATE);
}

该ls.getShoppingList()方法:

 公开名单<项目> getShoppingList(字符串文件名,日期字符串){
    接下来的String [] = {};
    清单<项目> NLIST =新的ArrayList<项目>();    尝试{
        在的InputStream = ctx.openFileInput(shoppinglists.csv);
        InputStreamReader的ISR =新的InputStreamReader(中);
        CSVReader读卡器=新CSVReader(ISR);        为(;;){
            接下来= reader.readNext();
            如果(下!= NULL){
                如果(下一[0] .equals(日期)){
                    nlist.add(新项目(下一个[0],接下来的[1],接下来的[2]
                    下[3],接下来的[4],接下来的[5]));
                }
            }其他{
                打破;
            }
        }
    }赶上(IOException异常五){
        e.printStackTrace();
        Log.e(DEBUG_TAGIOException的在列表():+ e.toString());
    }    返回NLIST;
}

----------原帖------------------

我有显示的项目行的ArrayAdapter一个ListView LV。

 列表<项目>项目;
ItemArrayAdapter适配器;
适配器=新ItemArrayAdapter(getApplicationContext(),R.layout.item_listitem,
                               项目);lv.setAdapter(适配器);

这工作得很好。该项目ArrayList是填充,适配器显示ListView中的内容。

不过,后来我做一些东西,更新ArrayList的项目。然后,我要更新的适配器和ListView控件。但是,这code,

  adapter.setNotifyOnChange(真);
adapter.notifyDataSetChanged();

显然没有做到这一点。那么,我该如何更新适配器,因此ListView控件与更新的项目的ArrayList?如果没有重新初始化适配器,那是什么?

编辑:

该项目ArrayList是正是如此更新:

 如果(((按钮)V).getText()。等于(更新)){
    尝试{
        tmpitems = rs.getNewEntries(CDATE,latestitem);
            如果(tmpitems.size()大于0){
                ls.UpdateLocalList(tmpitems);
                getShoppingList();
                refreshList();
           }其他{
           updateFooter(服务器上找不到新项目。);
       }
    }赶上(NPE的NullPointerException){
        Log.e(DEBUG_TAGNPException我的onClick():+ npe.toString());
        updateFooter(服务器上找不到新项目。);
    }
}refreshList(){
    adapter.setNotifyOnChange(真);
    adapter.notifyDataSetChanged();
}

- 新的项目被读入tmpitems ArrayList中。
- 如果新项目的数量是> 0,一个本地文件与tmpitems更新。
- 然后,物品的ArrayList从本地文件(带有getShoppingList()方法,这增加了更新的
新的条目)
- 然后我尝试运行refreshList()方法,这是应该更新适配器,如上mentioened

的项目ArrayList是更新,并且本地文件被更新。这只是适配器,我不能去上班的更新方法。当初始化适配器时,项目显示在ListView - 更新时,新的内容不是


解决方案

使用((ItemArrayAdapter)lv.getAdapter())。notifyDataSetChanged()而不是 adapter.notifyDataSetChanged()

更新:
问题是, notifyDataSetChanged()工作,只有当数据的的适配器发生了变化,它重新渲染列表。你的情况,你更改数据的外部的,所以你必须改变里面的数据的 的适配器。

您需要更换 nlist.add(...)插入()删除()的适配器方法:的官方

一些澄清它如何工作的:在初始化适配器,它只是坚持你传递给它以它自己的那些项目数组。这是不知道你的项目数组所做了修改。

I posting this again, trying to make the question/problem clearer:

ListView lv;
List<Item> items;
Localstorage ls;
RemoteStorage rs;
ItemArrayAdapter adapter;

...

getShoppingList();  // <--- this method populates the items ArrayList
adapter = new ItemArrayAdapter(getApplicationContext(), R.layout.item_listitem, 
                               items);  // <--- Create an ArrayAdapter and insert the 
                                        //      ArrayList...
lv.setAdapter(adapter);

The code so far populates the ArrayList items, creates an adapter and adds items to this, and then shows it in the ListView lv. So far, so good.

However, the getShoppingList() method is run several times, updating the ArrayList items. And instead of recreating the adapter every time this happens, I'd like to just update it and display the updated contents in the ListView lv.

What I'm trying is this:

OnClick method for the "Update" button:

public void onClick(View v) {
    if (((Button)v).getText().equals("Update")) {
        try {
            tmpitems = rs.getNewEntries(cdate, latestitem);
            if (tmpitems.size() > 0) {
                updateFooter(tmpitems.size() + " new items found on server!");
                ls.UpdateLocalList(tmpitems);
                getShoppingList();               
                updateLatestItem(); 
                adapter.setNotifyOnChange(true);    //
                adapter.notifyDataSetChanged();     // THIS IS WHAT ISN'T WORKING
                lv.setAdapter(adapter);             //
            } else {
                updateFooter("No new items found on server.");
            }
        } catch (NullPointerException npe) {
            Log.e(DEBUG_TAG, "NPException i onClick(): " + npe.toString());
            updateFooter("No new items found on server.");
        }

However, the lines for updating the adapter (after items have been updated) does absolutely nothing. I know that items is updated, because it's also written to a file, so the problem is not there. Besides, if I replace the lines for updating the adapter with lines for recreating it, it works nicely.

Update (sept. 16, 2012):

private void getShoppingList () {
    items = ls.getShoppingList(SHOPPINGLIST_FILE, cdate);
}

The ls.getShoppingList() method:

public List<Item> getShoppingList (String filename, String date) {
    String next[] = {};
    List<Item> nlist = new ArrayList<Item>();

    try {
        InputStream in = ctx.openFileInput("shoppinglists.csv");
        InputStreamReader isr = new InputStreamReader(in);
        CSVReader reader = new CSVReader(isr);

        for(;;) {
            next = reader.readNext();
            if(next != null) {
                if (next[0].equals(date)) {
                    nlist.add(new Item(next[0], next[1], next[2], 
                    next[3], next[4], next[5]));
                }
            } else {
                break;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        Log.e(DEBUG_TAG, "IOException in List(): " + e.toString());
    }

    return nlist;
}

---------- Original post ------------------

I have a ListView lv with an ArrayAdapter for displaying rows of items.

List<Item> items;
ItemArrayAdapter adapter;
adapter = new ItemArrayAdapter(getApplicationContext(), R.layout.item_listitem, 
                               items);

lv.setAdapter(adapter);

This works nicely. The items arraylist is populated, and the adapter shows the contents in the ListView.

However, then I do some stuff that updates the ArrayList items. And then I want to update the adapter and the ListView. But this code,

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

obviously doesn't do it. So, how do I update the adapter and hence the ListView with the updated items ArrayList? Without reinitializing the adapter, that is?

EDIT:

The items ArrayList is updated thusly:

if (((Button)v).getText().equals("Update")) {
    try {
        tmpitems = rs.getNewEntries(cdate, latestitem);
            if (tmpitems.size() > 0) {
                ls.UpdateLocalList(tmpitems);
                getShoppingList();
                refreshList();
           } else {
           updateFooter("No new items found on server.");
       }
    } catch (NullPointerException npe) {
        Log.e(DEBUG_TAG, "NPException i onClick(): " + npe.toString());
        updateFooter("No new items found on server.");
    }
}

refreshList() {
    adapter.setNotifyOnChange(true);
    adapter.notifyDataSetChanged();
}

- New items are read into the tmpitems ArrayList. - If the number of new items are >0, a local file is updated with the tmpitems. - Then the items ArrayList is updated from the local file (with the getShoppingList() method, which adds the new entries) - Then I try running the refreshList() method, which is supposed to update the adapter, as mentioened above.

The items ArrayList IS updated, and the local files are updated. It's just the update method on the adapter I can't get to work. When initializing the adapter, the items are shown in the ListView - when updating it, the new contents are not.

解决方案

Use ((ItemArrayAdapter)lv.getAdapter()).notifyDataSetChanged() instead of adapter.notifyDataSetChanged()

Update: The thing is that notifyDataSetChanged() works only when the data inside the adapter has changed, and it rerenders the list. In your case you change the data externally, so you have to change the data inside the adapter.

You will need to replace nlist.add(...) with insert() and remove() methods of the adapter: official reference

Some clarification on how it works: when you initialize adapter, it just sticks the items array you've passed to it to it's own ones. It is not aware of the changes made to your items array anymore.

这篇关于ArrayAdapter,ListView控件,创建和更新内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆