0

我有一组 json 数据。如何将数据填充到 Spinner 并显示 hyname 字符并获取 id 的值?

这是我的java代码:

[{"id":"32","hyname":"Construction & Remodel"},{"id":"15","hyname":"machine manufacturing"},{"id":"16","hyname":"E-communication"},{"id":"21","hyname":"Measurement monitoring"},{"id":"28","hyname":"Laboratory testing"},{"id":"19","hyname":"geological exploration "},{"id":"23","hyname":"Agriculture & Farming "},{"id":"30","hyname":"Mechanical cold"},{"id":"31","hyname":"Mechanical hot work"},{"id":"22","hyname":"Material storage"},{"id":"24","hyname":"Food Service"}]
4

2 回答 2

1

使用 Collection 设置 ID 和 HeName。

class YourCollection{
  private String id;
  private String hename;

  void setHename(String hename){
  this.hename = hename;
  }

  String getHename(){
   return hename;
   }

  void setId(String id){
  this.id= id;
  }
   String getId(){
   return id;
   }

}

// 异步任务

@Override
    protected List<YourCollection> doInBackground(Void... params) {

        JSONObject obj = null;
        List<YourCollection> result = new ArrayList<YourCollection>();

        try{

            InputStream ips = activity.getAssets().open("your.json");
            BufferedReader buffer = new BufferedReader(new InputStreamReader(ips));

            StringBuilder string = new StringBuilder();
            String line = "";

            while((line = buffer.readLine()) != null){
                string.append(line);
            }

            obj = new JSONObject(string.toString());

        }
        catch(Exception e){

        }

            if(obj != null){
                JSONArray array = obj.optJSONArray("data");       
                for (int i=0; i<array.length(); i++){
                    JSONObject rss = array.optJSONObject(i);
                    YourCollection m = new YourCollection ();
                    m.setHename(rss.optString("hename")); m.setId(rss.optString("id")); 
                    result.add(m);
                }
            }

        return result;
    }

并在您的适配器创建函数以返回您的列表,并使用

    OnItemSelectedListener mwe = new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
List<YourCollection> list = YourAdapter.getList(); // your function to get List
YourCollection c = list.get(arg2);
String id = c.getId();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    };
于 2013-02-17T09:54:19.307 回答
1

试试这个

String jsontext=[{"id":"32","hyname":"Construction & Remodel"},...];

ArrayList<String> list=new ArrayList<String>();

Hashtable<String,String> hnameids=new Hashtable<String,String>();

JsonArray array=new JsonArray(jsontext);

for(int i=0;i<array.length;i++)
{

JsonObject obj=array.getJsonObject(i);

list.add(obj.getString("hyname"));

hnameids.put(obj.getString("hyname"),obj.getString("id"));

}
ArrayAdapter adapter=new ArrayAdapter(context,android.R.layout.simple_list_item1,list);
spinner.setAdapter(adapter);

spinner.OnItemSelectedListener (new OnItemClickListener() {

    public void onItemClick(AdapterView<?> adapter, View view, int pos,
            long id) {

        String data= spinner.getSelectedItem();
              String id=hnameids.get(data);
    }

});
于 2013-02-17T10:02:19.520 回答