如何将值从AutoCompleteTextView抛出到JsonParse?

时间:2019-03-20 13:29:07

标签: android json

我要制作 AutoCompleteTextView 。 要求用户选择 AutoCompleteTextView 来选择 分区 。 选择分区AutoCompleteTextView后,然后选择 AutoCompleteTextView村庄

我有一个Web服务-json区。

{
"kecamatan_rbg":[
    {
        "id": "3317010",
        "regency_id": "3317",
        "name": "SUMBER"
    },
    {
        "id": "3317020",
        "regency_id": "3317",
        "name": "BULU"
    }]
}

我有一个网络服务-json村。

{
"desa_kecamatan_rbg": [
    {
        "id": "3317010001",
        "district_id": "3317010",
        "name": "RONGGOMULYO"
    },
    {
        "id": "3317010002",
        "district_id": "3317010",
        "name": "LOGEDE"
    },
    {
        "id": "3317010003",
        "district_id": "3317010",
        "name": "PELEMSARI"
    }]
}

适配器:

@Override
public Filter getFilter() {
    Filter myFilter = new Filter() {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults filterResults = new FilterResults();
            JsonParseDesa jp=new JsonParseDesa();
            if (constraint != null) {
                new_suggestions =jp.getParseJsonDesa(constraint.toString());
                suggestions.clear();
                for (int i=0;i<new_suggestions.size();i++) {
                    suggestions.add(new_suggestions.get(i).getName());
                }
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
            }
            return filterResults;
        }

        @Override
        protected void publishResults(CharSequence contraint, FilterResults results) {
            if (results != null && results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    };
    return myFilter;
}

Selected District AutoCompleteTextView脚本: 从此脚本获取分区值的值

final KecamatanAdapter adapterKecamatan = new KecamatanAdapter(this, autoKecamatan.getText().toString());
    autoKecamatan.setAdapter(adapterKecamatan);
    autoKecamatan.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            JsonParseKecamatan jps=new JsonParseKecamatan();

            Kecamatan selectedSuggestGetSet = adapterKecamatan.getAllUpdatedSuggestion().get(position);
            kecamatan = selectedSuggestGetSet.getId().toString();

        }
    });

要选择AutoCompleteTextView村,请从AutoCompleteTextView地区的变量district_id中获取值。 该脚本选择AutoCompleteTextView Village:

final DesaAdapter adapterDesa = new DesaAdapter(this, autoDesa.toString());
    autoDesa.setAdapter(adapterDesa);
    autoDesa.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // TODO Auto-generated method stub
            JsonParseDesa jps=new JsonParseDesa();

            Desa selectedSuggestGetSet = adapterDesa.getAllUpdatedSuggestion().get(position);
            desa = selectedSuggestGetSet.getId().toString();

        }
    });

Json解析村庄:

public List<Desa> getParseJsonDesa(String sName)
{
    List<Desa> ListData = new ArrayList<Desa>();
    try {
        String districtId = "3317090";
        String temp = sName.replace(" ", "%20");
        URL js = new URL("http://examplewebservice.com/api/public/village/"+districtId+"/"+temp);
        URLConnection jc = js.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
        String line = reader.readLine();
        JSONObject jsonResponse = new JSONObject(line);
        JSONArray jsonArray = jsonResponse.getJSONArray("desa_kecamatan_rbg");
        for(int i = 0; i < jsonArray.length(); i++){
            JSONObject r = jsonArray.getJSONObject(i);
            ListData.add(new Desa(r.getString("id"),r.getString("district_id"),r.getString("name")));
        }
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return ListData;

}

在选择AutoCompleteTextView子区域后,如何从活动中获取 String districtId 变量的值,然后将该值扔给JsonParse?

0 个答案:

没有答案