我想获取我检查过的数据 问题是我正在使用可扩展布局 而且我不知道如何接收真实的数据信息值项 如何从模型中接收数据?
这是我的CheckedActivity
package com.example.together.Activities.GoodbyePet;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.example.together.Model.SubCategoryItem;
import com.example.together.R;
public class CheckedActivity extends AppCompatActivity {
SubCategoryItem subCategoryItem;
private TextView tvParent, tvChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checked);
tvParent = findViewById(R.id.parent);
tvChild = findViewById(R.id.child);
for (int i = 0; i < MyCategoriesExpandableListAdapter.parentItems.size(); i++ ){
String isChecked = MyCategoriesExpandableListAdapter.parentItems.get(i).get(ConstantManager.Parameter.IS_CHECKED);
if (isChecked.equalsIgnoreCase(ConstantManager.CHECK_BOX_CHECKED_TRUE))
{
tvParent.setText(tvParent.getText() + MyCategoriesExpandableListAdapter.parentItems.get(i).get(ConstantManager.Parameter.CATEGORY_NAME));
}
for (int j = 0; j < MyCategoriesExpandableListAdapter.childItems.get(i).size(); j++ ){
String isChildChecked = MyCategoriesExpandableListAdapter.childItems.get(i).get(j).get(ConstantManager.Parameter.IS_CHECKED);
if (isChildChecked.equalsIgnoreCase(ConstantManager.CHECK_BOX_CHECKED_TRUE))
{
tvChild.setText(tvChild.getText() +" , " + MyCategoriesExpandableListAdapter.parentItems.get(i).get(ConstantManager.Parameter.CATEGORY_NAME) + " "+(j+1));
}
}
}
}
}
这是我的ConstantManager
package com.example.together.Activities.GoodbyePet;
import java.util.ArrayList;
import java.util.HashMap;
public class ConstantManager {
public static final String CHECK_BOX_CHECKED_TRUE = "YES";
public static final String CHECK_BOX_CHECKED_FALSE = "NO";
public static ArrayList<ArrayList<HashMap<String, String>>> childItems = new ArrayList<>();
public static ArrayList<HashMap<String, String>> parentItems = new ArrayList<>();
public class Parameter {
public static final String IS_CHECKED = "is_checked";
public static final String SUB_CATEGORY_NAME = "sub_category_name";
public static final String SUB_CATEGORY_PRICE = "sub_category_price";
public static final String CATEGORY_NAME = "category_name";
public static final String CATEGORY_ID = "category_id";
public static final String SUB_ID = "sub_id";
public static final String SUB_CATEGORY_IMAGE = "sub_category_image";
}
}
这是我的CategoryExpandableAdapter
package com.example.together.Activities.GoodbyePet;
import android.content.Context;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.together.R;
import java.util.ArrayList;
import java.util.HashMap;
import com.example.together.Fragment.TabFragment1;
import com.squareup.picasso.Picasso;
/**
* Created by zerones on 04-Oct-17.
*/
public class MyCategoriesExpandableListAdapter extends BaseExpandableListAdapter {
public static ArrayList<ArrayList<HashMap<String, String>>> childItems;
public static ArrayList<HashMap<String, String>> parentItems;
// private final ArrayList<HashMap<String, String>> childItems;
private LayoutInflater inflater;
private FragmentActivity activity;
private HashMap<String, String> child;
private int count = 0;
private boolean isFromMyCategoriesFragment;
public MyCategoriesExpandableListAdapter(FragmentActivity activity, ArrayList<HashMap<String, String>> parentItems,
ArrayList<ArrayList<HashMap<String, String>>> childItems, boolean isFromMyCategoriesFragment) {
this.parentItems = parentItems;
this.childItems = childItems;
this.activity = activity;
this.isFromMyCategoriesFragment = isFromMyCategoriesFragment;
inflater = LayoutInflater.from(activity);
}
@Override
public int getGroupCount() {
return parentItems.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return (childItems.get(groupPosition)).size();
}
@Override
public Object getGroup(int i) {
return null;
}
@Override
public Object getChild(int i, int i1) {
return null;
}
@Override
public long getGroupId(int i) {
return 0;
}
@Override
public long getChildId(int i, int i1) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, final boolean b, View convertView, ViewGroup viewGroup) {
final ViewHolderParent viewHolderParent;
if (convertView == null) {
if(isFromMyCategoriesFragment) {
convertView = inflater.inflate(R.layout.group_list_layout_my_categories, null);
}else {
convertView = inflater.inflate(R.layout.group_list_layout_choose_categories, null);
}
viewHolderParent = new ViewHolderParent();
viewHolderParent.tvMainCategoryName = convertView.findViewById(R.id.tvMainCategoryName);
viewHolderParent.cbMainCategory = convertView.findViewById(R.id.cbMainCategory);
viewHolderParent.ivCategory = convertView.findViewById(R.id.ivCategory);
convertView.setTag(viewHolderParent);
} else {
viewHolderParent = (ViewHolderParent) convertView.getTag();
}
if (parentItems.get(groupPosition).get(ConstantManager.Parameter.IS_CHECKED).equalsIgnoreCase(ConstantManager.CHECK_BOX_CHECKED_TRUE)) {
viewHolderParent.cbMainCategory.setChecked(true);
notifyDataSetChanged();
} else {
viewHolderParent.cbMainCategory.setChecked(false);
notifyDataSetChanged();
}
viewHolderParent.cbMainCategory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (viewHolderParent.cbMainCategory.isChecked()) {
parentItems.get(groupPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_TRUE);
for (int i = 0; i < childItems.get(groupPosition).size(); i++) {
childItems.get(groupPosition).get(i).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_TRUE);
}
notifyDataSetChanged();
}
else {
parentItems.get(groupPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_FALSE);
for (int i = 0; i < childItems.get(groupPosition).size(); i++) {
childItems.get(groupPosition).get(i).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_FALSE);
}
notifyDataSetChanged();
}
}
});
ConstantManager.childItems = childItems;
ConstantManager.parentItems = parentItems;
viewHolderParent.tvMainCategoryName.setText(parentItems.get(groupPosition).get(ConstantManager.Parameter.CATEGORY_NAME));
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, final boolean b, View convertView, ViewGroup viewGroup) {
final ViewHolderChild viewHolderChild;
child = childItems.get(groupPosition).get(childPosition);
if (convertView == null) {
convertView = inflater.inflate(R.layout.child_list_layout_choose_category, null);
viewHolderChild = new ViewHolderChild();
viewHolderChild.tvSubCategoryImage = convertView.findViewById(R.id.tvSubCategoryImage);
viewHolderChild.tvSubCategoryName = convertView.findViewById(R.id.tvSubCategoryName);
viewHolderChild.cbSubCategory = convertView.findViewById(R.id.cbSubCategory);
viewHolderChild.viewDivider = convertView.findViewById(R.id.viewDivider);
viewHolderChild.tvSubCategoryPrice = convertView.findViewById(R.id.tvSubCategoryPrice);
convertView.setTag(viewHolderChild);
} else {
viewHolderChild = (ViewHolderChild) convertView.getTag();
}
if (childItems.get(groupPosition).get(childPosition).get(ConstantManager.Parameter.IS_CHECKED).equalsIgnoreCase(ConstantManager.CHECK_BOX_CHECKED_TRUE)) {
viewHolderChild.cbSubCategory.setChecked(true);
notifyDataSetChanged();
} else {
viewHolderChild.cbSubCategory.setChecked(false);
notifyDataSetChanged();
}
viewHolderChild.tvSubCategoryName.setText(child.get(ConstantManager.Parameter.SUB_CATEGORY_NAME));
viewHolderChild.tvSubCategoryPrice.setText(child.get(ConstantManager.Parameter.SUB_CATEGORY_PRICE));
Picasso.get().load(child.get(ConstantManager.Parameter.SUB_CATEGORY_IMAGE)).fit().into(viewHolderChild.tvSubCategoryImage);
// Picasso.get().load()
// viewHolderChild.value.setText(child.get());
viewHolderChild.cbSubCategory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (viewHolderChild.cbSubCategory.isChecked()) {
count = 0;
childItems.get(groupPosition).get(childPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_TRUE);
notifyDataSetChanged();
} else {
count = 0;
childItems.get(groupPosition).get(childPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_FALSE);
notifyDataSetChanged();
}
for (int i = 0; i < childItems.get(groupPosition).size(); i++) {
if (childItems.get(groupPosition).get(i).get(ConstantManager.Parameter.IS_CHECKED).equalsIgnoreCase(ConstantManager.CHECK_BOX_CHECKED_TRUE)) {
count++;
}
}
if (count == childItems.get(groupPosition).size()) {
parentItems.get(groupPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_TRUE);
notifyDataSetChanged();
} else {
parentItems.get(groupPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_FALSE);
notifyDataSetChanged();
}
ConstantManager.childItems = childItems;
ConstantManager.parentItems = parentItems;
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
private class ViewHolderParent {
TextView tvMainCategoryName;
CheckBox cbMainCategory;
ImageView ivCategory;
}
private class ViewHolderChild {
ImageView tvSubCategoryImage;
TextView tvSubCategoryName;
CheckBox cbSubCategory;
View viewDivider;
TextView tvSubCategoryPrice;
}
}
**这是我的数据值,包括soruce TabFragment1 **
@Override
protected JSONObject doInBackground(Void... voids) {
jobj = new JSONObject();
try {
//서버 연결
Url = new URL(strUrl); // URL화 한다.
HttpURLConnection conn = (HttpURLConnection) Url.openConnection(); // URL을 연결한 객체 생성.
conn.setRequestMethod("POST"); // post방식 통신
conn.setDoOutput(true); // 쓰기모드 지정
conn.setDoInput(true); // 읽기모드 지정
conn.setRequestProperty("Content-Type", "application/json; utf-8");
conn.setRequestProperty("Accept", "application/json; utf-8");
conn.connect();
//데이터 전달 하는곳
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(etp_cd);
wr.flush();
wr.close(); //전달후 닫아준다.
// 데이터 받아오는 곳
InputStream is = null; //input스트림 개방
BufferedReader reader = null;
is = conn.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); //문자열 셋 세팅
StringBuffer rbuffer = new StringBuffer(); //문자열을 담기 위한 객체
String line = null;
rbuffer.append(reader.readLine());
jobj = new JSONObject(rbuffer.toString().trim());
is.close();
// result = rbuffer.toString().trim();
conn.disconnect();
Log.e("result", jobj+"");
} catch (MalformedURLException | ProtocolException exception) {
exception.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
}catch (JSONException e) {
e.printStackTrace();
}
return jobj;
}
@Override
protected void onPostExecute(JSONObject aVoid) {
super.onPostExecute(aVoid);
// Log.e("넘어왓는지 확인", jobj + "");
//
JSONObject jsonObject = null;
try {
JSONArray jsonArray = (JSONArray) jobj.get("result");
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
if(i==0) {
String pd_nm;
String pd_img;
String pd_content;
String pd_price;
JSONArray sarray = (JSONArray) jsonObject.get("수의");
for (int s=0; s<sarray.length(); s++) {
JSONObject sjobj = sarray.getJSONObject(s);
pd_nm = sjobj.getString("etp_pd_nm");
pd_img = sjobj.getString("etp_img_path");
pd_content = sjobj.getString("etp_pd_content");
pd_price = sjobj.getString("etp_pd_price");
Log.e("수의 imageURL",pd_img);
f_s.add(new SubCategoryItem(pd_nm,pd_img,pd_content,pd_price)); //array 배열에 넣음
}
} else if(i==1){
String pd_nm;
String pd_img;
String pd_content;
String pd_price;
JSONArray sarray = (JSONArray) jsonObject.get("함");
// Log.e("sarray",sarray+"");
for (int s=0; s<sarray.length(); s++) {
JSONObject sjobj = sarray.getJSONObject(s);
pd_nm = sjobj.getString("etp_pd_nm");
pd_img = sjobj.getString("etp_img_path");
pd_content = sjobj.getString("etp_pd_content");
pd_price = sjobj.getString("etp_pd_price");
// Log.e("수의",pd_nm + "//" + pd_price);
f_h.add(new SubCategoryItem(pd_nm,pd_img,pd_content,pd_price)); //함 배열 add
}
} else if(i==2){
String pd_nm;
String pd_img;
String pd_content;
String pd_price;
JSONArray sarray = (JSONArray) jsonObject.get("관");
for (int s=0; s<sarray.length(); s++) {
JSONObject sjobj = sarray.getJSONObject(s);
pd_nm = sjobj.getString("etp_pd_nm");
pd_img = sjobj.getString("etp_img_path");
pd_content = sjobj.getString("etp_pd_content");
pd_price = sjobj.getString("etp_pd_price");
f_g.add(new SubCategoryItem(pd_nm,pd_img,pd_content,pd_price)); //관 배열 add
}
} else {
String pd_nm;
String pd_img;
String pd_content;
String pd_price;
JSONArray farray = (JSONArray) jsonObject.get("화장");
for (int f=0; f<farray.length(); f++) {
JSONObject sjobj = farray.getJSONObject(f);
pd_nm = sjobj.getString("etp_pd_nm");
pd_img = sjobj.getString("etp_img_path");
pd_content = sjobj.getString("etp_pd_content");
pd_price = sjobj.getString("etp_pd_price");
// Log.e("수의",pd_nm + "//" + pd_price);
//해당 모델 객체에 add하기
f_f.add(new SubCategoryItem(pd_nm,pd_img,pd_content,pd_price));
}
}
}
setupReferences(view,f_s,f_h,f_g,f_f);
}catch (JSONException e) {
e.printStackTrace();
}
}
}.execute();
btn = view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), GoodbyepetReservationResultActivity.class);
startActivity(intent);
}
});
return view;
}
private void setupReferences(View view, ArrayList<SubCategoryItem> f_s, ArrayList<SubCategoryItem> f_h, ArrayList<SubCategoryItem> f_g, ArrayList<SubCategoryItem> f_f) {
lvCategory = view.findViewById(R.id.lvCategory);
arCategory = new ArrayList<>(); // 큰 상품들의 array 배열
arSubCategory = new ArrayList<>();
parentItems = new ArrayList<>();
childItems = new ArrayList<>();
Log.e("넘어온 이미지",f_s.get(0).getPd_img()+"");
// ArrayList<SubCategoryItem> list = suit;
// 1
DataItem dataItem = new DataItem();
dataItem.setCategoryId("1");
dataItem.setCategoryName("수의");
// arSubCategory = new ArrayList<>();
for (int i = 0; i <f_s.size(); i++) {
SubCategoryItem subCategoryItem = new SubCategoryItem();
subCategoryItem.setCategoryId(String.valueOf(i));
subCategoryItem.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_FALSE);
subCategoryItem.setSubCategoryName(f_s.get(i).getPd_nm());
subCategoryItem.setPd_price(f_s.get(i).getPd_price());
subCategoryItem.setPd_img(f_s.get(i).getPd_img());
arSubCategory.add(subCategoryItem);
}
dataItem.setSubCategory(arSubCategory);
arCategory.add(dataItem);
// 2
dataItem = new DataItem();
dataItem.setCategoryId("2");
dataItem.setCategoryName("함");
arSubCategory = new ArrayList<>();
for (int j = 0; j < f_h.size(); j++) {
SubCategoryItem subCategoryItem = new SubCategoryItem();
subCategoryItem.setCategoryId(String.valueOf(j));
subCategoryItem.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_FALSE);
subCategoryItem.setSubCategoryName(f_h.get(j).getPd_nm());
subCategoryItem.setPd_price(f_h.get(j).getPd_price());
subCategoryItem.setPd_img(f_h.get(j).getPd_img());
arSubCategory.add(subCategoryItem);
}
dataItem.setSubCategory(arSubCategory);
arCategory.add(dataItem);
// 3
dataItem = new DataItem();
dataItem.setCategoryId("3");
dataItem.setCategoryName("관");
arSubCategory = new ArrayList<>();
for (int k = 0; k < f_g.size(); k++) {
SubCategoryItem subCategoryItem = new SubCategoryItem();
subCategoryItem.setCategoryId(String.valueOf(k));
subCategoryItem.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_FALSE);
subCategoryItem.setSubCategoryName(f_g.get(k).getPd_nm());
subCategoryItem.setPd_price(f_g.get(k).getPd_price());
subCategoryItem.setPd_img(f_g.get(k).getPd_img());
arSubCategory.add(subCategoryItem);
}
dataItem.setSubCategory(arSubCategory);
arCategory.add(dataItem);
//4
dataItem = new DataItem();
dataItem.setCategoryId("4");
dataItem.setCategoryName("화장비");
arSubCategory = new ArrayList<>();
for (int o = 0; o < f_f.size(); o++) {
SubCategoryItem subCategoryItem = new SubCategoryItem();
subCategoryItem.setCategoryId(String.valueOf(o));
subCategoryItem.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_FALSE);
subCategoryItem.setSubCategoryName(f_f.get(o).getPd_nm());
subCategoryItem.setPd_img(f_f.get(o).getPd_img());
subCategoryItem.setPd_price(f_f.get(o).getPd_price());
arSubCategory.add(subCategoryItem);
}
dataItem.setSubCategory(arSubCategory);
arCategory.add(dataItem);
Log.d("TAG", "setupReferences: " + arCategory.size());
for (DataItem data : arCategory) {
// Log.i("Item id",item.id);
ArrayList<HashMap<String, String>> childArrayList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> mapParent = new HashMap<String, String>();
mapParent.put(ConstantManager.Parameter.CATEGORY_ID, data.getCategoryId());
mapParent.put(ConstantManager.Parameter.CATEGORY_NAME, data.getCategoryName());
int countIsChecked = 0;
for (SubCategoryItem subCategoryItem : data.getSubCategory()) {
HashMap<String, String> mapChild = new HashMap<String, String>();
mapChild.put(ConstantManager.Parameter.SUB_ID, subCategoryItem.getSubId());
mapChild.put(ConstantManager.Parameter.SUB_CATEGORY_NAME, subCategoryItem.getSubCategoryName());
mapChild.put(ConstantManager.Parameter.SUB_CATEGORY_PRICE, subCategoryItem.getPd_price());
mapChild.put(ConstantManager.Parameter.SUB_CATEGORY_IMAGE, subCategoryItem.getPd_img());
mapChild.put(ConstantManager.Parameter.CATEGORY_ID, subCategoryItem.getCategoryId());
mapChild.put(ConstantManager.Parameter.IS_CHECKED, subCategoryItem.getIsChecked());
if (subCategoryItem.getIsChecked()
.equalsIgnoreCase(ConstantManager.CHECK_BOX_CHECKED_TRUE)) {
countIsChecked++;
}
childArrayList.add(mapChild);
}
if (countIsChecked == data.getSubCategory().size()) {
data.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_TRUE);
} else {
data.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_FALSE);
}
mapParent.put(ConstantManager.Parameter.IS_CHECKED, data.getIsChecked());
childItems.add(childArrayList);
parentItems.add(mapParent);
}
ConstantManager.parentItems = parentItems;
ConstantManager.childItems = childItems;
myCategoriesExpandableListAdapter = new MyCategoriesExpandableListAdapter(getActivity(), parentItems, childItems, false);
lvCategory.setAdapter(myCategoriesExpandableListAdapter);
}
我想要的结果是 将我仅检查过的类别的数据发送给孩子