在main.xml中添加动态ExpandableListView

时间:2011-04-28 17:14:13

标签: android expandablelistview expandablelistadapter

是否有一个网站可以找到BaseExpandableListAdapter的内部工作原理?我读了API,但我仍然不明白它是如何遍历为提供视图而提供的整个数组。我自己的实现有问题。我不能在不使用ExpandableListActivity的情况下创建可扩展列表的完整列表,即使两者都相同。它应该从数据库中检索字符串并从每个字符串中创建一个expandablelistview,并将所有创建的可扩展列表添加到main.xml内的linearlayout。会发生什么是只显示第一个字符串的expandablelistview。这是片段

主类:

public class MainActivity extends Activity implements OnClickListener {
DBAdapter groupTable = new DBAdapter(this);
ExpandableListView groupLabel;
GroupAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

       groupTable.open();
    adapter = new GroupAdapter(groupTable.getAllGroups());
    retrieveExpandables(adapter);
       groupTable.close();
    Button addGroupButton = (Button)findViewById(R.id.addGroup);
    addGroupButton.setOnClickListener(this);
}

public void retrieveExpandables(GroupAdapter adapter) { 
    LinearLayout layout = (LinearLayout)findViewById(R.id.grouplist);
    LinearLayout groupLayout =    
             (LinearLayout)getLayoutInflater().inflate(R.layout.grouplistview, null);

    ExpandableListView groupExpandableList = 
             (ExpandableListView)groupLayout.findViewById(R.id.groupLabel);
    groupExpandableList.setAdapter(adapter);

    layout.addView(groupLayout);
}

BaseExpandableListAdapter类:

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    if (convertView instanceof ViewGroup)
        return (ViewGroup) convertView;
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);
    ViewGroup item = (ViewGroup)inflater.inflate(R.layout.grouplisttext, null);
    TextView groupLabel = (TextView)item.findViewById(R.id.groupLabel);
    groupLabel.setText(groups[groupPosition].name);
    groupLabel.setVisibility(View.VISIBLE);
    Log.d("A1", "This part repeating");
    return item;
}

TextView的XML文件,它将是可扩展列表标题

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:id="@+id/groups"
          android:layout_height="wrap_content">

    <TextView android:id="@+id/groupLabel"
              android:textSize="24sp"
              android:text="asdf"
              android:textStyle="bold"
              android:layout_width="fill_parent"
              android:layout_weight="1"
              android:layout_height="40dip"
              android:layout_marginLeft="45dip"
              android:gravity="center_vertical" />

 </LinearLayout>

ExpandableListView

的XML文件
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:id="@+id/groups"
          android:layout_height="wrap_content">

    <ExpandableListView android:id="@+id/groupLabel"
              android:textSize="24sp"
              android:textStyle="bold"
              android:layout_width="fill_parent"
              android:layout_weight="1"
              android:layout_height="40dip" />

 </LinearLayout>

很抱歉,如果我发布了太多代码,我尝试删除尽可能多的不必要信息。

1 个答案:

答案 0 :(得分:0)

我发现问题出了问题。显然我得到了LinearLayout并且main.xml中的ScrollView互换了,并且getViewGroup类的方法是错误的。现在一切都好了