-1

Actually I've implemented listview where we can select appropriate row by tapping on togglebutton associated with it. I'm adding data which is on that row to arraylist. But, where I'm facing problem is that when i select e.g. row1 and then row2 the data which is in arraylist is like this: (row1 data,row1 data,row2 data), here row1 data is repeating.. But, I need this: (row1 data,row2 data). Even I'm trying to use clear() method also. But, when i used it, it is rither showing blank data or data last selected..

So, What I should do?

Please , help me.

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;
    }
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Mangesh
  • 11
  • 2
  • 3
    Can you post your code ? Otherwise it is highly unlikely, that anyone will help you. – slezadav Nov 07 '12 at 11:47
  • 1
    Without the actual code no one will be able to solve your issue. – Egor Nov 07 '12 at 11:47
  • k.. simply what I've to do is, adding string data to arraylist. But, it repeats element added previously e.g. data is 1,2. So, what here it is doing is it first adds 1 and then again adds 1 and then finally 2. i.e.,(1)->(1,1)->(1,1,2). But, What i want is adding data this way (1)-> (1,2) – Mangesh Nov 07 '12 at 12:03

1 Answers1

0

Since your question is not clear, Here is possible solution

I have assumed, you need to all entries in row1.

Community
  • 1
  • 1
onkar
  • 109
  • 1
  • 2
  • 11
  • kk.. simply what I've to do is, adding string data to arraylist. But, it repeats element added previously e.g. data is 1,2. So, what here it is doing is it first adds 1 and then again adds 1 and then finally 2. i.e.,(1)->(1,1)->(1,1,2). But, What i want is adding data this way (1)-> (1,2).. What should i do then?? – Mangesh Nov 07 '12 at 12:03
  • Well, you need to do slight modifications and the code will work fine for you. Just create a map and make changes(IDE will help you) – onkar Nov 07 '12 at 12:05
  • let me check.. I'll let u know? – Mangesh Nov 07 '12 at 12:31