【问题标题】:how to set the childView of the ExpandableListView in mainActivity如何在mainActivity中设置ExpandableListView的childView
【发布时间】:2018-04-25 21:18:09
【问题描述】:

我刚刚完成了我的自定义 ExpandableListView 适配器的实现, 现在在主要活动中,我不知道如何为我的 2 个自定义 ArrayLists 编写代码(一个用于 groupView,另一个用于 childView)。 ExpandableListView 的 GroupView 工作正常,但我不知道我应该如何编写代码并设置 childView 的数据。

让我们考虑一下 Group 和 Child 视图的所有数据与此完全相同:

X.add(new Word(R.drawable.ic_launcher_background,"A" , "#11111"));

所以在 ExpandableListView 的子视图和组视图的行中都有两个文本和一个 img。

-谢谢

MainActivity.java

public class MainActivity extends Activity {

    private ExpandListAdapter ExpAdapter;
    private ArrayList<Word> ExpListItems;
    private ExpandableListView ExpandList;

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

        ExpandList = (ExpandableListView) findViewById(R.id.lvExp);
        ExpAdapter = new ExpandListAdapter(MainActivity.this, ExpListItems);

        ExpListItems = new ArrayList<Word>();
        ExpListItems.add(new Word(R.drawable.ic_launcher_background,"A" , "#11111"));
        ExpListItems.add(new Word(R.drawable.ic_launcher_background,"B" , "#11111"));
        ExpListItems.add(new Word(R.drawable.ic_launcher_background,"C" , "#11111"));
        ExpListItems.add(new Word(R.drawable.ic_launcher_background,"D" , "#11111"));
        ExpListItems.add(new Word(R.drawable.ic_launcher_background,"E" , "#11111"));
        ExpListItems.add(new Word(R.drawable.ic_launcher_background,"F" , "#11111"));

        ExpandList.setAdapter(ExpAdapter);


    }

ExpandListAdapter.java(去掉不必要的代码)

    public class ExpandListAdapter extends BaseExpandableListAdapter {
    public Context context;
    public ArrayList<Word> groups ;

        public ExpandListAdapter(Context context, ArrayList<Word> groups) {
            this.context = context;
            this.groups = groups;
        }

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

        @Override
        public int getChildrenCount(int i) {
            ArrayList<Word> childList = groups.get(i).getItems();
            return childList.size();
        }



        @Override
        public Object getChild(int i, int i1) {
            ArrayList<Word> childList = groups.get(i).getItems();
            return childList.get(i1);
        }
    .
.
.


        @Override
        public long getChildId(int i, int i1) {
            return i1;
        }

        .
        .
        .
        .

        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            View listItemView = view;
            if(listItemView == null) {
                LayoutInflater inflater = (LayoutInflater) this.context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                assert inflater != null;
                listItemView = inflater.inflate(R.layout.child_row,null);
            }

            Word currentWord = groups.get(i);
            TextView nameText = (TextView) listItemView.findViewById(R.id.childNameView);
            nameText.setText(currentWord.getBakhsh());


            TextView numberText = (TextView) listItemView.findViewById(R.id.childNumView);
            numberText.setText(currentWord.getNumber());


            ImageView imageIcons = (ImageView) listItemView.findViewById(R.id.childImageView);
            if (currentWord.hasImage()) {
                imageIcons.setImageResource(currentWord.getImage());


            return listItemView;

        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return true;
        }
    }

【问题讨论】:

标签: android listview expandablelistview expandablelistadapter


【解决方案1】:

您可以将子项放入地图中,并以组中的唯一字段作为 KEY,因此可能是这样的:

onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ExpandList = (ExpandableListView) findViewById(R.id.lvExp);

    ExpListItems = new ArrayList<Word>();
    ExpListItems.add(new Word(R.drawable.ic_launcher_background, "A" , "#11111"));
    ExpListItems.add(new Word(R.drawable.ic_launcher_background, "B" , "#11111"));
    ExpListItems.add(new Word(R.drawable.ic_launcher_background, "C" , "#11111"));
    ExpListItems.add(new Word(R.drawable.ic_launcher_background, "D" , "#11111"));
    ExpListItems.add(new Word(R.drawable.ic_launcher_background, "E" , "#11111"));
    ExpListItems.add(new Word(R.drawable.ic_launcher_background, "F" , "#11111"));
    Map<String, List<Word>> mapChild = new HashMap<>();
    for(Word word: ExpListItems){
        List<Word> listChild = new ArrayList<>();
        listChild.add(new Word(R.drawable.ic_launcher_background, "Aa" , "#11111"));
        listChild.add(new Word(R.drawable.ic_launcher_background, "Bb" , "#11111"));
        listChild.add(new Word(R.drawable.ic_launcher_background, "Cc" , "#11111"));
        mapChild.put(word.getBakhsh(), listChild);
    }

    ExpAdapter = new ExpandListAdapter(MainActivity.this, ExpListItems, mapChild);
    ExpandList.setAdapter(ExpAdapter);
}

您的适配器:

public class ExpandListAdapter extends BaseExpandableListAdapter {
public Context context;
public ArrayList<Word> groups ;
public Map<String, List<Word>> childs;

public ExpandListAdapter(Context context, ArrayList<Word> groups, Map<String, List<Word>> childs) {
    this.context = context;
    this.groups = groups;
    this.childs = childs;
}

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

@Override
public int getChildrenCount(int i) {
    return childs.get(groups.get(i).getBakhsh()).size();
}

@Override
public Word getGroup(int i) {
    return groups.get(i);
}

@Override
public Word getChild(int i, int i1) {
    return childs.get(groups.get(i).getBakhsh()).get(i1);
}

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

@Override
public long getChildId(int i, int i1) {
    return i1;
}

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

@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
    View listItemView = view;
    if (listItemView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        assert inflater != null;
        listItemView = inflater.inflate(R.layout.child_row, null);
    }

    Word currentWord = getGroup(i);
    TextView nameText = (TextView) listItemView.findViewById(R.id.childNameView);
    nameText.setText(currentWord.getBakhsh());

    TextView numberText = (TextView) listItemView.findViewById(R.id.childNumView);
    numberText.setText(currentWord.getNumber());

    ImageView imageIcons = (ImageView) listItemView.findViewById(R.id.childImageView);
    if (currentWord.hasImage()) {
        imageIcons.setImageResource(currentWord.getImage());
    }
    return listItemView;
}

@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
    View listItemView = view;
    if (listItemView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        assert inflater != null;
        listItemView = inflater.inflate(R.layout.child_row, null);
    }

    Word currentWord = getChild(i,i1);
    TextView nameText = (TextView) listItemView.findViewById(R.id.childNameView);
    nameText.setText(currentWord.getBakhsh());

    TextView numberText = (TextView) listItemView.findViewById(R.id.childNumView);
    numberText.setText(currentWord.getNumber());

    ImageView imageIcons = (ImageView) listItemView.findViewById(R.id.childImageView);
    if (currentWord.hasImage()) {
        imageIcons.setImageResource(currentWord.getImage());
    }
    return listItemView;
}

@Override
public boolean isChildSelectable(int i, int i1) {
    return true;
}
}  

我在这里回答了关于 ExpandableListView 的另一个问题:Tree with checkBox,您也可以尝试该示例。希望对您有所帮助!

【讨论】:

  • 嗨,Thx,我更改了代码,但是当我单击 groupList 以展开应用程序时崩溃。
  • 它说“GetChildrenCount(int i)”返回 null 并且应用程序崩溃。 java.lang.NullPointerException:尝试在 com.example.android.expandablelistview.ExpandListAdapter.getChildrenCount(ExpandListAdapter.java:36) 处的空对象引用上调用接口方法“int java.util.List.size()”
  • 更新了答案! getChildrenCount() 和 getChild() 的变化。
  • 哦,伙计..它成功了,我不知道该怎么说谢谢你帮助我并花费你的时间..我很感激。
  • 还有一件事,现在如果我想为每个 groupItems 设置特定的 childItems 我应该改变什么?我必须使用“switch”语句而不是“for”对吗?还有一件事我不明白,“:”在 for (Word word: ExpListItems) 中是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 2015-04-18
  • 2016-07-22
  • 1970-01-01
相关资源
最近更新 更多