2017-06-17 50 views
0

我能夠將數據放入Firebase數據庫,但是當我想要檢索它並將其顯示到ListView時,它只顯示空白屏幕。不能在Firebase的ListView中顯示

BaseActivity類:

public class BaseActivity extends AppCompatActivity { 

    @Bean 
    OttoBus bus; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     bus.register(this); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     bus.unregister(this); 
    } 
} 

HomeActivity類:

@OptionsMenu(R.menu.signout) 
@EActivity(R.layout.activity_home) 
public class HomeActivity extends BaseActivity { 

    private static final int LOGIN_REQUEST_CODE = 42; 

    @ViewById 
    ListView listView; 

    @Bean 
    ConversationAdapter conversationAdapter; 

    @Bean 
    UserDao userDao; 

    @AfterViews 
    void init() { 
     // if no user is logged in 
     if (FirebaseAuth.getInstance().getCurrentUser() == null) { 
      LoginActivity_.intent(this).startForResult(LOGIN_REQUEST_CODE); 
     } else { 
      userDao.init(); 
     } 

     listView.setAdapter(conversationAdapter); 
    } 

    @OnActivityResult(value = LOGIN_REQUEST_CODE) 
    void loginSucceeded(int resultCode) { 
     if (resultCode != RESULT_OK) { 
      return; 
     } 
     userDao.init(); 
     conversationAdapter.resetConversationFlow(); 
    } 

    @Subscribe 
    public void usersLoaded(UsersLoadedEvent event) { 
     final FirebaseUser firebaseUser = FirebaseAuth 
       .getInstance().getCurrentUser(); 
     if (userDao.userExists(firebaseUser.getUid())) { 
      userDao.setCurrentUser(userDao.getUserById(firebaseUser.getUid())); 
     } else { 
      final User user = new User(firebaseUser.getUid(), 
        firebaseUser.getDisplayName(), 
        firebaseUser.getPhotoUrl().toString()); 
      userDao.write(user); 
      userDao.setCurrentUser(user); 
     } 
    } 

    @ItemClick 
    void listViewItemClicked(Conversation conversation) { 
     ConversationActivity_.intent(this) 
       .conversation(conversation) 
       .start(); 
    } 

    /** 
    * When option item with id signOut is clicked. 
    */ 
    @OptionsItem 
    void signOut() { 
     FirebaseAuth.getInstance().signOut(); 

     // restart this activity after user is logged out because if there is no user we will start 
     // login activity 
     final Intent intent = getIntent(); 
     finish(); 
     startActivity(intent); 
    } 

    /** 
    * Called when button with id=fab is clicked. 
    */ 
    @Click 
    void fab() { 
     CreateConversationActivity_.intent(this).start(); 
    } 
} 

activity_home.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.nemus.execomchatworkshop.activity.HomeActivity"> 

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

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:layout_margin="@dimen/large_margin" 
     android:src="@drawable/ic_add_black_24dp" /> 

</RelativeLayout> 

ConversationItemView類:

@EViewGroup(R.layout.item_view_conversation) 
public class ConversationItemView extends LinearLayout { 

    @ViewById 
    TextView title; 

    public ConversationItemView(Context context) { 
     super(context); 
    } 

    public void bind(Conversation conversation) { 
     title.setText(conversation.getTitle()); 
    } 
} 

item_view_conversation.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:background="?android:attr/selectableItemBackground"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

     <ImageView 
      android:id="@+id/conversationPhoto" 
      android:layout_width="@dimen/image_large_size" 
      android:layout_height="@dimen/image_large_size" 
      android:layout_margin="@dimen/medium_margin" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/large_margin" 
      android:orientation="vertical"> 

      <TextView 
       android:id="@+id/title" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:textColor="@color/primary_text" 
       android:textSize="@dimen/large_text" /> 

      <TextView 
       android:id="@+id/conversationPreview" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:textColor="@color/secondary_text" 
       android:textSize="@dimen/medium_text" /> 

     </LinearLayout> 

    </LinearLayout> 

</RelativeLayout> 

ConversationDao類

@EBean(scope = EBean.Scope.Singleton) 
public class ConversationDao { 

    static final String CONVERSATION_TAG = "conversations"; 

    private FirebaseDatabase database = FirebaseDatabase.getInstance(); 

    private List<Conversation> conversations = new ArrayList<>(); 

    private Map<String, Conversation> conversationMap = new HashMap<>(); 

    @Bean 
    OttoBus bus; 

    /** 
    * After this class is injected call this method. 
    */ 
    @AfterInject 
    public void init() { 
     database.getReference(CONVERSATION_TAG).addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       conversationMap = dataSnapshot.getValue(
         new GenericTypeIndicator<Map<String, Conversation>>() { 
         }); 
       publish(); 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 
    } 

    public void write(Conversation conversation) { 
     final DatabaseReference databaseReference = 
       database.getReference(CONVERSATION_TAG).push(); 

     conversation.setId(databaseReference.getKey());  // set unique key to our conversation 
     databaseReference.setValue(conversation);   // push conversation to database 
    } 

    public List<Conversation> getConversations() { 
     return conversations; 
    } 

    private void publish() { 
     conversations.clear(); 
     if (conversationMap != null) { 
      conversations.addAll(conversationMap.values()); 
     } 

     // post to event bus 
     bus.post(new ConversationsUpdateEvent()); 
    } 

ConversationAdapter類:

@EBean 
public class ConversationAdapter extends BaseAdapter { 

    private List<Conversation> conversations = new ArrayList<>(); 

    @RootContext 
    Context context; 

    @Bean 
    ConversationDao conversationDao; 

    @Bean 
    OttoBus bus; 

    /** 
    * This method is called after this class is injected. 
    */ 
    @AfterInject 
    void init() { 
     bus.register(this); 
    } 

    public void resetConversationFlow() { 
     conversationDao.init(); 
    } 

    @Override 
    public int getCount() { 
     return conversations.size(); 
    } 

    @Override 
    public Conversation getItem(int position) { 
     return conversations.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final ConversationItemView conversationItemView; 

     if (convertView == null) { // if view item is not created 
      conversationItemView = ConversationItemView_.build(context); 
     } else { // if view item was already created 
      conversationItemView = (ConversationItemView) convertView; 
     } 

     // bind data to view 
     conversationItemView.bind(getItem(position)); 

     return conversationItemView; 
    } 

    private void setConversations(List<Conversation> conversations) { 
     this.conversations = conversations; 

     // notify that data set changed so that the list is refreshed 
     notifyDataSetChanged(); 
    } 

    @Subscribe 
    public void conversationsUpdated(ConversationsUpdateEvent event) { 
     setConversations(conversationDao.getConversations()); 
    } 
} 

那麼,我在這裏錯過了什麼。爲什麼不把對話標題顯示到ListView中?

回答

1

很難一次遵守所有這些代碼。如果你問一個小問題,你會更有可能得到答案,而不是一個大問題。這是我建議你想辦法解決這個問題:

  1. 熟悉調試器。這是至關重要的,一旦你瞭解它,你將會更好地解決任何問題和編碼。

安裝斷點以在您成功檢索到數據後停止。然後確認您確實收到了數據,並且它當前存儲在您希望使用的變量中。

  1. 既然您已確認您的數據存在,請確保您正確設置了列表視圖。首先,我建議你從RecylerView開始,因爲這是更新的標準,並且在線有更多有用的教程。它們基本上是一樣的,但RecylerView更好。然後,我會繼續使用調試器來檢查我的數據是否正確傳遞到RecylerViewAdapter,並且當數據有界時,它將它指向正確的視圖。

Here is a good tutorial for using the Android studio debugger.

Here is a good tutorial by google for adding lists(Using recylerview)