为什么 ArrayList 没有插入一些值?

huangapple go评论68阅读模式
英文:

Why is ArrayList not inserting some values?

问题

        items = new ArrayList<String>();
        itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
        listView.setAdapter(itemsAdapter);
        registerForContextMenu(listView);
        fstore.child(/*MyFolder1*/).child(/*MyFolder2*/).listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {
            @Override
            public void onSuccess(ListResult listResult) {
                for(StorageReference sref : listResult.getItems())
                {
                    items.add(sref.getName());  // <- NOT working
                    System.out.println("IIIIII: "+sref.getName());  //sref.getName() returns proper value here
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(tabtwoactivity.this, "Fetching failed", Toast.LENGTH_LONG).show();
            }
        });
       itemsAdapter.notifyDataSetChanged();
英文:
        items = new ArrayList&lt;String&gt;();
        itemsAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, items);
        listView.setAdapter(itemsAdapter);
        registerForContextMenu(listView);
        fstore.child(/*MyFolder1*/).child(/*MyFolder2*/).listAll().addOnSuccessListener(new OnSuccessListener&lt;ListResult&gt;() {
            @Override
            public void onSuccess(ListResult listResult) {
                for(StorageReference sref : listResult.getItems())
                {
                    items.add(sref.getName());  //&lt;- NOT working
                    System.out.println(&quot;IIIIII: &quot;+sref.getName());  //sref.getName() returns proper value here
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(tabtwoactivity.this, &quot;Fetching failed&quot;, Toast.LENGTH_LONG).show();
            }
        });
       itemsAdapter.notifyDataSetChanged();

Why doesn't sref.getName() get added to the list? All items.add() statements before and after work properly.

Ignore this part - I need to add some more info to be able to publish the question. I hope this much is enough.

答案1

得分: 1

Firebase Storage API的listAll()是异步的,在结果可用之前会立即返回。您的回调会在一段时间后被调用,即在结果可用后被触发。当listAll()在执行时,您的代码会立即渲染空列表,并且notifyDataSetChanged最终不起作用。您的代码需要在尝试渲染任何视图之前等待结果完成。

相反,尝试在onSuccess回调内部调用itemsAdapter.notifyDataSetChanged(),以强制适配器渲染新的结果。

fstore.child(/*MyFolder1*/).child(/*MyFolder2*/).listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {

            @Override
            public void onSuccess(ListResult listResult) {
                for(StorageReference sref : listResult.getItems())
                {
                    items.add(sref.getName());  //&lt;- NOT working
                    System.out.println("IIIIII: "+sref.getName());  //sref.getName() 在这里返回正确的值
                }
                itemsAdapter.notifyDataSetChanged();
            }

您可能还希望在listAll()完成之前决定要显示什么内容,因为它可能不如您期望的那么快。

英文:

The Firebase Storage API listAll() is asynchronous and returns immediately before the results are available. Your callback is invoked some time later, after the results are available. While listAll() is executing, your code does on to immediately render the empty list, and notifyDataSetChanged ends up doing nothing. Your code needs wait for the results to complete before trying to render any views.

Try instead calling itemsAdapter.notifyDataSetChanged() from within the onSuccess callback to force the adapter to render the new results.

fstore.child(/*MyFolder1*/).child(/*MyFolder2*/).listAll().addOnSuccessListener(new OnSuccessListener&lt;ListResult&gt;() {

            @Override
            public void onSuccess(ListResult listResult) {
                for(StorageReference sref : listResult.getItems())
                {
                    items.add(sref.getName());  //&lt;- NOT working
                    System.out.println(&quot;IIIIII: &quot;+sref.getName());  //sref.getName() returns proper value here
                }
                itemsAdapter.notifyDataSetChanged();
            }

You will probably also want to decide what you want to display before listAll() is complete, as it might not be as fast as you want.

huangapple
  • 本文由 发表于 2020年9月26日 23:30:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64079440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定