首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >列表扩展分组采集丢失校验数据

列表扩展分组采集丢失校验数据
EN

Stack Overflow用户
提问于 2016-01-04 16:31:17
回答 1查看 63关注 0票数 0

嗨,我有一个ExpandableListView视图,这个子项有一个复选框。当我选中或取消选中子项时,它工作正常,但当我收集组时,我丢失了选中的项,并且它恢复为开始。

child.xml

代码语言:javascript
复制
<?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="50px"
         android:focusable="false"
         android:textSize="14px"
         android:textStyle="italic"
         android:layout_width="150px"
         android:layout_height="wrap_content"/>

    <TextView android:id="@+id/rgb"
         android:focusable="false"
         android:textSize="14px"
         android:textStyle="italic"
         android:layout_width="100px"
         android:layout_height="wrap_content"/>

    <CheckBox android:id="@+id/check1"
           android:focusable="false"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"/>

</LinearLayout>

group.xml

代码语言:javascript
复制
<?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="horizontal" >

    <TextView
        android:id="@+id/groupname"
        android:layout_width="150px"
        android:layout_height="25dp"
        android:paddingLeft="50px"
        android:layout_margin="5dp"
        android:textSize="20sp"
        android:textStyle="italic" />

</LinearLayout>

main.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ExpandableListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="36dp"
        android:layout_above="@+id/button1"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="show data" />

    <TextView
        android:id="@+id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="18dp"
        android:layout_above="@+id/button1"
        android:layout_alignParentLeft="true"
        android:text="@string/main_no_items" />

</RelativeLayout>

Color.java

代码语言:javascript
复制
package com.test;

public class Color {
    public String color = null;
    public String rgb = null;
    public boolean state = false;

    public Color() {

    }

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

    public String getColor() {
        return color;
    }

    public String getRgb() {
        return rgb;
    }

    public boolean getState() {
        return state;
    }
}

ColorAdabter.java

代码语言:javascript
复制
package com.test;

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.TextView;

public class ColorAdapter extends BaseExpandableListAdapter {

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

    public ColorAdapter(Context context, 
                        ArrayList<String> groups,
                        ArrayList<ArrayList<Color>> 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 = null;
        if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.child_row, parent, false); 
        Color c = (Color)getChild( groupPosition, childPosition );
        TextView color = (TextView)v.findViewById( R.id.childname );
        if( color != null )
            color.setText( c.getColor() );
        TextView rgb = (TextView)v.findViewById( R.id.rgb );
        if( rgb != null )
            rgb.setText( c.getRgb() );
        CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
        cb.setChecked( c.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.groupname );
        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) {}


}

MainActivity.java

代码语言:javascript
复制
package com.test;

import java.util.ArrayList;

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

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

    Button b;
    ArrayList<String> groupNames;
    ArrayList<ArrayList<Color>> colors;
    ArrayList<Color> color;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        b = (Button) findViewById(R.id.button1);


        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                for (int i = 0; i < groupNames.size(); i++) {
                    for (int j = 0; j < colors.get(i).size(); j++) {
                        if (colors.get(i).get(j).state == true) {
                            Toast.makeText(MainActivity.this, String.valueOf(groupNames.get(i)) + " "+  String.valueOf(j+1), Toast.LENGTH_LONG).show();
                        }
                    }
                }
            }
        });



        groupNames = new ArrayList<String>();
        groupNames.add( "grey" );
        groupNames.add( "blue" );
        groupNames.add( "yellow" );
        groupNames.add( "red" );
        colors = new ArrayList<ArrayList<Color>>(); 
        color = new ArrayList<Color>();
        color.add( new Color( "lightgrey","#D3D3D3", false ) ); 
        color.add( new Color( "dimgray","#696969", true ) ); 
        color.add( new Color( "sgi gray 92","#EAEAEA", false ) );
        colors.add( color );
        color = new ArrayList<Color>();
        color.add( new Color( "dodgerblue 2","#1C86EE",false ) );
        color.add( new Color(  "steelblue 2","#5CACEE",false ) ); 
        color.add( new Color( "powderblue","#B0E0E6", true ) );
        colors.add( color );
        color = new ArrayList<Color>();
        color.add( new Color( "yellow 1","#FFFF00",true ) );
        color.add( new Color( "gold 1","#FFD700",false ) ); 
        color.add( new Color( "darkgoldenrod 1","#FFB90F", true ) );
        colors.add( color );
        color = new ArrayList<Color>();
        color.add( new Color( "indianred 1","#FF6A6A",true ) );
        color.add( new Color( "firebrick 1","#FF3030",false ) ); 
        color.add( new Color( "maroon","#800000", false ) );
        colors.add( color );

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

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

    public boolean onChildClick(
            ExpandableListView parent, 
            View v, 
            int groupPosition,
            int childPosition,
            long id) {
        Toast.makeText(MainActivity.this, "group is: " + String.valueOf(groupPosition) +"\nchild is: " + String.valueOf(childPosition), Toast.LENGTH_LONG).show();
        Log.d( LOG_TAG, "onChildClick: "+childPosition );
        CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
        if( cb != null )
            cb.toggle();
        return false;
    }
}

当我点击按钮时,我会看到我选择的所有项目。

给你加满油。

EN

回答 1

Stack Overflow用户

发布于 2016-01-04 17:46:27

我来修好它

代码语言:javascript
复制
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {

    Toast.makeText(
            Listsefaresh_Activity.this,
            "group is: " + String.valueOf(groupPosition) + "\nchild is: "
                    + String.valueOf(childPosition), Toast.LENGTH_LONG)
            .show();
    Log.d(LOG_TAG, "onChildClick: " + childPosition);
    CheckBox cb = (CheckBox) v.findViewById(R.id.checkBoxchilditem);
    if (cb.isChecked() == true) {
        colors.get(groupPosition).get(childPosition).state = false;
    }
    else {
        colors.get(groupPosition).get(childPosition).state = true;
    }       if (cb != null)
        cb.toggle();
    return false;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34587210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档