热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

java阅读firebaseDB后如何在ListView上更改颜色和字体

如何处理为ListView显示空视图的问题,因为当我从Firebase数据库读取数据并将其显示在ListView中时,我不知道要放入哪些数据.我正在使用CustomListAdapter更改字体

如何处理为ListView显示空视图的问题,因为当我从Firebase数据库读取数据并将其显示在ListView中时,我不知道要放入哪些数据.我正在使用CustomListAdapter更改字体颜色和ListView的背景颜色

注意:我正在从Firebase数据库读取数据,并将其显示在ListView中,任何解决方案

CustomListAdapter.java

public class CustomListAdapter extends BaseAdapter {
private static final int HERO_COUNT = 12;
private Context context;
private List 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 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);
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 updatedItems) {
items.clear();
items.addAll(updatedItems);
notifyDataSetChanged();
}
}

ViewDatabase.java

public class ViewDatabase extends AppCompatActivity {
private static final String TAG = "ViewDatabase";
//add Firebase Database stuff
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;
private String userID;
private ListView mListView;
@Override
protected 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 inLog.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());toastMessage("Successfully signed in.");
} else {// User is signed outLog.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 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));
}
}
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public 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


android:layout_
android:layout_
android:orientation="vertical">
android:layout_
android:layout_
android:orientation="vertical">
android:id="@+id/tvUserInfo"
android:layout_
android:layout_
android:text="User Information"
android:textAlignment="center"
android:textColor="@color/colorPrimaryDark"
android:textSize="25sp"/>
android:id="@+id/listview"
android:layout_
android:layout_/>


custom_list.xml


xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>
android:layout_
android:layout_
android:id="@+id/textView"
android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/>

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 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 }
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 updatedItems) {
items.clear();
items.addAll(updatedItems);
notifyDataSetChanged();
}
}
}

编辑:

为了在接收值时更改列表上的默认背景色,可以在主布局上设置默认颜色:


android:layout_
android:layout_
android:orientation="vertical">
android:layout_
android:layout_
android:orientation="vertical">
android:id="@+id/tvUserInfo"
android:layout_
android:layout_
android:text="User Information"
android:textAlignment="center"
android:textColor="@color/colorPrimaryDark"
android:textSize="25sp"/>
android:id="@+id/listview"
android:background="@color/empty_color"
android:layout_
android:layout_/>


然后在收到值时更改它:

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
指定在何处进行视图更改


推荐阅读
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 2023年京东Android面试真题解析与经验分享
    本文由一位拥有6年Android开发经验的工程师撰写,详细解析了京东面试中常见的技术问题。涵盖引用传递、Handler机制、ListView优化、多线程控制及ANR处理等核心知识点。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • Windows服务与数据库交互问题解析
    本文探讨了在Windows 10(64位)环境下开发的Windows服务,旨在定期向本地MS SQL Server (v.11)插入记录。尽管服务已成功安装并运行,但记录并未正确插入。我们将详细分析可能的原因及解决方案。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
author-avatar
青藤摄影876
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有