0

私はAndroidプログラミングの初心者です。JSON を使用して MySQL データベースからの ID 識別子をスローしようとしています。listView に対応するデータを Toast に表示するにはどうすればよいですか? 私は本当に助けが必要です:(

 // Async Task to access the web
    @SuppressLint("NewApi")
   private class JsonReadTask extends AsyncTask<String, Void, String> {
     @Override
     protected String doInBackground(String... params) {
      HttpClient httpclient = new DefaultHttpClient();
      HttpPost httppost = new HttpPost(url);
      nameValuePairs = new ArrayList<NameValuePair>(2);
      nameValuePairs.add(new BasicNameValuePair("stud_id",MainActivity.user_id)); 
    try {
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
      try {
       HttpResponse response = httpclient.execute(httppost);
       jsonResult = inputStreamToString(
       response.getEntity().getContent()).toString();
      }

      catch (ClientProtocolException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      }
      return null;
     }

     private StringBuilder inputStreamToString(InputStream is) {
      String rLine = "";
      StringBuilder answer = new StringBuilder();
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));

      try {
       while ((rLine = rd.readLine()) != null) {
        answer.append(rLine);
       }
      }

      catch (IOException e) {
      }
      return answer;
     }

     @Override
     protected void onPostExecute(String result) {
      ListDrawer();
     }
    }// end async task

    public void accessWebService() {
        try{
     JsonReadTask task = new JsonReadTask();
     // passes values for the urls string array
     task.execute(new String[] { url });
        }catch(Exception e){
         Toast.makeText(getActivity(), e.getMessage().toString() + " 3", Toast.LENGTH_LONG).show();
        }
    }

    // build hash set for list view
    public void ListDrawer() {
     List<Map<String, String>> classList = new ArrayList<Map<String, String>>();

     try {
      JSONObject jsonResponse = new JSONObject(jsonResult);
      JSONArray jsonMainNode = jsonResponse.optJSONArray("recipient");

      for (int i = 0; i < jsonMainNode.length(); i++) {
       JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
       String friend = jsonChildNode.optString("last_name") + ", " + jsonChildNode.optString("first_name");
       String outPut = friend;
       classList.add(createMsgList("recipient", outPut));
      }
     } catch (JSONException e) {
         Toast.makeText(getActivity(), e.getMessage().toString() + " 1", Toast.LENGTH_LONG).show();
    }

     try{
         SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity() , classList,
         android.R.layout.simple_list_item_1, new String[] { "recipient" }, new int[] { android.R.id.text1 });
         listView.setAdapter(simpleAdapter);

         listView.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> a, View v, int i,
                long l) {
            // TODO Auto-generated method stub

         Toast.makeText(getActivity(), listView.getId(), Toast.LENGTH_LONG).show();

        }

     });
     }catch(Exception e){
         Toast.makeText(getActivity(), e.getMessage().toString() + " 2", Toast.LENGTH_LONG).show();
     }
    }

    private HashMap<String, String> createMsgList(String name, String subject) {
     HashMap<String, String> friendList = new HashMap<String, String>();
     friendList.put(name, subject);
     return friendList;
    }


}
4

2 に答える 2

0

int ilistviewたとえば、配列から情報をコールバックするために使用できるアイテムの位置とIDです。

@Override
        public void onItemClick(AdapterView<?> a, View v, int i,
                long l) {
            // TODO Auto-generated method stub
             String me = classList.get(i);
         Toast.makeText(getActivity(), me, Toast.LENGTH_LONG).show();

        }
于 2013-11-06T15:25:49.463 に答える