【发布时间】:2015-11-05 07:06:01
【问题描述】:
我想在可扩展的listview programmatically 的radiogroup 中添加radiobuttons
我在一个新的linearlayout 中添加所需的视图(即radiogroup 等)并希望访问可扩展listchild 项目的linearlayout,以便将添加的视图添加到可扩展listchild 项目中.
请帮助我从可扩展的listchild 项目 XML 访问linearlayout。
这是我的PollListAdapter 班级
public class PollsListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<LinearLayout>> _listDataChild;
TextView title;
public RadioGroup rg;
Button b;
public PollsListAdapter(Context context, List<String> listDataHeader,
HashMap<String,List<LinearLayout>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// final String childText = (String) getChild(groupPosition, childPosition);
View v=convertView;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.polls_item, null);
}
title = (TextView) v.findViewById(R.id.title);
// rg=(RadioGroup)v.findViewById(R.id.rg);
// b=(Button)v.findViewById(R.id.submit);
Log.i("childview",title.getText().toString()+rg.getChildCount()+b);
// TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
// txtListChild.setText(childText);
return v;
}
@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 int getGroupCount() {
return this._listDataHeader.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 infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.polls_header, null);
}
TextView lblListHeader = (TextView) convertView.findViewById(R.id.header);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
这是我的活动课
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String,List<LinearLayout>>();
top250 = new ArrayList<LinearLayout>();
expListView = (ExpandableListView) findViewById(R.id.lvExpz);
listAdapter=new PollsListAdapter(this,null,null);
// mLinearLayout = (LinearLayout)findViewById(R.id.ll);
mLinearLayout = new LinearLayout(this);
for (int k = 1; k < a; k++) {
//create text button
TextView title = new TextView(this)
listDataHeader.add(title.getText().toString());
final RadioButton[] rb = new RadioButton[b];
RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.VERTICAL);
for (int i = 0; i < b; i++) {
rb[i] = new RadioButton(this);
rg.addView(rb[i]);
rb[i].setText("i");
rb[i].setVisibility(View.VISIBLE);
}
mLinearLayout.addView(rg);
top250.add(mLinearLayout);
listDataChild.put(listDataHeader.get(k-1),top250);
listDataChild.values();
expListView.setAdapter(new PollsListAdapter(this, listDataHeader, listDataChild));
main.xml 包含可扩展列表和
polls_item.xml 是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:layout_marginTop="10sp"
android:layout_marginBottom="10sp"
android:background="@drawable/polls_single_border"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title"
/>
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
我坚持了三天,请帮助我。
【问题讨论】:
-
据我所知,您的代码是您正在使用扩展列表视图,对于扩展列表视图,您可以为孩子和组提供不同的视图...访问此链接androidhive.info/2013/07/android-expandable-list-view-tutorial/… 你钢铁发现任何困难然后让我知道
-
是的,我知道有不同的子视图和组视图,我面临的场景是我不知道如何访问可扩展组视图中的子视图项
标签: android android-activity expandablelistview expandablelistadapter