jeudi 6 septembre 2018

Android ExpandableListView with custom adapter of checkboxes gets checked on its own

I recently started developing several android apps and in an app that I begun developing this week, I needed retrieve a JSONObject from a web service and filter it according to user parameters, To achieve that I created an activity with an expandable listview that contains checkboxes. When clicked, it saves the checkbox contents to an array and filters it accordingly. And the filtering works perfectly fine. However, there is something bizarre about the checkboxes, when I check one from the bottom of one of the drawers, one from the top of the same drawer and several from other drawers gets checked as well. However, it does not register them as clicks and doesn't add the to the filter parameters array, ıt only adds the one that we originally checked. As far as I can see, there is no pattern between those checked automatically. There is the code of the list adapter. CustomExpandableListAdapter.java

package com.divasoft.retivaraporlar;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> expandableListTitle;
    private HashMap<String, List<String>> expandableListDetail;
    static public HashMap<String, ArrayList<String>> filterData = new HashMap<>();
    public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
                                       HashMap<String, List<String>> expandableListDetail) {
        this.context = context;
        this.expandableListTitle = expandableListTitle;
        this.expandableListDetail = expandableListDetail;
        filterData.put("Cari Açıklama", new ArrayList<String>());
        filterData.put("Döviz", new ArrayList<String>());
        filterData.put("Borç", new ArrayList<String>());
        filterData.put("Alacak", new ArrayList<String>());
        filterData.put("Bakiye", new ArrayList<String>());
    }

    @Override
    public Object getChild(int listPosition, int expandedListPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .get(expandedListPosition);
    }

    @Override
    public long getChildId(int listPosition, int expandedListPosition) {
        return expandedListPosition;
    }

    @Override
    public View getChildView(final int listPosition, final int expandedListPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        final String expandedListText = (String) getChild(listPosition, expandedListPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_item, null);
        }
        CheckBox expandedListViewCheckBox = (CheckBox) convertView
                .findViewById(R.id.expandedListItem);
        expandedListViewCheckBox.setText(expandedListText);
        expandedListViewCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                if(isChecked) {
                    if(expandableListTitle.get(listPosition).equals("Bakiye")){
                        switch(buttonView.getText().toString()) {
                            case "Borçlu":
                                filterData.get(expandableListTitle.get(listPosition)).add("DEBT");
                                break;
                            case "Alacak":
                                filterData.get(expandableListTitle.get(listPosition)).add("OWE");
                                break;
                            default:
                                filterData.get(expandableListTitle.get(listPosition)).add("ZERO");
                                break;
                        }
                    }else {
                        filterData.get(expandableListTitle.get(listPosition)).add(buttonView.getText().toString());
                    }
                }else{
                    filterData.get(expandableListTitle.get(listPosition)).remove(buttonView.getText().toString());
                }

            }
        }
    );
        return convertView;
    }

    @Override
    public int getChildrenCount(int listPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .size();
    }

    @Override
    public Object getGroup(int listPosition) {
        return this.expandableListTitle.get(listPosition);
    }

    @Override
    public int getGroupCount() {
        return this.expandableListTitle.size();
    }

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

    @Override
    public View getGroupView(int listPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String listTitle = (String) getGroup(listPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_group, null);
        }
        TextView listTitleTextView = (TextView) convertView
                .findViewById(R.id.listTitle);
        listTitleTextView.setTypeface(null, Typeface.BOLD);
        listTitleTextView.setText(listTitle);
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int listPosition, int expandedListPosition) {
        return true;
    }
}

and there is the code of the activity:

filter_popup.java

package com.divasoft.retivaraporlar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class filter_popup extends Activity {
    public View filter;
    ExpandableListView expandableListView;
    ExpandableListAdapter expandableListAdapter;
    List<String> expandableListTitle;
    HashMap<String, List<String>> expandableListDetail;

    public String TAG = "Filter";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter_popup);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        expandableListDetail = getData();
        expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
        expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
        expandableListView.setAdapter(expandableListAdapter);
        filter = findViewById(R.id.filter);
        filter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
               Intent myIntent = new Intent(filter_popup.this, cariBakiye.class);
               myIntent.putExtra("filters", CustomExpandableListAdapter.filterData); //Optional parameters
               filter_popup.this.startActivity(myIntent);
               finish();
               cariBakiye.getInstance().finish();
            }
        });
        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        expandableListTitle.get(groupPosition) + " List Expanded.",
                        Toast.LENGTH_SHORT).show();
            }
        });

        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        expandableListTitle.get(groupPosition) + " List Collapsed.",
                        Toast.LENGTH_SHORT).show();

            }
        });

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                Toast.makeText(
                        getApplicationContext(),
                        expandableListTitle.get(groupPosition)
                                + " -> "
                                + expandableListDetail.get(
                                expandableListTitle.get(groupPosition)).get(
                                childPosition), Toast.LENGTH_SHORT
                ).show();
                return false;
            }
        });
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        getWindow().setLayout((int)(width*0.8), (int)(height*0.65));


    }

    public static HashMap<String, List<String>> getData() {
        HashMap<String, List<String>> expandableListDetail = new HashMap<String, List<String>>();
        try {
            List<String> cariAciklama = new ArrayList<>();
            for (int i = 0; i<((JSONArray)cariBakiye.getCariData().get("DATA")).length(); i++){
                if(cariAciklama.indexOf(((JSONObject)((JSONArray)cariBakiye.getCariData().get("DATA")).get(i)).get("current_description").toString())==-1) {
                    cariAciklama.add(((JSONObject)((JSONArray)cariBakiye.getCariData().get("DATA")).get(i)).get("current_description").toString());
                }
            }

            List<String> doviz = new ArrayList<>();
            for (int i = 0; i<((JSONArray)cariBakiye.getCariData().get("DATA")).length(); i++){
                if(doviz.indexOf(((JSONObject)((JSONArray)cariBakiye.getCariData().get("DATA")).get(i)).get("currency_code").toString())==-1) {
                    doviz.add(((JSONObject) ((JSONArray) cariBakiye.getCariData().get("DATA")).get(i)).get("currency_code").toString());
                }
            }

            List<String> alacak = new ArrayList<>();
            for (int i = 0; i<((JSONArray)cariBakiye.getCariData().get("DATA")).length(); i++){
                if(alacak.indexOf(((JSONObject)((JSONArray)cariBakiye.getCariData().get("DATA")).get(i)).get("total_owe").toString())==-1) {
                    alacak.add(((JSONObject) ((JSONArray) cariBakiye.getCariData().get("DATA")).get(i)).get("total_owe").toString());
                }
            }

            List<String> borc = new ArrayList<>();
            for (int i = 0; i<((JSONArray)cariBakiye.getCariData().get("DATA")).length(); i++){
                if(borc.indexOf(((JSONObject)((JSONArray)cariBakiye.getCariData().get("DATA")).get(i)).get("total_debt").toString())==-1) {
                    borc.add(((JSONObject) ((JSONArray) cariBakiye.getCariData().get("DATA")).get(i)).get("total_debt").toString());
                }
            }

            List<String> bakiye = new ArrayList<>();
            for (int i = 0; i<((JSONArray)cariBakiye.getCariData().get("DATA")).length(); i++){
                switch(((JSONObject) ((JSONArray) cariBakiye.getCariData().get("DATA")).get(i)).get("balance_type").toString()) {
                    case "DEBT":
                        if(bakiye.indexOf("Borçlu")==-1){
                            bakiye.add("Borçlu");
                        }
                        break;
                    case "OWE":
                        if(bakiye.indexOf("Alacak")==-1){
                            bakiye.add("Alacak");
                        }
                        break;
                    default:
                        if(bakiye.indexOf("Sıfır")==-1){
                            bakiye.add("Sıfır");
                        }
                        break;

                }
            }

            expandableListDetail.put("Cari Açıklama", cariAciklama);
            expandableListDetail.put("Bakiye", bakiye);
            expandableListDetail.put("Döviz", doviz);
            expandableListDetail.put("Borç", borc);
            expandableListDetail.put("Alacak", alacak);
        }catch(JSONException e){Log.e("FilterPopup","[ERROR]: "+e.toString());}
        return expandableListDetail;
    }
}




Aucun commentaire:

Enregistrer un commentaire