(Android需要帮助)复选框在可展开的列表视图中被自动选中,就像疯了似的 [英] (Android need help) Checkbox automaticly checked like crazy in expandable listview

查看:100
本文介绍了(Android需要帮助)复选框在可展开的列表视图中被自动选中,就像疯了似的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个可扩展的列表视图,子视图具有复选框,在展开父视图时,请选中一个复选框,不仅要选中该复选框,还要选中另一个复选框. 我不知道为什么以及如何解决这个问题. 我通过以下链接将源代码上传到mediafire.com:

I created a expandable listview, childview has checkbox, when expand parent view, check on one checkbox, not only that checkbox be checked also there are another checkboxs be checked. I dont know why and how to fix this problem. I upload sourcecode to mediafire.com in this link:

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

这是MainActivity.java

This is 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

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

推荐答案

我关注了 http://androidtrainningcenter.blogspot.in/2012/07/android-expandable-listview-simple.html

我们所需要的只是存储您复选框的选中状态.我在子视图的xml设计&中添加了一个复选框将其状态存储在NewAdapter中.这样,当父视图展开时,我们可以恢复复选框状态.

All we need is to just store your checkbox's checked status. I have added a checkbox in child view's xml design & store its status in NewAdapter., so that when parent view expands we can restore checkbox status.

在NewAdapter中完成的修改:

Modifications done in NewAdapter :

  1. 声明了一个String类型的列表,名称为selectedNames.
  2. 更改了getChildView方法的实现,如下所示.

  1. Declared a List of type String named as selectedNames.
  2. Changed getChildView method implementation as below.

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;

这篇关于(Android需要帮助)复选框在可展开的列表视图中被自动选中,就像疯了似的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆