二级列表实现CheckBox单选及全选

  • Post author:
  • Post category:其他



首先,我们得有个实体类


public class PhonesInfo {


public String flag;

public String code;

public List<DataInfo> data;

public String getFlag() {


return flag;

}

public void setFlag(String flag) {


this.flag = flag;

}

public String getCode() {


return code;

}

public void setCode(String code) {


this.code = code;

}

public List<DataInfo> getData() {


return data;

}

public void setData(List<DataInfo> data) {


this.data = data;

}

public static  class DataInfo {


public String title;

public List<DatasInfo> datas;

public boolean allCheck = false;

public String getTitle() {


return title;

}

public void setTitle(String title) {


this.title = title;

}

public List<DatasInfo> getDatas() {


return datas;

}

public void setDatas(List<DatasInfo> datas) {


this.datas = datas;

}

public boolean isAllCheck() {


return allCheck;

}

public void setAllCheck(boolean allCheck) {


this.allCheck = allCheck;

}

public static class DatasInfo {


public int price;

public String type_name;

public List<String> msg;

public String add_time;

public boolean itemCheck = false;

public int getPrice() {


return price;

}

public void setPrice(int price) {


this.price = price;

}

public String getType_name() {


return type_name;

}

public void setType_name(String type_name) {


this.type_name = type_name;

}

public List<String> getMsg() {


return msg;

}

public void setMsg(List<String> msg) {


this.msg = msg;

}

public String getAdd_time() {


return add_time;

}

public void setAdd_time(String add_time) {


this.add_time = add_time;

}

public boolean isItemCheck() {


return itemCheck;

}

public void setItemCheck(boolean itemCheck) {


this.itemCheck = itemCheck;

}

}

}

}


接下来,我们的用网络请求数据,这里我们封装了个工具类


public class MyOkHttp {


public static OkHttpClient client=new OkHttpClient();

public static String get(String url,String key,String value){


FormEncodingBuilder builder=new FormEncodingBuilder();

builder.add(key,value);

Request request = new Request.Builder().url(url).post(builder.build()).build();

try {


Response response = client.newCall(request).execute();

if (response.isSuccessful()){


return response.body().string();

}

} catch (IOException e) {


e.printStackTrace();

}

return null;

}

}



接下来就是我们的主界面了,不多说,直接上代码





public class MainActivity extends AppCompatActivity {

private ExpandableListView elv;

private CheckBox cb;

private TextView tv_count;

private TextView tv_sum;

private PhonesInfo phonesInfo;

@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//找控件

initView();

//获取网络数据

getServerData();

}

/**

* 获取网络数据

*/

private void getServerData() {


String url = “http://api.ehu



版权声明:本文为Lianglei1994原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。