2017-12-13 10 views
0

私は各ユーザにユーザとの円があるユーザのデータベース構造を持っています。サークル名を取得し、サークル名と各ユーザーに対応するユーザーとの詳細情報を各リストビューヘッダーに追加したいとします。FirebaseデータベースからのExpanableListView

私はExpanableListViewを実装しましたが、リアルタイムデータの取得には苦労しています。私はfirebase uiアダプタは単純なリストビューでしか使えないと理解していますが、ここでは展開可能なリストビューではありません。

CustomExpandableListAdapter

public class CustomExpandableListAdapter extends BaseExpandableListAdapter { 


private Context context; 
private List<String> expandableListTitle; 
private HashMap<String, List<String>> expandableListDetail; 

public CustomExpandableListAdapter(Context context, List<String> expandableListTitle, 
            HashMap<String, List<String>> expandableListDetail) { 
    this.context = context; 
    this.expandableListTitle = expandableListTitle; 
    this.expandableListDetail = expandableListDetail; 
} 

@Override 
public Object getChild(int listPosition, int expandedListPosition) { 
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)) 
      .get(expandedListPosition); 
} 

@Override 
public long getChildId(int listPosition, int expandedListPosition) { 
    return expandedListPosition; 


} 
@Override 
public View getChildView(int listPosition, final int expandedListPosition, 
         boolean isLastChild, View convertView, ViewGroup parent) { 
    final String expandedListText = (String) getChild(listPosition, expandedListPosition); 
    if (convertView == null) { 
     LayoutInflater layoutInflater = (LayoutInflater) this.context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.list_item, null); 
    } 
    TextView expandedListTextView = (TextView) convertView 
      .findViewById(R.id.expandedListItem); 
    expandedListTextView.setText(expandedListText); 
    return convertView; 
} 

@Override 
public int getChildrenCount(int listPosition) { 
    return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)) 
      .size(); 
} 

@Override 
public Object getGroup(int listPosition) { 
    return this.expandableListTitle.get(listPosition); 
} 

@Override 
public int getGroupCount() { 
    return this.expandableListTitle.size(); 
} 

@Override 
public long getGroupId(int listPosition) { 
    return listPosition; 
} 

@Override 
public View getGroupView(int listPosition, boolean isExpanded, 
         View convertView, ViewGroup parent) { 
    String listTitle = (String) getGroup(listPosition); 
    if (convertView == null) { 
     LayoutInflater layoutInflater = (LayoutInflater) this.context. 
       getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.list_group, null); 
    } 
    TextView listTitleTextView = (TextView) convertView 
      .findViewById(R.id.listTitle); 
    listTitleTextView.setTypeface(null, Typeface.BOLD); 
    listTitleTextView.setText(listTitle); 
    return convertView; 
} 

@Override 
public boolean hasStableIds() { 
    return false; 
} 

@Override 
public boolean isChildSelectable(int listPosition, int expandedListPosition) { 
    return true; 
} 
} 

CirclesFragment

ExpandableListView expandableListView; 
ExpandableListAdapter expandableListAdapter; 
List<String> expandableListTitle; 
HashMap<String, List<String>> expandableListDetail; 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    auth = FirebaseAuth.getInstance(); 
    if (auth.getCurrentUser() == null) { 
     startActivity(new Intent(getActivity(), login.class)); 

    } else { 
     //fetch list of groups from database 
     mFirebaseDatabase = FirebaseDatabase.getInstance(); 
     myRef=mFirebaseDatabase.getReference(); 

     FirebaseUser user =auth.getCurrentUser(); 
     userID=user.getUid(); 



     expandableListView = (ExpandableListView) getView().findViewById(R.id.expandableListView); 


     expandableListDetail = getData(myRef,userID); 

     expandableListTitle = new ArrayList<String>(expandableListDetail.keySet()); 

     expandableListAdapter = new CustomExpandableListAdapter(getContext(), expandableListTitle, expandableListDetail); 
     expandableListView.setAdapter(expandableListAdapter); 
     expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 

      @Override 
      public void onGroupExpand(int groupPosition) { 
       Toast.makeText(getContext(), 
         expandableListTitle.get(groupPosition) + " List Expanded.", 
         Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { 

      @Override 
      public void onGroupCollapse(int groupPosition) { 
       Toast.makeText(getContext(), 
         expandableListTitle.get(groupPosition) + " List Collapsed.", 
         Toast.LENGTH_SHORT).show(); 

      } 
     }); 

     expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
      @Override 
      public boolean onChildClick(ExpandableListView parent, View v, 
             int groupPosition, int childPosition, long id) { 
       Toast.makeText(
         getContext(), 
         expandableListTitle.get(groupPosition) 
           + " -> " 
           + expandableListDetail.get(
           expandableListTitle.get(groupPosition)).get(
           childPosition), Toast.LENGTH_SHORT 
       ).show(); 
       return false; 
      } 
     }); 

Loadlist()メソッド

private void loadList() { 
    myRef.child("users").child(userID).child("circles").addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      lst = new ArrayList<String>(); // Result will be holded Here 
      for (DataSnapshot dsp : dataSnapshot.getChildren()) { 
       Log.d("bbb",String.valueOf(dsp.getKey())); 
       lst.add(String.valueOf(dsp.getKey())); //add result into array list 


    //need to bind the real time data in ExpandableListView somehow   


      } 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 

} 
+0

に、あなたはあなたのAdapterクラスに登録しているList<>上のデータを入れて、あなたのデータ構造化?あなたはあなたのデータベース構造を含めるべきです –

答えて

0

私は拡張可能なリストを使ったことがありませんが、AdapternotifyDataSetChanged(); のようなものを使ってListViewを更新しました。

私はお勧めし

private void loadList() { 
myRef.child("users").child(userID).child("circles").addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     for (DataSnapshot dsp : dataSnapshot.getChildren()) { 
      Log.d("bbb",String.valueOf(dsp.getKey())); 
      expandableListTitle.add(String.valueOf(dsp.getKey())); //add result into array list 
      //need to bind the real time data in ExpandableListView somehow   
     } 
     expandableListAdapter.notifyDataSetChanged(); 
    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 

    } 
}); 
} 

注:あなたがそれを作成したときにどのように

はない新しいArrayList<>

関連する問題