-1

实际上我已经实现了列表视图,我们可以通过点击与之关联的切换按钮来选择适当的行。我正在将该行上的数据添加到arraylist。但是,我面临的问题是,当我选择例如 row1 然后 row2 时,arraylist 中的数据是这样的:(row1 数据,row1 数据,row2 数据),这里 row1 数据重复.. 但是,我需要这:(第 1 行数据,第 2 行数据)。即使我也在尝试使用 clear() 方法。但是,当我使用它时,它会显示空白数据或上次选择的数据..

那么,我该怎么办?

请帮我。

public class ExpListActivity extends ExpandableListActivity {

static String arrGroupelements[]=new String[60];

List<String> arraylistGroupelements=new ArrayList<String>();
List<String> arraylistChildren;
List<List<String>> arraylistChildelements=new ArrayList<List<String>>();

static String arrChildelements[][];
static final int arrCheckChildToggleButtons[][]=new int[60][60];    

ArrayList<Integer> countChildren=new ArrayList<Integer>();
boolean allChildrenSelected=false,noChildrenSelected=false;  

ExpandableListView expList;
ExpAdapter adapter;
TextView txtDetails;  
ArrayList<String> details,newData;
Button btnShow;

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

    expList = getExpandableListView(); 
    adapter = new ExpAdapter(this);
    expList.setAdapter(adapter);
    newData=new ArrayList<String>();

    try {

        JSONArray categories=json.getJSONArray("Category_Display");

        for (int i = 0; i < categories.length(); i++) {

            JSONObject c=categories.getJSONObject(i);
            String strCatName=c.getString("category_name");
            arraylistGroupelements.add(strCatName);
            JSONArray subcategories=c.getJSONArray("subcategory");
            arraylistChildren=new ArrayList<String>();
            for (int j = 0; j < subcategories.length(); j++) {
                JSONObject sb=subcategories.getJSONObject(j);
                String strSbName=sb.getString("subcategory_name");
                arraylistChildren.add(strSbName);   

            }
            arraylistChildelements.add(arraylistChildren);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }       

    arrGroupelements=arraylistGroupelements.toArray(new String[arraylistGroupelements.size()]);

    final int listSize=arraylistChildelements.size();
    arrChildelements=new String[listSize][];
    for (int i = 0; i < listSize; i++) {
        List<String> sublist=arraylistChildelements.get(i);
        final int sublistSize=sublist.size();
        countChildren.add(new Integer(sublistSize));
        arrChildelements[i]=new String[sublistSize];
        for (int j = 0; j < sublistSize; j++) {
            arrChildelements[i][j]=sublist.get(j);
        }
    } 

    for (int k = 0; k < adapter.getGroupCount(); k++) {
        for (int m = 0; m < adapter.getChildrenCount(k);m++){
            arrCheckChildToggleButtons[k][m]=0;
        }

    expList.setOnGroupClickListener(new OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            return false;
        }
    });

    expList.setOnGroupExpandListener(new OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
        }
    });

    expList.setOnGroupCollapseListener(new OnGroupCollapseListener() {
        @Override
        public void onGroupCollapse(int groupPosition) {
        }
    });

    expList.setOnChildClickListener(new OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            return false;
        }
    });
}

public class ExpAdapter extends BaseExpandableListAdapter {

    private Context myContext;

    public ExpAdapter(Context context) {
        myContext = context;
        details=new ArrayList<String>();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return arrChildelements[groupPosition][childPosition];
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) myContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.child_row, null);

        TextView tvPlayerName = (TextView) convertView
                .findViewById(R.id.tvPlayerName);
        tvPlayerName
                .setText(arrChildelements[groupPosition][childPosition]);

        ToggleButton btnCToggle=(ToggleButton) convertView.findViewById(R.id.btnChildToggle);

        final int gP=groupPosition;
        final int cP=childPosition;

        btnCToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {
                    arrCheckChildToggleButtons[gP][cP]=1;
                } else {
                    arrCheckChildToggleButtons[gP][cP]=0;
                }

            }
        });

        if (arrCheckChildToggleButtons[groupPosition][childPosition]==1) {
            btnCToggle.setChecked(true);
            details=new ArrayList<String>();
            details.add((String) adapter.getChild(gP, cP));
            for (int index = 0; index < details.size(); index++) {
                Toast.makeText(myContext, index+" "+details.get(index), 8000).show();
            }
        }
        else {
            btnCToggle.setChecked(false);

        }
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return countChildren.get(groupPosition);
    }

    @Override
    public Object getGroup(int groupPosition) {
        return arrGroupelements[groupPosition];
    }

    @Override
    public int getGroupCount() {
        return arrGroupelements.length;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {   
                    LayoutInflater inflater = (LayoutInflater) myContext                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.group_row, null);

        final int gPos=groupPosition;

        TextView tvGroupName = (TextView) convertView
                .findViewById(R.id.tvGroupName);
        tvGroupName.setText(arrGroupelements[gPos]);
        ToggleButton btnGtoggle= (ToggleButton) convertView.findViewById(R.id.btnGroupToggle);

        ImageView imgHollowCircle=(ImageView) convertView.findViewById(R.id.imgHollowCircle);

        btnGtoggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {
                    for(int i=0;i<adapter.getChildrenCount(gPos);i++){
                            arrCheckChildToggleButtons[gPos][i]=1;
                            Log.d("getgroupview_checked", "if part from getgrpview");
                            Log.d("getgroupview_checked", "arrCheckChildToggleButtons["+gPos+"]"+"["+i+"]="+arrCheckChildToggleButtons[gPos][i]);
                    }
                }
                else {
                    for (int i = 0; i < adapter.getChildrenCount(gPos); i++) {
                        arrCheckChildToggleButtons[gPos][i]=0;
            }
                }

                expList.invalidateViews();
            }  
        });

        int i=0;
        for (i = 0; i < adapter.getChildrenCount(gPos); i++) {
            if (arrCheckChildToggleButtons[gPos][i]==0) {
                allChildrenSelected=false;
                break;
            }
        }

        if (i==adapter.getChildrenCount(gPos)) {
            allChildrenSelected=true;
        } 

        imgHollowCircle.setPadding(0, 0, 8, 0);

        if (allChildrenSelected) {
            btnGtoggle.setChecked(true);
            imgHollowCircle.setVisibility(View.VISIBLE);
            imgHollowCircle.setBackgroundDrawable(getResources().getDrawable(R.drawable.green_concrete_circle));

        } else {
            btnGtoggle.setChecked(false);
                imgHollowCircle.setBackgroundResource(R.drawable.green_hollow_circle);
                imgHollowCircle.setVisibility(View.VISIBLE);

        }


        return convertView;
    }
4

1 回答 1

0

由于您的问题不清楚,这是可能的解决方案

我假设,您需要 row1 中的所有条目。

于 2012-11-07T11:56:10.000 回答