android可扩展列表视图从firebase检索数据

2024-01-12

我该如何使用firebase检索我的数据Expandable listview. my firebase节点是这样的..

适配器类:

public class CustomExpandableListViewAdapter extends BaseExpandableListAdapter {

private Context context;
private List<String> headerItem;
private HashMap<String, List<String>> childItem;


public CustomExpandableListViewAdapter(Context context, List<String> headerItem, HashMap<String, List<String>> childItem) {
    this.context = context;
    this.headerItem = headerItem;
    this.childItem = childItem;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return childItem.get(headerItem.get(groupPosition)).get(childPosition);
}

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

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

    String childText = (String) getChild(groupPosition, childPosition);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.child_items, null);
    }
    TextView tv = convertView.findViewById(R.id.tvChildItem);
    tv.setText(childText);

    return convertView;

}

@Override
public int getChildrenCount(int groupPosition) {
    return childItem.get(headerItem.get(groupPosition)).size();
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.group_items, null);
    }
    TextView tv = convertView.findViewById(R.id.tvItemHeader);
    tv.setText(headerTitle);
    return convertView;
}

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

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

主要活动 :

public class MainActivity extends AppCompatActivity {

    ExpandableListView expandableListView;
    CustomExpandableListViewAdapter customExpandableListViewAdapter;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    FirebaseDatabase database;
    DatabaseReference myRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("Expandable ListView Data");

        expandableListView = findViewById(R.id.expLv);
        SetStandardGroups();
        customExpandableListViewAdapter = new CustomExpandableListViewAdapter(this, listDataHeader, listDataChild);
        expandableListView.setAdapter(customExpandableListViewAdapter);
    }

    public void SetStandardGroups() {

        listDataHeader = new ArrayList<>();
        listDataChild = new HashMap<>();

        myRef.addChildEventListener(new ChildEventListener() {
            int counter = 0;
            List<String> childItem = new ArrayList<>();

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

                final String headerTitle = dataSnapshot.getKey();
                listDataHeader.add(headerTitle);
                Log.e("TAG", headerTitle);

                for (DataSnapshot ds : dataSnapshot.getChildren()){
                    String child = (String) ds.getValue();
                    childItem.add(child);
                }

                listDataChild.put(listDataHeader.get(counter), childItem);
                counter++;
                Log.e("TAG", "counter :" + counter);


                customExpandableListViewAdapter.notifyDataSetChanged();
            }

            @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) {

            }
        });

    }
}

输出是这样的:Out put https://i.stack.imgur.com/cBjdK.png

我需要 Task1 第一个项目的标题标题,它包含 8 个子项目,像 2nd Task2 一样,第二个项目的标题标题,它将包含 5 个子项目 第三个任务3是第三个项目的标题,将包含10个孩子,我该如何实现,请帮助我,我是firebase新手,谢谢...


最后我发现了问题。问题是我为子对象创建一个 ArrayList 对象,它运行 3 次并将所有数据保存在一个对象中,所以我发现我需要创建 ArrayList 对象,相应地循环 Turn 的值为 3。 这是现在正在运行的代码。

myRef.addChildEventListener(new ChildEventListener() {
            int counter = 0;
            List<String> childItem;

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

                listDataHeader.add(dataSnapshot.getKey());
                Log.e("TAG", listDataHeader.get(counter));
                childItem = new ArrayList<>();

                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    String childNames = (String) ds.getValue();
                    Log.e("TAG", "childNames :" + childNames);
                    childItem.add(childNames);
                }

                listDataChild.put(listDataHeader.get(counter), childItem);
                counter++;
                Log.e("TAG", "counter :" + counter);

                customExpandableListViewAdapter.notifyDataSetChanged();
            }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android可扩展列表视图从firebase检索数据 的相关文章

随机推荐