可展开的ListView复选框子checkChangeListener?

时间:2016-12-28 00:58:14

标签: android sqlite checkbox expandablelistview

我想在检查每个孩子复选框时,在DatabaseHelper中启动不同方法。有MainActivity

public class MainActivity extends AppCompatActivity {
ExpListViewAdapterWithCheckbox listAdapter;
ExpandableListView expListView;
ArrayList<String> listDataHeader;
HashMap<String, List<String>> listDataChild;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    expListView = (ExpandableListView) findViewById(R.id.lvExp);
    prepareListData();
    listAdapter = new ExpListViewAdapterWithCheckbox(this, listDataHeader, listDataChild);
    expListView.setAdapter(listAdapter);
    expListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            return false;
        }
    });
    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

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

    // Listview Group collasped listener
    expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

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

        }
    });

    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {

            if(groupPosition == 1 && childPosition == 1) {

            DatabaseHelper db = new DatabaseHelper(MainActivity.this);
            db.arpa();


            }
ifif(groupPosition == 1 && childPosition == 2) {

            DatabaseHelper db = new DatabaseHelper(MainActivity.this);
            db.bugday();


            }

            return true;
        }
    });

    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            Intent i = new Intent(MainActivity.this , TumKayitlar.class);
            startActivity(i);

        }
    });

}


private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("Tahıl Ürünleri");
    listDataHeader.add("Baklagiller");
    listDataHeader.add("Sebzeler");

    // Adding child data
    List<String> tahilürünleri = new ArrayList<String>();
    tahilürünleri.add("Arpa");
    tahilürünleri.add("Buğday");
    tahilürünleri.add("Mısır");
    tahilürünleri.add("Darı");

    List<String> baklagiller = new ArrayList<String>();
    baklagiller.add("Nohut");
    baklagiller.add("Fasulye ");
    baklagiller.add("Bezelye");
    baklagiller.add("Mısır");


    List<String> sebzeler = new ArrayList<String>();
    sebzeler.add("Pırasa");
    sebzeler.add("Ispanak");
    sebzeler.add("Patlıcan");
    sebzeler.add("Domates");
    sebzeler.add("Patates");

    listDataChild.put(listDataHeader.get(0), tahilürünleri);
    listDataChild.put(listDataHeader.get(1), baklagiller);
    listDataChild.put(listDataHeader.get(2), sebzeler);
}

例如,当我检查Cbx“Arpa”时,我想要开始db.arpa(); 或者我检查了“Buğday”开始db.bugday();

Databasehelper部分:

 public void nohut() {

    nohutt = true;

}
public bugday() {
bugday = true;
}

显示数据库代码:

public class TumKayitlar extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.expandable_listview);
    DatabaseHelper dbHelper = new DatabaseHelper(this);
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    String[] getColumnName = {"_id,  student_name , student_surname "};
    Cursor imlec = db.query(" student ",getColumnName, "student_surname = 81 " , null, null,null,null);
    ListView listView = (ListView) findViewById(R.id.lvExp2);
    ArrayList<String> student = new ArrayList<String >();
    ArrayAdapter<String > adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,student);
    int student_name = imlec.getColumnIndex(" student_name ");
    int student_surname = imlec.getColumnIndex(" student_surname ");
    int _id = imlec.getColumnIndex("_id");
    String name_surname = student_name + "  " + student_surname;
    while (imlec.moveToNext()){
        name_surname =   imlec.getString(1) + "  " + imlec.getString(2) + "\n";
        student.add(name_surname);
    }
    listView.setAdapter(adapter);
    imlec.close();
    db.close();
}

}

顺便提一下ExpandableAdapter

  public class ExpListViewAdapterWithCheckbox extends BaseExpandableListAdapter {
    private Context mContext;
    private HashMap<String, List<String>> mListDataChild;
    private ArrayList<String> mListDataGroup;
    private HashMap<Integer, boolean[]> mChildCheckStates;
    private evdenevarcom.ar.evdenevar.MainActivity.ExpListViewAdapterWithCheckbox.ChildViewHolder childViewHolder;
    private evdenevarcom.ar.evdenevar.MainActivity.ExpListViewAdapterWithCheckbox.GroupViewHolder groupViewHolder;
    private String groupText;
    private String childText;
    public ExpListViewAdapterWithCheckbox(Context context, ArrayList<String> listDataGroup,          HashMap<String, List<String>> listDataChild){
        mContext = context;
        mListDataGroup = listDataGroup;
        mListDataChild = listDataChild;
        mChildCheckStates = new HashMap<Integer, boolean[]>();
    }

    public int getNumberOfCheckedItemsInGroup(int mGroupPosition)
    {
        boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
        int count = 0;
        if(getChecked != null) {
            for (int j = 0; j < getChecked.length; ++j) {
                if (getChecked[j] == true) count++;
            }
        }
        return  count;
    }

    @Override
    public int getGroupCount() {
        return mListDataGroup.size();
    }
    @Override
    public String getGroup(int groupPosition) {
        return mListDataGroup.get(groupPosition);
    }
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        groupText = getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_group, null);
            groupViewHolder = new evdenevarcom.ar.evdenevar.MainActivity.ExpListViewAdapterWithCheckbox.GroupViewHolder();
            groupViewHolder.mGroupText = (TextView) convertView.findViewById(R.id.lblListHeader);
            convertView.setTag(groupViewHolder);
        } else {

            groupViewHolder = (evdenevarcom.ar.evdenevar.MainActivity.ExpListViewAdapterWithCheckbox.GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.mGroupText.setText(groupText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mListDataChild.get(mListDataGroup.get(groupPosition)).size();
    }
    @Override
    public String getChild(int groupPosition, int childPosition) {
        return mListDataChild.get(mListDataGroup.get(groupPosition)).get(childPosition);
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final int mGroupPosition = groupPosition;
        final int mChildPosition = childPosition;

        childText = getChild(mGroupPosition, mChildPosition);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, null);
            childViewHolder = new evdenevarcom.ar.evdenevar.MainActivity.ExpListViewAdapterWithCheckbox.ChildViewHolder();
            childViewHolder.mChildText = (TextView) convertView
                    .findViewById(R.id.lblListItem);
            childViewHolder.mCheckBox = (CheckBox) convertView
                    .findViewById(R.id.lstcheckBox);
            convertView.setTag(R.layout.list_item, childViewHolder);
        } else {

            childViewHolder = (evdenevarcom.ar.evdenevar.MainActivity.ExpListViewAdapterWithCheckbox.ChildViewHolder) convertView
                    .getTag(R.layout.list_item);
        }
        childViewHolder.mChildText.setText(childText);


        childViewHolder.mCheckBox.setOnCheckedChangeListener(null);
        if (mChildCheckStates.containsKey(mGroupPosition)) {
            boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
            childViewHolder.mCheckBox.setChecked(getChecked[mChildPosition]);

        } else {
            boolean getChecked[] = new boolean[getChildrenCount(mGroupPosition)];
            mChildCheckStates.put(mGroupPosition, getChecked);
            childViewHolder.mCheckBox.setChecked(false);
        }
        childViewHolder.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

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

                    boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
                    getChecked[mChildPosition] = isChecked;
                    mChildCheckStates.put(mGroupPosition, getChecked);

                } else {
                    boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
                    getChecked[mChildPosition] = isChecked;
                    mChildCheckStates.put(mGroupPosition, getChecked);
                }

            }
        });
        return convertView;

    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
    @Override
    public boolean hasStableIds() {
        return false;
    }
    public final class GroupViewHolder {
        TextView mGroupText;
    }
    public final class ChildViewHolder {

        TextView mChildText;
        CheckBox mCheckBox;
    }
}
}

请帮帮我,我几乎不了解java。 :((

0 个答案:

没有答案