java - 列表未显示在屏幕上(Android Studio、Firebase)

标签 java android xml

我目前正在使用 Android Studio、Firebase 实时数据库开发 android 应用程序。

我想让列表显示在屏幕上,但是列表没有显示。 我相信应用程序未能从 firebase 读取数据。 (我在 ResultsAdmin.java Log.e("The read success: ","su"+result.size()); 为0。所以,我认为数据传输成功,但内容为空.)

对于造成这种情况的原因,我非常感谢有用的建议。

相关代码如下。

activity_tests.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".Tests">


<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="?android:attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>

<include layout="@layout/content_tests" />



</RelativeLayout>

content_tests.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
>

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/test_listview">

</ListView>
<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_gravity="center|bottom"
    android:layout_marginBottom="20dp"
    android:visibility="gone" />
</FrameLayout>

测试项目.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginBottom="@dimen/activity_horizontal_margin">

<TextView
    android:id="@+id/item_textView"
    android:layout_width="wrap_content"
    android:layout_height="?android:actionBarSize"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@+id/item_imageView"
    android:gravity="center_vertical|center_horizontal"/>

<ImageView
    android:id="@+id/item_imageView"
    android:layout_width="?android:actionBarSize"
    android:layout_height="?android:actionBarSize" />
<Button
    android:layout_margin="5dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:id="@+id/item_button"
    android:background="@color/colorPrimaryDark"
    android:layout_alignParentEnd="true"/>

</RelativeLayout>

ResultsAdmin.java(我缩写了import和package语句。)

public class ResultsAdmin extends AppCompatActivity {

private FirebaseDatabase database;
private FirebaseAuth auth;
private DatabaseReference myRef;
private ProgressBar progressBar;
private ListView listView;
private ResultsAdmin.TestAdapter testAdapter;
ArrayList<String> result=new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    auth=FirebaseAuth.getInstance();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tests);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    progressBar.setVisibility(View.VISIBLE);
    database=FirebaseDatabase.getInstance();
    myRef=database.getReference();
    listView=findViewById(R.id.test_listview);
    testAdapter=new ResultsAdmin.TestAdapter(ResultsAdmin.this,result);
    listView.setAdapter(testAdapter);
    getResults();
}

public void getResults(){

    myRef.child("Results").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            result.clear();
            for(DataSnapshot snapshot:dataSnapshot.getChildren()){
                result.add(snapshot.getKey()); //keyを取得している。Verbal~のこと。
            }
            testAdapter.dataList=result;
            testAdapter.notifyDataSetChanged();
            progressBar.setVisibility(View.GONE);
            Log.e("The read success: " ,"su"+result.size());
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            progressBar.setVisibility(View.GONE);
            Log.e("The read failed: " ,databaseError.getMessage());
        }
    });
}
class TestAdapter extends ArrayAdapter<String> {
    private Context mContext;
    ArrayList<String> dataList;
    public TestAdapter( Context context,ArrayList<String> list) {
        super(context, 0 , list);
        mContext = context;
        dataList = list;
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View listItem = convertView;
        if(listItem == null)
            listItem = LayoutInflater.from(mContext).inflate(R.layout.test_item,parent,false);
       ((ImageView)listItem.findViewById(R.id.item_imageView)).setImageDrawable
(ContextCompat.getDrawable(mContext,R.drawable.ic_assessment_black_24dp));
        ((TextView)listItem.findViewById(R.id.item_textView)).setText(dataList.get(position));
        ((Button)listItem.findViewById(R.id.item_button)).setText("View");
        ((Button)listItem.findViewById(R.id.item_button)).setOnClickListener(new 
View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(ResultsAdmin.this,ResultsAdminDetailed.class);
                intent.putExtra("test",dataList.get(position));
                startActivity(intent);
            }
        });
        return listItem;
    }
}
}

最佳答案

您应该向 ListView 添加一个监听器,或者更好地使用 Recyclerview 和“OnScrolled 监听器”,并保持一个 boolean 值以显示进度条,并在加载数据时禁用进度条。推荐 - 使用分页。

关于java - 列表未显示在屏幕上(Android Studio、Firebase),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58255228/

相关文章:

php - 此页面包含以下错误 : error on line 1 at column 1: Document is empty

android - 使用volley通过按钮将recyclerview中特定卡的数据存储在mysql中

android - 根据日期选择器更改从数据库重新加载 ListView 内容

java - EclipseLink Moxy unmarshall 具有不同元素名称的集合

java - 使用Java ProcessBuilder调用全局安装的 Node 模块

java - 重叠相对布局的透明前景,使相对布局内的 TextView 颜色为灰色透明

jquery - 导入 XML 值?

Android BADTOKENEXCEPTION : UNABLE TO ADD WINDOW, 是您的 Activity 正在运行

java - 如何使用 logback.xml 将 Apache Camel 日志级别设置为 ERROR

java - 使用STS,如何添加应用上下文