В настоящее время я разрабатываю приложение для Android с использованием Android Studio, базы данных Firebase RealTime.
Я бы хотел, чтобы список отображался на экране, но список не отображался. Я считаю, что приложение не смогло прочитать данные из базы данных. (Я сделал отладку, и значение result.size () для ResultsAdmin.java Log.e("The read success: " ,"su"+result.size());
было равно 0. Итак, я думаю, что передача данных прошла успешно, но содержимое пустое.)
Iочень признателен за полезные советы относительно того, что вызвало эту ситуацию.
Ниже приведены соответствующие коды.
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;
}
}
}