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;
   }

}

// AsyncTask

@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 に答える