2017-02-20 119 views
0

我正在尝试在我的应用中使用展开式列表。我正在关注这些 steps,但我陷入了如何从Firebase检索数据以将它们添加为我的代码的Dummydata。将数据从Firebase加载到ExpandableList

这里是主要活动

public class MainActivity extends AppCompatActivity { 

ExpandableListAdapter myAdapter; 
ExpandableListView myList; 
List<String> listDataHeader; 
HashMap<String,List<String>> listDataChild; 

private FirebaseDatabase mFirebaseDatabase; 
private DatabaseReference mDatabaseReference; 
private ChildEventListener mChildEventListener; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // get the listview 
    myList=(ExpandableListView)findViewById(R.id.Explist); 
    prepareListData(); 
    myAdapter= new ExpandableListAdapter(this,listDataHeader,listDataChild); 
    myList.setAdapter(myAdapter); 

} 
private void prepareListData(){ 
    mFirebaseDatabase= FirebaseDatabase.getInstance(); 
    mDatabaseReference = mFirebaseDatabase.getReference().child("category"); 
    //Read Data from Database 
    mChildEventListener = new ChildEventListener() { 
     @Override 
     public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
      listDataHeader = new ArrayList<String>(); 
      listDataChild = new HashMap<String, List<String>>(); 
      listDataHeader=dataSnapshot.getValue(); 
      /* 
      * how to load data from firebase to the expandable 
      * 
      * 
      * */ 

     } 

     @Override 
     public void onChildChanged(DataSnapshot dataSnapshot, String s) { 

     } 

     @Override 
     public void onChildRemoved(DataSnapshot dataSnapshot) { 

     } 

     @Override 
     public void onChildMoved(DataSnapshot dataSnapshot, String s) { 

     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }; 
    mDatabaseReference.addChildEventListener(mChildEventListener); 

} 

这里是适配器

public class ExpandableListAdapter extends BaseExpandableListAdapter { 
private Context _context; 
private List<String> _listDataHeader; // header titles 
// child data in format of header title, child title 
private HashMap<String, List<String>> _listDataChild; 

public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listDataChild) { 
    this._context = context; 
    this._listDataHeader = listDataHeader; 
    this._listDataChild = listDataChild; 

} 

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

@Override 
public int getChildrenCount(int groupPosition) { 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size(); 
} 

@Override 
public Object getGroup(int groupPosition) { 
    return this._listDataHeader.get(groupPosition); 
} 

@Override 
public Object getChild(int groupPosition, int childPosition) { 
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition) ; 
} 

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

@Override 
public long getChildId(int groupPosition, int childPosition) { 
    return childPosition; 
} 

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

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    String headerTitle = (String) getGroup(groupPosition); 
    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_gruop, null); 
    } 
    TextView lblListHeader = (TextView) convertView 
      .findViewById(R.id.lblListHeader); 
    lblListHeader.setTypeface(null, Typeface.BOLD); 
    lblListHeader.setText(headerTitle); 

    return convertView; 
} 

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    final String childText = (String) getChild(groupPosition, childPosition); 
    if(convertView == null){ 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_item, null); 
    } 
    TextView txtListChild = (TextView) convertView 
      .findViewById(R.id.lblListItem); 

    txtListChild.setText(childText); 
    return convertView;} 


@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    return true; 
} 

任何帮助表示赞赏代码。

在适配器

回答

0

Add方法用于更新列表,在列表中

 public class ExpandableListAdapter extends BaseExpandableListAdapter 
     { 
      private Context _context; 
      private List<String> _listDataHeader; // header titles 
      // child data in format of header title, child title 
      private HashMap<String, List<String>> _listDataChild; 

      public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listDataChild) { 
       this._context = context; 
       this._listDataHeader = listDataHeader; 
       this._listDataChild = listDataChild; 

      } 

    public void addItemToList(String newItem){ 
     this._listDataChild.plus(newItem); 
     notifyDataSetChanged(); 
    } 

    public void setList(List<String> newList){ 
     this._listDataChild = listDataChild; 
     notifyDataSetChanged(); 

    } 
      ... 

添加项目,并在活动类使用这些功能,, (其在科特林..它转换成Java在AS)

/* 
      * how to load data from firebase to the expandable 
      * 
      * 
      * */ 
      if (dataSnapshot != null) { 
        val newList = arrayListOf<String>() 

        for (listDataSnap in dataSnapshot.getChildren()) { 
         val newItem: String? = listDataSnap.getValue() 
         if (newItem != null) { 
           newList.add(newItem) 
         } 
        } 

        myAdapter?.setList(newList) 

       } 

*重点是明白!干杯

+0

谢谢你..但我不明白这一点,你指的是newItem的“DataListHeader”和newList的“DataListChild”我是对吗? @SijanGurung – labon

+0

是的,你是对的! –

+0

对不起,但我必须定义头字符串和孩子的字符串列表,而不是我定义它上面的权利? @SijanGurung – labon

相关问题