英文:
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<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();
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()); //<- 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<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
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论