如何处理为ListVIEw显示空视图的问题,因为当我从Firebase数据库读取数据并将其显示在ListVIEw中时,我不知道要放入哪些数据.我正在使用Customlistadapter@R_572_6502@颜色和ListVIEw的背景颜色
注意:我正在从Firebase数据库读取数据,并将其显示在ListVIEw中,任何解决方案
Customlistadapter.java
public class Customlistadapter extends BaseAdapter {private static final int HERO_COUNT = 12;private Context context;private List<String> items;public Customlistadapter(Context context) { this.context = context; items = new ArrayList<>();}@OverrIDepublic int getCount() { return HERO_COUNT;}@OverrIDepublic String getItem(int position) { if (position >= 0 && position < items.size()) { return items.get(position); } return "";}@OverrIDepublic long getItemID(int position) { return 0;}@OverrIDepublic VIEw getVIEw(int position, VIEw v, VIEwGroup parent) { VIEw mVIEw = v; if (mVIEw == null) { LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mVIEw = vi.inflate(R.layout.custom_List, null, false); TextVIEw text = (TextVIEw) mVIEw.findVIEwByID(R.ID.textVIEw); //textVIEw.setTextcolor(color.BLUE); text.setText(getItem(position)); if (items.isEmpty()) { // Set holder color } else { text.setTextcolor(color.WHITE); int color = color.argb(200, 255, 64, 64); text.setBackgroundcolor(color); } } return mVIEw;}public voID updateList(List<String> updatedItems) { items.clear(); items.addAll(updatedItems); notifyDataSetChanged();}}
VIEwDatabase.java
public class VIEwDatabase extends AppCompatActivity {private static final String TAG = "VIEwDatabase";//add Firebase Database stuffprivate FirebaseDatabase mFirebaseDatabase;private FirebaseAuth mAuth;private FirebaseAuth.AuthStateListener mAuthListener;private DatabaseReference myRef;private String userID;private ListVIEw mListVIEw;@OverrIDeprotected voID onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.vIEw_database_layout); mListVIEw = (ListVIEw) findVIEwByID(R.ID.ListvIEw); Customlistadapter adapter = new Customlistadapter(this); mListVIEw.setAdapter(adapter); //declare the database reference object. This is what we use to access the database. //NOTE: Unless you are signed in, this will not be useable. mAuth = FirebaseAuth.getInstance(); mFirebaseDatabase = FirebaseDatabase.getInstance(); myRef = mFirebaseDatabase.getReference(); FirebaseUser user = mAuth.getCurrentUser(); // userID = user.getUID(); userID = ""; mAuthListener = new FirebaseAuth.AuthStateListener() { @OverrIDe public voID onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if (user != null) { // User is signed in Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUID()); toastMessage("Successfully signed in."); } else { // User is signed out Log.d(TAG, "onAuthStateChanged:signed_out"); toastMessage("Successfully signed out."); } // ... } }; myRef.addValueEventListener(new ValueEventListener() { @OverrIDe public voID onDataChange(DataSnapshot dataSnapshot) { // This method is called once with the initial value and again // whenever data at this location is updated. showData(dataSnapshot); } @OverrIDe public voID onCancelled(DatabaseError databaseError) { } });}private voID showData(DataSnapshot dataSnapshot) { for (DataSnapshot ds : dataSnapshot.getChildren()) { DailyInfo uInfo = new DailyInfo(); uInfo.setHero1(ds.child(userID).getValue(DailyInfo.class).getHero1()); //set the name1 uInfo.setHero2(ds.child(userID).getValue(DailyInfo.class).getHero2()); //set the name2 uInfo.setHero3(ds.child(userID).getValue(DailyInfo.class).getHero3()); //set the name3 uInfo.setHero4(ds.child(userID).getValue(DailyInfo.class).getHero4()); //set the name4 uInfo.setHero5(ds.child(userID).getValue(DailyInfo.class).getHero5()); //set the name5 uInfo.setHero6(ds.child(userID).getValue(DailyInfo.class).getHero6()); //set the name6 uInfo.setHero7(ds.child(userID).getValue(DailyInfo.class).getHero7()); //set the name7 uInfo.setHero8(ds.child(userID).getValue(DailyInfo.class).getHero8()); //set the name8 uInfo.setHero9(ds.child(userID).getValue(DailyInfo.class).getHero9()); //set the name9 uInfo.setHero10(ds.child(userID).getValue(DailyInfo.class).getHero10()); //set the name10 uInfo.setHero11(ds.child(userID).getValue(DailyInfo.class).getHero11()); //set the name11 uInfo.setHero12(ds.child(userID).getValue(DailyInfo.class).getHero12()); //set the name12 //display all the information Log.d(TAG, "showData: Hero1: " + uInfo.getHero1()); Log.d(TAG, "showData: Hero2: " + uInfo.getHero2()); Log.d(TAG, "showData: Hero3: " + uInfo.getHero3()); Log.d(TAG, "showData: Hero4: " + uInfo.getHero4()); Log.d(TAG, "showData: Hero5: " + uInfo.getHero5()); Log.d(TAG, "showData: Hero6: " + uInfo.getHero6()); Log.d(TAG, "showData: Hero7: " + uInfo.getHero7()); Log.d(TAG, "showData: Hero8: " + uInfo.getHero8()); Log.d(TAG, "showData: Hero9: " + uInfo.getHero9()); Log.d(TAG, "showData: Hero10: " + uInfo.getHero10()); Log.d(TAG, "showData: Hero11: " + uInfo.getHero11()); Log.d(TAG, "showData: Hero12: " + uInfo.getHero12()); ArrayList<String> array = new ArrayList<>(); array.add(uInfo.getHero1()); array.add(uInfo.getHero2()); array.add(uInfo.getHero3()); array.add(uInfo.getHero4()); array.add(uInfo.getHero5()); array.add(uInfo.getHero6()); array.add(uInfo.getHero7()); array.add(uInfo.getHero8()); array.add(uInfo.getHero9()); array.add(uInfo.getHero10()); array.add(uInfo.getHero11()); array.add(uInfo.getHero12()); /* ArrayAdapter adapter = new ArrayAdapter(this,androID.R.layout.simple_List_item_1,array); mListVIEw.setAdapter(adapter);*/ if (mListVIEw.getAdapter() instanceof Customlistadapter) { ((Customlistadapter) mListVIEw.getAdapter()).updateList(array); mListVIEw.setBackgroundcolor(ContextCompat.getcolor(mListVIEw.getContext(), R.color.category_colors)); } }}@OverrIDepublic voID onStart() { super.onStart(); mAuth.addAuthStateListener(mAuthListener);}@OverrIDepublic voID onStop() { super.onStop(); if (mAuthListener != null) { mAuth.removeAuthStateListener(mAuthListener); }}/** * customizable toast * * @param message */private voID toastMessage(String message) { Toast.makeText(this, message, Toast.LENGTH_SHORT).show();}}
vIEw_database_layout.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:orIEntation="vertical"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical"> <TextVIEw androID:ID="@+ID/tvUserInfo" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="User information" androID:textAlignment="center" androID:textcolor="@color/colorPrimaryDark" androID:textSize="25sp"/><ListVIEw androID:ID="@+ID/ListvIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"/></linearLayout></linearLayout>
custom_List.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayoutxmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"><TextVIEw androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:ID="@+ID/textVIEw" androID:textSize="20px" androID:paddingtop="10dip" androID:paddingBottom="10dip"/></linearLayout>
DailyInfo.java
public class DailyInfo {private String hero1;private String hero2;private String hero3;private String hero4;private String hero5;private String hero6;private String hero7;private String hero8;private String hero9;private String hero10;private String hero11;private String hero12;public DailyInfo(){}public String getHero1() { return hero1;}public voID setHero1(String hero1) { this.hero1 = hero1;}public String getHero2() { return hero2; }public voID setHero2(String hero2) { this.hero2 = hero2;}public String getHero3() { return hero3;}public voID setHero3(String hero3) { this.hero3 = hero3;}public String getHero4() { return hero4;}public voID setHero4(String hero4) { this.hero4 = hero4;}public String getHero5() { return hero5;}public voID setHero5(String hero5) { this.hero5 = hero5;}public String getHero6() { return hero6;}public voID setHero6(String hero6) { this.hero6 = hero6;}public String getHero7() { return hero7;}public voID setHero7(String hero7) { this.hero7 = hero7;}public String getHero8() { return hero8;}public voID setHero8(String hero8) { this.hero8 = hero8;}public String getHero9() { return hero9;}public voID setHero9(String hero9) { this.hero9 = hero9;}public String getHero10() { return hero10;}public voID setHero10(String hero10) { this.hero10 = hero10;}public String getHero11() { return hero11;}public voID setHero11(String hero11) { this.hero11 = hero11;}public String getHero12() { return hero12;}public voID setHero12(String hero12) { this.hero12 = hero12;}}
解决方法:
您在ValueEventListener上收到响应后创建适配器:
Customlistadapter adapter = new Customlistadapter(VIEwDatabase.this , R.layout.custom_List , mList); ListVIEw mListVIEw= findVIEwByID(R.ID.ListvIEw); mListVIEw.setAdapter(adapter);
取而代之的是,例如,在onCreate内的VIEwDatabase上设置适配器,并根据该初始状态设置颜色.
@OverrIDe protected voID onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.vIEw_database_layout); mListVIEw = findVIEwByID(R.ID.ListvIEw); Customlistadapter adapter = new Customlistadapter(this); mListVIEw.setAdapter(adapter); //[...] }
并且,而不是创建适配器,而是对其进行更新:
private voID showData(DataSnapshot dataSnapshot) { //[...] if (mListVIEw.getAdapter() instanceof Customlistadapter) { ((Customlistadapter)mListVIEw.getAdapter()).updateList(array); } }
您还可以将ArrayAdapter替换为BaseAdapter作为扩展类:
public class Customlistadapter extends BaseAdapter { private static final int HERO_COUNT = 12; private Context context; private List<String> items; public Customlistadapter(Context context) { this.context = context; items = new ArrayList<>(); } @OverrIDe public int getCount() { return HERO_COUNT; } @OverrIDe public String getItem(int position) { if (position >= 0 && position < items.size()) { return items.get(position); } return ""; } @OverrIDe public long getItemID(int position) { return 0; } @OverrIDe public VIEw getVIEw(int position, VIEw v, VIEwGroup parent) { VIEw mVIEw = v; if (mVIEw == null) { LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mVIEw = vi.inflate(R.layout.custom_List, null, false); } //Bind vIEw content here //Todo associate holder to tag return mVIEw; } public voID updateList(List<String> updatedItems) { items.clear(); items.addAll(updatedItems); notifyDataSetChanged(); } }}
编辑:
为了在接收值时更改列表上的默认背景色,可以在主布局上设置默认颜色:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:orIEntation="vertical"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical"> <TextVIEw androID:ID="@+ID/tvUserInfo" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="User information" androID:textAlignment="center" androID:textcolor="@color/colorPrimaryDark" androID:textSize="25sp"/><ListVIEw androID:ID="@+ID/ListvIEw" androID:background="@color/empty_color" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"/></linearLayout></linearLayout>
然后在收到值时更改它:
private voID showData(DataSnapshot dataSnapshot) { //[...] if (mListVIEw.getAdapter() instanceof Customlistadapter) { ((Customlistadapter)mListVIEw.getAdapter()).updateList(array); ListVIEw.setBackgroundcolor(ContextCompat.getcolor(ListVIEw.getContext(), R.color.loaded_color)); } }
如果要更改行颜色,则需要在字符串为空时更改getVIEw上的行布局.
编辑2
指定在何处进行视图更改
以上是内存溢出为你收集整理的java-阅读firebase DB后如何在ListView上更改颜色和字体全部内容,希望文章能够帮你解决java-阅读firebase DB后如何在ListView上更改颜色和字体所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)