我通过从Firebase数据库中读取数据来制作一个gridview来显示图标列表(图像和文本),这是第一次调用该活动时gridview显示为空白。只有在我更改方向后才会加载数据。
请告知我如何在第一时间显示它。(这是我第一次开发Android应用程序,之前从不编写Java代码) 谢谢
以下是我的代码:
package com.abc.expensestracker;
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; import java.util.ArrayList; public class NewCategoryIconListActivity extends AppCompatActivity { private GridView mIconListGridView; private IconsListGridViewAdapter mIconListGridViewAdapter; public ArrayList<IconsImageGridItemData> mIconsImageArrData = null; private DatabaseReference mIconImagesRef; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new_category_icon_list); mIconListGridView = (GridView) findViewById(R.id.icons_list_gridview); mIconImagesRef = FirebaseDatabase.getInstance().getReference().child("Iconimages"); mIconsImageArrData = new ArrayList<IconsImageGridItemData>(); setListData(); mIconListGridViewAdapter = new IconsListGridViewAdapter(this,R.layout.icons_single_item_layout,mIconsImageArrData);//(this, R.layout.icons_single_item_layout, mGridData); mIconListGridView.setAdapter(mIconListGridViewAdapter); mIconListGridViewAdapter.setGridData(mIconsImageArrData); mIconListGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //Get item at position IconsImageGridItemData item = (IconsImageGridItemData) parent.getItemAtPosition(position); String selectedIconURL = item.getIconURL(); //send back the selected icon to parent activity Intent returnIntent = getIntent(); returnIntent.putExtra("icon_URL",selectedIconURL); setResult(Activity.RESULT_OK,returnIntent); finish(); } }); } /****** Function to set data in ArrayList *************/ public void setListData() { mIconImagesRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for(DataSnapshot categorySnapshot: dataSnapshot.getChildren()){ String iconURL = categorySnapshot.child("iconURL").getValue(String.class); final IconsImageGridItemData iconsImageData = new IconsImageGridItemData(); /******* Firstly take data in model object ******/ iconsImageData.setIconURL(iconURL); /******** Take Model Object in ArrayList **********/ mIconsImageArrData.add(iconsImageData); } } @Override public void onCancelled(DatabaseError databaseError) { } }); } }
答案 0 :(得分:0)
您需要通知适配器您已添加项目。 所以在你的适配器中执行以下操作:
public void add (IconsImageGridItemData iconsImageData) {
mIconsImageArrData.add (0, iconsImageData);
notifyItemInserted(0); //position to add the item
}
然后在你的setListData:
final IconsImageGridItemData iconsImageData = new IconsImageGridItemData();
/******* Firstly take data in model object ******/
iconsImageData.setIconURL(iconURL);
adapter.add (iconsImageData);
/******** Take Model Object in ArrayList**********/
// mIconsImageArrData.add(iconsImageData);
答案 1 :(得分:0)
在上面的代码中形成我能理解的内容,setListData();
内的任务在执行后完成
mIconListGridViewAdapter.setGridData(mIconsImageArrData);
一个简单的改变应该有效。完成内部eventlistener的onDataChange()函数后,在你的setListData()函数旁边
/******** Take Model Object in ArrayList **********/
mIconsImageArrData.add(iconsImageData);
}// For finish
mIconListGridViewAdapter.setGridData(mIconsImageArrData);
}// end of onDataChange
这将确保您的适配器在mIconsImageArrData完成数据复制后立即获取数据
注意: - 我假设你肯定在调用notifyDataSetChanged();在setGridData函数中复制数据。