我目前正在使用Android Studio Firebase RealTime数据库开发android应用程序。
我想使列表显示在屏幕上,但是列表没有显示。
我认为应用程序无法从Firebase读取数据。
(我在上面进行了调试和result.size()的值
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>
test_item.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;
}
}
}
答案 0 :(得分:0)
您应该将侦听器添加到列表视图中,或者更好地在其上使用Recyclerview和'OnScrolled侦听器',并保持一个布尔值以显示进度条,并且在加载数据时禁用进度条。推荐-使用分页。