1

以前にチェックボックスを実装したリストビューがありますが、それをラジオボタンに置​​き換えたいと思っています。以前の実装は機能しないようです。チェックボックスの実装をラジオボタンに置​​き換えるには、どのような変更が必要ですか? 以下は私の作業コードです:

ここに画像の説明を入力

ElistCBox.java

package aexp.elistcbox;

import java.util.ArrayList;

import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.RadioButton;

public class ElistCBox extends ExpandableListActivity
{
    private static final String LOG_TAG = "ElistCBox2";
    private ColorAdapter expListAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        ArrayList<String> groupNames = new ArrayList<String>();

        groupNames.add("ABC");
        groupNames.add("DEF");
        groupNames.add("GHI");


        ArrayList<ArrayList<Item>> colors = new ArrayList<ArrayList<Item>>();         
        ArrayList<Item> color = new ArrayList<Item>();              
        color = new ArrayList<Item>();
        color.add(new SectionItem("Please select any one"));
        color.add( new EntryItem( "ABC", false ) ); 
        color.add( new EntryItem( "DEF", true ) ); 
        color.add( new EntryItem( "GHI", true ));
        color.add( new EntryItem( "JKL", false ));      

        color.add(new SectionItem("Please select any one"));
        color.add( new EntryItem( "ABC", false ));
        color.add( new EntryItem( "DEF", false ));
        color.add( new EntryItem( "GHI", false ));
        color.add( new EntryItem( "JKL", false ));
        colors.add( color );

        expListAdapter = new ColorAdapter( this,groupNames, colors );
        setListAdapter( expListAdapter );


        final Button button = (Button) findViewById(R.id.submitbutton);
        button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Get data
             }
        });
    }

    public void onContentChanged  () {
        super.onContentChanged();
        Log.d( LOG_TAG, "onContentChanged" );
    }

    public boolean onChildClick(
            ExpandableListView parent, 
            View v, 
            int groupPosition,
            int childPosition,
            long id) {
        Log.d( LOG_TAG, "onChildClick: "+childPosition );
        RadioButton cb = (RadioButton)v.findViewById( R.id.radiobutton );
        if( cb != null )
            cb.toggle();
        return false;
    }
}

ColorAdapter.java

package aexp.elistcbox;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TextView;

public class ColorAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<String> groups;
    private ArrayList<ArrayList<Item>> colors;
    private LayoutInflater inflater;

    public ColorAdapter(Context context, 
                        ArrayList<String> groups,
                        ArrayList<ArrayList<Item>> colors ) { 
        this.context = context;
        this.groups = groups;
        this.colors = colors;
        inflater = LayoutInflater.from( context );
    }

    public Object getChild(int groupPosition, int childPosition) {
        return colors.get( groupPosition ).get(childPosition );
    }

    public long getChildId(int groupPosition, int childPosition) {
        return (long)( groupPosition*1024+childPosition );                          // Max 1024 children per group
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View v = convertView;

        Item c = (Item)getChild( groupPosition, childPosition );

        /*if( color != null )
            color.setText( c.getColor() );
        CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
        cb.setChecked( c.getState() );*/


        if (c != null) {
            if(c.isSection()){
                SectionItem si = (SectionItem)c;
                v = inflater.inflate(R.layout.list_item_section, parent,false);

                final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
                sectionView.setText(si.getTitle());
            } else {
                EntryItem ei = (EntryItem) c;
                v = inflater.inflate(R.layout.child_row, parent, false);

                TextView color = (TextView) v.findViewById(R.id.childname);

                if (color != null)
                    color.setText(ei.getColor());
                RadioButton cb = (RadioButton) v.findViewById(R.id.radiobutton);
                cb.setChecked(ei.getState());

            }
        }
        return v;
    }

    public int getChildrenCount(int groupPosition) {
        return colors.get( groupPosition ).size();
    }

    public Object getGroup(int groupPosition) {
        return groups.get( groupPosition );        
    }

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

    public long getGroupId(int groupPosition) {
        return (long)( groupPosition*1024 );  // To be consistent with getChildId
    } 

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View v = null;
        if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.group_row, parent, false); 
        String gt = (String)getGroup( groupPosition );
        TextView colorGroup = (TextView)v.findViewById( R.id.childname );
        if( gt != null )
            colorGroup.setText( gt );
        return v;
    }

    public boolean hasStableIds() {
        return true;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    } 

    public void onGroupCollapsed (int groupPosition) {} 
    public void onGroupExpanded(int groupPosition) {}


}

EntryItem.java

package aexp.elistcbox;

public class EntryItem implements Item {
    public String color = null;
    public boolean state = false;

    public EntryItem( String color, boolean state ) {
        this.color = color;
        this.state = state;
    }

    public String getColor() {
        return color;
    }

    public boolean getState() {
        return state;
    }

    @Override
    public boolean isSection() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isItem() {
        // TODO Auto-generated method stub
        return true;
    }

}

アイテム.java

package aexp.elistcbox;

public interface Item {

     public boolean isSection();
     public boolean isItem();
}

SectionItem.java

package aexp.elistcbox;

public class SectionItem implements Item{

    private final String title;

    public SectionItem(String title) {
        this.title = title;
    }

    public String getTitle(){
        return title;
    }

    @Override
    public boolean isSection() {
        return true;
    }

    @Override
    public boolean isItem() {
        // TODO Auto-generated method stub
        return false;
    }

}

child_row.xml

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

    <TextView android:id="@+id/childname"
         android:paddingLeft="20px"
         android:focusable="false"
         android:textSize="19dp"
         android:textStyle="italic"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="fill_parent"
        android:gravity="right">

        <RadioButton
            android:id="@+id/radiobutton"
            android:focusable="false"
            android:layout_marginRight="20dip"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />

    </LinearLayout>

</LinearLayout>

group_row.xml

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

    <TextView
        android:id="@+id/childname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dip"
        android:textSize="17dp"
        android:textColor="#f9f93d"/>

</LinearLayout>

list_item_section.xml

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

    <TextView
                android:id="@+id/list_item_section_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:gravity="center_vertical"
                android:paddingTop="10dip"
                android:paddingBottom="7dip"
                android:paddingLeft="15dip"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#737577"
                android:textSize="18sp" />

</LinearLayout>

main.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="#f4f4f4"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:paddingLeft="10dp"
        android:paddingStart="10dp"
        android:text="Please select"
        android:textColor="#000"
        android:textSize="17dp" />

    <ExpandableListView
        android:id="@+id/android:list"
        android:layout_marginTop="10dip"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:cacheColorHint="#00000000" />

    <Button
        android:id="@+id/submitbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit" />

</LinearLayout>
4

0 に答える 0