我正在尝试显示列表视图,但事实并非如此。我已阅读其他一些帖子,但无法找到问题。
以下是我设置适配器的代码段。我检查了调试器,之后没有设置为null。
tournaments = (ArrayList) dataSnapshot.getValue();
adapter = new ArrayAdapter(AdminControl.this, android.R.layout.simple_list_item_1, tournaments);
list.setAdapter(adapter);
这是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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.kaushikshivakumar.vexteamqueuing.AdminControl"
tools:showIn="@layout/activity_admin_control"
android:background="#ffffff">
<ListView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="@+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="30dp" />
</RelativeLayout>
这是xml活动文件 - activity_admin_control
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.example.kaushikshivakumar.vexteamqueuing.AdminControl"
app:theme = "@style/NoActionBar">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/NoActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
/>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_input_add"
android:onClick="createNewTournament"
android:clickable="true" />
</android.support.design.widget.CoordinatorLayout>
我的整个活动课程:
package com.example.kaushikshivakumar.vexteamqueuing;
import android.app.ActionBar;
import android.content.Intent;
import android.provider.ContactsContract;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import java.lang.Object.*;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.firebase.client.authentication.*;
//import com.firebase.ui.database.FirebaseListAdapter;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class AdminControl extends AppCompatActivity {
Firebase ref;
FloatingActionButton fab;
public static List <Tournament> tournaments;
public static DataSnapshot currentSnap;
ArrayAdapter adapter;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_control);
Toolbar b = (Toolbar) findViewById(R.id.toolbar);
b.setTitle("Tournaments");
setSupportActionBar(b);
list = (ListView) findViewById(R.id.listView);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
createNewTournament(v);
}
});
ref = AdminLogin.firebase.child("users").child(AdminLogin.firebase.getAuth().getUid());
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() != null){
tournaments = (ArrayList) dataSnapshot.getValue();
adapter = new ArrayAdapter(AdminControl.this, android.R.layout.simple_list_item_1, tournaments);
list.setAdapter(adapter);
}
else{
tournaments = new ArrayList<>();
adapter = new ArrayAdapter(AdminControl.this, android.R.layout.simple_list_item_1, tournaments);
list.setAdapter(adapter);
}
currentSnap = dataSnapshot;
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
public void createNewTournament(View v) {
if (tournaments == null){
System.out.println(tournaments);
}
Intent newIntent = new Intent(this, TournamentCreator.class);
startActivity(newIntent);
}
}
答案 0 :(得分:1)
您需要在activity_admin_control.xml
内加入您的内容文件。现在没有任何内容指向内容文件。活动不知道它存在。