加载json数据第一次使用适合所选微调器选项的数据

时间:2016-01-28 06:26:45

标签: android json arraylist hashmap android-spinner

我在片段开始时加载json数据

查看我的网络服务

    {
Data: [
{
id: "1",
Name: "Shirt/Tees",
Dry_cleaning: "49",
Wash_Iron: "19",
Wash: "15",
Iron: "8"
},
{
id: "2",
Name: "Kurta(short)",
Dry_cleaning: "49",
Wash_Iron: "19",
Wash: "20",
Iron: "5"
},
{
id: "3",
Name: "Kurta(Long)",
Dry_cleaning: "50",
Wash_Iron: "20",
Wash: "15",
Iron: "8"
},
{
id: "4",
Name: "Shorts",
Dry_cleaning: "50",
Wash_Iron: "19",
Wash: "15",
Iron: "5"
},
{
id: "5",
Name: "Trousers/Pyjamas",
Dry_cleaning: "55",
Wash_Iron: "30",
Wash: "20",
Iron: "8"
},
{
id: "6",
Name: "Jeans",
Dry_cleaning: "70",
Wash_Iron: "29",
Wash: "20",
Iron: "7"
},
{
id: "7",
Name: "Blazer",
Dry_cleaning: "150",
Wash_Iron: "19",
Wash: "25",
Iron: "5"
},
{
id: "8",
Name: "Leather_Jacket",
Dry_cleaning: "100",
Wash_Iron: "36",
Wash: "30",
Iron: "6"
},
{
id: "9",
Name: "Lungi/Dhoti(Long)",
Dry_cleaning: "100",
Wash_Iron: "15",
Wash: "12",
Iron: "13"
},
{
id: "10",
Name: "Waist_coat",
Dry_cleaning: "120",
Wash_Iron: "30",
Wash: "40",
Iron: "12"
},
{
id: "11",
Name: "Muffler/Cap/Gloves",
Dry_cleaning: "50",
Wash_Iron: "35",
Wash: "30",
Iron: "20"
},
{
id: "14",
Name: "Bathrobe",
Dry_cleaning: null,
Wash_Iron: "15",
Wash: "5",
Iron: "10"
},
{
id: "16",
Name: "Sherwani",
Dry_cleaning: "100",
Wash_Iron: "50",
Wash: "30",
Iron: "10"
}
],
message: "Values"
}

我想要的是当我选择微调器选项DryClean,Wash,Wash& Iron和iron时不要调用AsyncTask只显示listview中的ArrayList

enter image description here 请有人帮我这个

这是我的代码

ManFragment.java

public class ManFragment extends Fragment {

private View rootView;
//Dry
public static String TAG_ITEM_MAN = "Name";
public static String TAG_PRICE_DRY = "Dry_cleaning";
public static String TAG_PRICE_WASH_IRON = "Wash_Iron";
public static String TAG_PRICE_WASH = "Wash";
public static String TAG_PRICE_IRON = "Iron";
public static String Name, Price_Dry, price_wash_iron, price_wash, price_iron;

static TextView ItemName, ItemPrice;
public static int Cat_Position;
ArrayList<HashMap<String, String>> DryList = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> WashList = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> WashIronList = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> IronList = new ArrayList<HashMap<String, String>>();


ListView list;
ProgressDialog pdialog;

static Spinner Spin_Man;

public ManFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    rootView = inflater.inflate(R.layout.fragment_one, container, false);
    // Locate the listview in listview_main.xml
    list = (ListView) rootView.findViewById(R.id.man_listView);
    Spin_Man = (Spinner) rootView.findViewById(R.id.spinner_man);


    new DryData().execute();

    Spin_Man.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {

            try {


                switch (position) {
                    case 0:

                        Cat_Position=position;
                        getView();

                        break;
                    case 1:

                        Cat_Position=position;
                        getView();

                        break;
                    case 2:

                        Cat_Position=position;
                        getView();

                        break;
                    case 3:

                        Cat_Position=position;
                        getView();

                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            Toast.makeText(getActivity(), "Position " + position, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

    });

    new DryData().execute();

    return rootView;
}


private class DryData extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pdialog = new ProgressDialog(getActivity());
        pdialog.setMessage("Please Wait...");
        pdialog.setCancelable(true);
        pdialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        JSONParser jp = new JSONParser();
        String jsonStr = jp.makeServiceCall(url_dry, JSONParser.GET);
        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {
            try {
                JSONObject jobj = new JSONObject(jsonStr);
                JSONArray jarray = jobj.getJSONArray("Data");

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject jobjin = jarray.getJSONObject(i);

                    String name = jobjin.getString(TAG_ITEM_MAN);
                    String price_dry = jobjin.getString(TAG_PRICE_DRY);
                    String price_wash_iron = jobjin.getString(TAG_PRICE_WASH_IRON);
                    String price_wash = jobjin.getString(TAG_PRICE_WASH);
                    String price_iron = jobjin.getString(TAG_PRICE_IRON);

                    HashMap<String, String> add_dry = new HashMap<String, String>();
                    add_dry.put(TAG_ITEM_MAN, name);
                    add_dry.put(TAG_PRICE_DRY, price_dry);
                    HashMap<String, String> add_wash_iron = new HashMap<String, String>();
                    add_wash_iron.put(TAG_ITEM_MAN, name);
                    add_wash_iron.put(TAG_PRICE_WASH_IRON, price_wash_iron);
                    HashMap<String, String> add_wash = new HashMap<String, String>();
                    add_wash_iron.put(TAG_ITEM_MAN, name);
                    add_wash.put(TAG_PRICE_WASH, price_wash);
                    HashMap<String, String> add_iron = new HashMap<String, String>();
                    add_iron.put(TAG_PRICE_IRON, price_iron);

                    DryList.add(add_dry);
                    WashList.add(add_wash);
                    WashIronList.add(add_wash_iron);
                    IronList.add(add_iron);
                }
            } catch (JSONException e) {
                e.printStackTrace();
                // TODO: handle exception
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url_dry");

        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if (pdialog.isShowing()) {
            pdialog.dismiss();
        }

        Filllist();
    }

}

public void Filllist() {
    // TODO Auto-generated method stub
    Cus adapter = new Cus();
    list.setAdapter(adapter);

}

private class Cus extends BaseAdapter {

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return DryList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.price_row, null);
        }
        ItemName = (TextView) convertView.findViewById(R.id.item_name);
        ItemPrice = (TextView) convertView.findViewById(R.id.price);

        Name = DryList.get(position).get(TAG_ITEM_MAN).toString();
        Price_Dry = DryList.get(position).get(TAG_PRICE_DRY).toString();
        price_wash_iron = WashIronList.get(position).get(TAG_PRICE_WASH_IRON).toString();
        price_wash = WashList.get(position).get(TAG_PRICE_WASH).toString();
        price_iron = IronList.get(position).get(TAG_PRICE_IRON).toString();


        if (Cat_Position == 0) {
            //set item name
            ItemName.setText("" + Name);
            ItemPrice.setText("" + Price_Dry);
        } else if (Cat_Position == 1) {
            //set item name
            ItemName.setText("" + Name);
            ItemPrice.setText("" + price_wash_iron);
        } else if (Cat_Position == 2) {
            //set item name
            ItemName.setText("" + Name);
            ItemPrice.setText("" + price_wash);
        }

        return convertView;
    }

}

}

1 个答案:

答案 0 :(得分:0)

我发现解决方案只是放了Filllist();在每种情况下都会看到我修改后的代码

<强> ManFragment.java

public class ManFragment extends Fragment {

private View rootView;
//Dry
public static String TAG_ITEM_MAN = "Name";
public static String TAG_PRICE_DRY = "Dry_cleaning";
public static String TAG_PRICE_WASH_IRON = "Wash_Iron";
public static String TAG_PRICE_WASH = "Wash";
public static String TAG_PRICE_IRON = "Iron";
public static String Name, Price_Dry, price_wash_iron, price_wash, price_iron;
public static String url_dry = "http://openspace.tranetech.com/mis/Laundry/men_dry.php";
static TextView ItemName, ItemPrice;
public static int Cat_Position;
ArrayList<HashMap<String, String>> DryList = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> WashList = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> WashIronList = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> IronList = new ArrayList<HashMap<String, String>>();


ListView list;
ProgressDialog pdialog;

static Spinner Spin_Man;

public ManFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    rootView = inflater.inflate(R.layout.fragment_one, container, false);
    // Locate the listview in listview_main.xml
    list = (ListView) rootView.findViewById(R.id.man_listView);
    Spin_Man = (Spinner) rootView.findViewById(R.id.spinner_man);


    new DryData().execute();

    Spin_Man.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {

            try {


                switch (position) {
                    case 0:

                        Cat_Position=position;
                        Filllist();
                        getView();

                        break;
                    case 1:

                        Cat_Position=position;
                        Filllist();
                        getView();

                        break;
                    case 2:

                        Cat_Position=position;
                        Filllist();
                        getView();

                        break;
                    case 3:

                        Cat_Position=position;
                        Filllist();
                        getView();

                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            Toast.makeText(getActivity(), "Position " + position, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

    });

    new DryData().execute();

    return rootView;
}


private class DryData extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pdialog = new ProgressDialog(getActivity());
        pdialog.setMessage("Please Wait...");
        pdialog.setCancelable(true);
        pdialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        JSONParser jp = new JSONParser();
        String jsonStr = jp.makeServiceCall(url_dry, JSONParser.GET);
        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {
            try {
                JSONObject jobj = new JSONObject(jsonStr);
                JSONArray jarray = jobj.getJSONArray("Data");

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject jobjin = jarray.getJSONObject(i);

                    String name = jobjin.getString(TAG_ITEM_MAN);
                    String price_dry = jobjin.getString(TAG_PRICE_DRY);
                    String price_wash_iron = jobjin.getString(TAG_PRICE_WASH_IRON);
                    String price_wash = jobjin.getString(TAG_PRICE_WASH);
                    String price_iron = jobjin.getString(TAG_PRICE_IRON);

                    HashMap<String, String> add_dry = new HashMap<String, String>();
                    add_dry.put(TAG_ITEM_MAN, name);
                    add_dry.put(TAG_PRICE_DRY, price_dry);
                    HashMap<String, String> add_wash_iron = new HashMap<String, String>();
                    add_wash_iron.put(TAG_ITEM_MAN, name);
                    add_wash_iron.put(TAG_PRICE_WASH_IRON, price_wash_iron);
                    HashMap<String, String> add_wash = new HashMap<String, String>();
                    add_wash_iron.put(TAG_ITEM_MAN, name);
                    add_wash.put(TAG_PRICE_WASH, price_wash);
                    HashMap<String, String> add_iron = new HashMap<String, String>();
                    add_iron.put(TAG_PRICE_IRON, price_iron);

                    DryList.add(add_dry);
                    WashList.add(add_wash);
                    WashIronList.add(add_wash_iron);
                    IronList.add(add_iron);
                }
            } catch (JSONException e) {
                e.printStackTrace();
                // TODO: handle exception
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url_dry");

        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if (pdialog.isShowing()) {
            pdialog.dismiss();
        }

        Filllist();
    }

}

public void Filllist() {
    // TODO Auto-generated method stub
    Cus adapter = new Cus();
    list.setAdapter(adapter);

}

private class Cus extends BaseAdapter {

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return DryList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.price_row, null);
        }
        ItemName = (TextView) convertView.findViewById(R.id.item_name);
        ItemPrice = (TextView) convertView.findViewById(R.id.price);

        Name = DryList.get(position).get(TAG_ITEM_MAN).toString();
        Price_Dry = DryList.get(position).get(TAG_PRICE_DRY).toString();
        price_wash_iron = WashIronList.get(position).get(TAG_PRICE_WASH_IRON).toString();
        price_wash = WashList.get(position).get(TAG_PRICE_WASH).toString();
        price_iron = IronList.get(position).get(TAG_PRICE_IRON).toString();




        if (Cat_Position == 0) {
            //set item name
            ItemName.setText("" + Name);
            ItemPrice.setText("" + Price_Dry);
        } else if (Cat_Position == 1) {
            //set item name
            ItemName.setText("" + Name);
            ItemPrice.setText("" + price_wash_iron);
        } else if (Cat_Position == 2) {
            //set item name
            ItemName.setText("" + Name);
            ItemPrice.setText("" + price_wash);
        }else if (Cat_Position == 3) {
            //set item name
            ItemName.setText("" + Name);
            ItemPrice.setText("" + price_wash_iron);
        }

        return convertView;
    }

}

}