2

展開可能なリストビューを作成しました。子ビューにはチェックボックスがあり、親ビューを展開するときに、1 つのチェックボックスをオンにします。そのチェックボックスだけでなく、別のチェックボックスもオンにします。この問題を解決する理由と方法がわかりません。ソースコードを次のリンクで mediafire.com にアップロードします。

http://www.mediafire.com/?eih80athr56ejg2

これは MainActivity.java です

public class MainActivity extends Activity {

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

private void SetListView(){
    ExpandableListView lv=(ExpandableListView)findViewById(R.id.listview);
    ExpAdapter adapter=new ExpAdapter(this);
    lv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

これはExpAdapter.javaです

public class ExpAdapter extends BaseExpandableListAdapter{
Context mContext;
int[][] data= new int[4][20];
public ExpAdapter(Context c){
    mContext=c;
}
@Override
public Object getChild(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getChildId(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean arg2, View convertView,
        ViewGroup parent) {
    // TODO Auto-generated method stub
    if(convertView==null){
         LayoutInflater inflater =  (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         convertView = inflater.inflate(R.layout.childview,parent, false);
    }
    TextView tv=(TextView)convertView.findViewById(R.id.txtChild);
    tv.setText("Child Position="+childPosition);
    return convertView;
}

@Override
public int getChildrenCount(int arg0) {
    // TODO Auto-generated method stub
    return data[arg0].length;
}

@Override
public Object getGroup(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return data.length;
}

@Override
public long getGroupId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getGroupView(int groupPosition, boolean arg1, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if(convertView==null){
         LayoutInflater inflater =  (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         convertView = inflater.inflate(R.layout.groupview,parent, false);
    }
    TextView tv=(TextView)convertView.findViewById(R.id.txtGroup);
    tv.setText("Group Position="+groupPosition);
    return convertView;
}

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

@Override
public boolean isChildSelectable(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return false;
}
4

1 に答える 1

2

http://androidtrainningcenter.blogspot.in/2012/07/android-expandable-listview-simple.htmlをフォローしました

必要なのは、チェックボックスのチェック状態を保存することだけです。子ビューのxmlデザインにチェックボックスを追加し、そのステータスをNewAdapterに保存して、親ビューが展開されたときにチェックボックスのステータスを復元できるようにしました。

NewAdapter で行われた変更:

  1. selectedNames という名前の文字列型のリストを宣言しました。
  2. getChildView メソッドの実装を以下のように変更しました。

    tempChild = (ArrayList<String>) Childtem.get(groupPosition);
    TextView text = null;
    
    if (convertView == null) {
        convertView = minflater.inflate(R.layout.childrow, null);
    }
    
    text = (TextView) convertView.findViewById(R.id.textView1);
    text.setText(tempChild.get(childPosition));
    
    cb = (CheckBox) convertView.findViewById(R.id.cb_item);
    
    cb.setOnCheckedChangeListener(null);
    
            cb.setChecked(false);
    
    if (selectedNames.size() > 0
            && selectedNames.indexOf(tempChild.get(childPosition)) != -1) {
    
        cb.setChecked(true);
        // Log.d("Present",tempChild.get(childPosition));
    
    } 
    
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (isChecked) {
    
                int i = selectedNames.indexOf(tempChild.get(childPosition));
    
                if (i == -1) {
                    selectedNames.add(tempChild.get(childPosition));
                }
            } else {
    
                int i = selectedNames.indexOf(tempChild.get(childPosition));
    
                if (i != -1) {
                    selectedNames.remove(i);
                }
    
            }
    
        }
    });
    
    return convertView;
    
于 2013-04-16T10:58:10.907 に答える