以编程方式从 activity 内的可扩展列表视图适配器访问子视图项

accessing a childview item from the expandable listview adapter inside an activity programmatically

我想在可展开的 radiogroup 中添加 radiobuttons listview programmatically

我正在新 linearlayout 中添加所需的视图(即 radiogroup 等),并希望访问可扩展 listchild 项目的 linearlayout,以便添加视图添加到可扩展的 listchild 项目中。 请帮助我从可扩展的 listchild 项 XML 访问 linearlayout

这是我的PollListAdapterClass

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;
}
}

这是我的activityclass

   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" 

    />

我卡了三天了,求大神帮忙解答一下。

以编程方式在可扩展列表视图的单选组内添加单选按钮

MainActivity.java

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ExpandableListView;

    public class MainActivity extends Activity {

    ExpandableListAdapter listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;


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

        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild );

        // setting list adapter
        expListView.setAdapter(listAdapter);

    }


    /*
     * Preparing the list data
     */
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data
        listDataHeader.add("Top");
        listDataHeader.add("Now ");
        listDataHeader.add("Coming Soon..");

        // Adding child data
        List<String> top250 = new ArrayList<String>();
        top250.add("Top 1");
        top250.add("Top 2");
        top250.add("Top 3");
        top250.add("Top 4");
        top250.add("Top 5");
        top250.add("Top 6");
        top250.add("Top 7");

        List<String> nowShowing = new ArrayList<String>();
        nowShowing.add("Showing 1");
        nowShowing.add("Showing 2");
        nowShowing.add("Showing 3");
        nowShowing.add("Showing 4");
        nowShowing.add("Showing 5");
        nowShowing.add("Showing 6");

        List<String> comingSoon = new ArrayList<String>();
        comingSoon.add("comingSoon 1");
        comingSoon.add("comingSoon 2");
        comingSoon.add("comingSoon 3");
        comingSoon.add("comingSoon 4");
        comingSoon.add("comingSoon 5");

        listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
        listDataChild.put(listDataHeader.get(1), nowShowing);
        listDataChild.put(listDataHeader.get(2), comingSoon);

    }

}

ExpandableListAdapter.java

import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    HashMap<String, List<String>> _listDataChild;


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

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

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

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


            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);

            final View view = convertView;

            final RadioGroup rdGroupListChile =  (RadioGroup) convertView.findViewById(R.id.rdGroupListItem);
            TextView tvSubmit = (TextView) convertView.findViewById(R.id.tvSubmitlListItem);

            for(int i =0; i<_listDataChild.get(_listDataHeader.get(groupPosition)).size(); i++)
            {
                RadioButton radioBtn = new RadioButton(_context);
                radioBtn.setText(_listDataChild.get(_listDataHeader.get(groupPosition)).get(i));

                rdGroupListChile.addView(radioBtn);
            }



            tvSubmit.setOnClickListener(new OnClickListener() { 
                @Override
                public void onClick(View v) {

                    int radioButtonID = rdGroupListChile.getCheckedRadioButtonId();
                    RadioButton radioSexButton = (RadioButton) view.findViewById(radioButtonID);
                    if(radioSexButton!=null)
                    {
                        String radioMsg = radioSexButton.getText().toString();
                        Toast.makeText(_context, radioMsg, Toast.LENGTH_LONG).show();
                    }

                }
            });

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @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.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
        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;
    }

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.expandablelistexample.MainActivity" >

    <ExpandableListView
        android:id="@+id/lvExp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

list_group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#031E39"
    android:orientation="vertical"
    android:padding="8dp" >

    <TextView
        android:id="@+id/lblListHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:textSize="17dp" />

</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RadioGroup
        android:id="@+id/rdGroupListItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="4dp"
        android:orientation="vertical" >
    </RadioGroup>

    <TextView
        android:id="@+id/tvSubmitlListItem"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:layout_marginTop="10dp"
        android:background="#F27000"
        android:gravity="center"
        android:text="Submit"
        android:textColor="#FFFFFF"
        android:textSize="17sp" />

</LinearLayout>