0

我是 Android 世界的新手,我有以下情况:

我有一个 Activity main.xml RelativeLayout 和一个 ExpandableListView。

当我展开一个组时,它会向我显示孩子的内容:这是 example.xml。

现在我在 example.xml-layout 1 上有 id 为 btn_send 的按钮,我的问题是如何访问该按钮?

我尝试通过 Example.java 访问按钮,但出现错误,无法找到 btn_send。我通过 Main.java 尝试了一些错误。

先感谢您,

BR阿里

public class Main extends Activity {

ExpandableListView exv;

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

    exv = (ExpandableListView) findViewById(R.id.exv_main);
    exv.setAdapter(new MainAdapter(this));
    exv.setOnGroupExpandListener(new OnGroupExpandListener() {

        int previousGroup = -1;

        @Override
        public void onGroupExpand(int groupPosition) {
            if(groupPosition != previousGroup)
                exv.collapseGroup(previousGroup);
            previousGroup = groupPosition;
        }
    });

}
}

在 MainAdapter 类中

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

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    RelativeLayout layout = new RelativeLayout(context);
    ImageView iv = new ImageView(context);


    RelativeLayout.LayoutParams llpImage = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    layout.setGravity(Gravity.CENTER | Gravity.TOP);


        switch (groupPosition){

            case 0:

                layout = (RelativeLayout) inflater.inflate(childList[groupPosition][childPosition], null);


            break;

            case 1:

            break; 
            case 2:

                break; 
        }
        return layout;
}

和 main.xml

<ExpandableListView
    android:id="@+id/exv_main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="20dp"
    android:background="#CDCDD0"
    android:divider="@android:color/transparent"
    android:listSelector="#CDCDD0"
    android:dividerHeight="1dip"
    android:cacheColorHint="#CDCDD0"
    android:childDivider="#CDCDD0"
    >
</ExpandableListView>

并在 example.xml

<Button
    android:id="@+id/btn_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="send" />

并在 Example.java

公共类示例扩展活动{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.example);

    final Button btn = (Button) findViewById(R.id.btn_send);    

    btn.setOnClickListener(new OnClickListener() {      
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            btn.setText("huhu");
        }
    }); 
}
}
4

2 回答 2

0

您必须在 getChildView 方法中为您的按钮设置一个 onClickListener:

public class MyExpandableAdapter extends BaseExpandableListAdapter {

    private final SparseArray<Group> groups;
    public Activity activity;
    public LayoutInflater inflater;

    public MyExpandableAdapter(Activity activity, SparseArray<Group> groups) {
        this.groups = groups;
        this.activity = activity;
        this.inflater = activity.getLayoutInflater();
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean  isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
          convertView = inflater.inflate(R.layout.rowlayout_details, null);
        }
        TextView text = (TextView) convertView.findViewById(R.id.textView1);
        text.setText("content");

        Button b = (Button) convertView.findViewById(R.id.button);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // implement functionality

            }
        });

        return convertView;
    }    

}
于 2013-09-25T11:55:12.540 回答
0

@Kuba Spatny 谢谢你的帮助,我用下面的代码解决了我的问题......也许还有其他方法可以解决它,但这是我的解决方案:

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

    LayoutInflater inflater; 

    switch (groupPosition) {
    case 0:
        if (convertView == null) {
            inflater  = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(childLayout[groupPosition][childPosition], null);
            Button btn1 = (Button) convertView.findViewById(R.id.btn_send);
            btn1.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                v.getContext().startActivity(new Intent(context, ExampTest1.class));
                            }
                        });
            }



        break;
    case 1:
            inflater  = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(childLayout[groupPosition][childPosition], null);
            Button btn1 = (Button) convertView.findViewById(R.id.btn_send);
            btn1.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                v.getContext().startActivity(new Intent(context, ExampTest2.class));
                            }
                        });
        break;
    case 2:
        if (convertView == null) {
            inflater  = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(childLayout[groupPosition][childPosition], null);
            Button btn1 = (Button) convertView.findViewById(R.id.btn_send);
            btn1.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                v.getContext().startActivity(new Intent(context, ExampTest3.class));
                            }
                        });
        break;
    }

    return convertView;
}
于 2013-09-26T07:23:34.673 回答