android - (Android需要帮助)复选框在可扩展 ListView 中自动疯狂检查

标签 android listview expandable

我创建了一个可展开的 ListView , subview 有复选框,当展开父 View 时,选中一个复选框,不仅会选中该复选框,还会选中另一个复选框。 我不知道为什么以及如何解决这个问题。 我在此链接中将源代码上传到 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;
}

最佳答案

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

我们所需要的只是存储您的复选框的选中状态。我在 subview 的 xml 设计中添加了一个复选框并将其状态存储在 NewAdapter 中,以便当父 View 展开时我们可以恢复复选框状态。

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

关于android - (Android需要帮助)复选框在可扩展 ListView 中自动疯狂检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13855349/

相关文章:

android - 可扩展面板布局

javascript - 可扩展广告的 iframe Busters 如何运作?

android - 在 Kotlin 中构建 GridView 时无法传递上下文

android - 100% 宽度的表格在 Gmail Android 中不起作用

android - 测试时不会出现用于保存凭据的 Google Smart Lock 对话框

android - 如何在 Android 中使用 CheckBox 跟踪 ListView 中选中的行

Android google maps APIs获取路线编程

vb.net - ListView 工具提示闪烁

android - 如何在未显示上下文菜单时停止将长按作为短按处理

swift - 有没有办法将长文本放入可扩展单元格的表格 View 单元格中? (以编程方式)