2015-05-04 68 views
-2

在我的android應用程序中,我收到了一個JSON的列表項名稱和一個URL,現在我需要的是如果我點擊一個列表項時,相應的URL應該是檢索並應將值傳遞給下一個Intent.This是我所嘗試過的...如何在android中選擇一個listitem中的兩個值

 AsyncHttpClient client = new AsyncHttpClient(); 
    RequestParams params = new RequestParams(); 
    progressDialog.show(); 
    params.put("videolistJSON",composeJSON()); 
    client.post("http://www.example.com/load_videos.php",params,new AsyncHttpResponseHandler() 
    { 

     public void onSuccess(String response) 
     { 

      progressDialog.hide(); 
      Gson gson = new GsonBuilder().create(); 
      try 
      { 

       JSONArray arr = new JSONArray(response); 

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

        JSONArray internalJSONArray=arr.getJSONArray(i); 

        for (int j=0;j<internalJSONArray.length();j++){ 

         videolist.add(internalJSONArray.getString(j)); 

         j++; 

         urlList.add(internalJSONArray.getString(j)); 



         } 
       load_data(); 
       } 
      } 
      catch (JSONException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

       } 

     @Override 
     public void onFailure(int statusCode, Throwable error,String content) 

     { 

      if (statusCode == 404) 

      { 
       Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show(); 
      } 

      else if (statusCode == 500) 

      { 
       Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show(); 
      } 

      else 

      { 
       Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet]", 
         Toast.LENGTH_LONG).show(); 
      } 
     } 

    }); 



} 

private String composeJSON() { 
    // TODO Auto-generated method stub 
    ArrayList<HashMap<String, String>> check_in_List; 
    check_in_List = new ArrayList<HashMap<String, String>>(); 
    HashMap<String, String> map = new HashMap<String, String>(); 

    map.put("video_id",rcvd_pos); 


    check_in_List.add(map); 
    Gson gson = new GsonBuilder().create(); 
    return gson.toJson(check_in_List); 

} 

public void load_data() 
{ 
    ArrayAdapter<String> phy = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,videolist); 

    l1.setAdapter(phy); 

    l1.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 
      // When clicked, show a toast with the TextView text 

      //String pos = (String) l1.getItemAtPosition(position); 



      //Toast.makeText(getApplicationContext(), ""+pstn, 5000).show(); 
      //Toast.makeText(getApplicationContext(), ""+urlList, 5000).show(); 

      String url = urlList.get(0).toString(); 



      Toast.makeText(getApplicationContext(), ""+url, 5000).show(); 

      } 
      /*Intent myIntent = new Intent(SubTopics.this,Functions.class); 
       myIntent.putExtra("pos", final_index); 
       startActivity(myIntent);*/ 
      } 

    }); 


} 
+0

你究竟想要做什麼? –

+0

@ AC-OpenSource我需要檢索列表項的URL時,我點擊它.. – user3805324

回答

0

試試這個。

l1.setOnItemClickListener(new OnItemClickListener() 
{ 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    { 
     String url = urlList.get(position).toString(); 
     Toast.makeText(getApplicationContext(), url, 5000).show(); 
    } 
}); 
+0

工程完美... – user3805324

相關問題