如何从android

时间:2015-05-04 05:51:01

标签: android listview

在我的Android应用程序中,我收到了一个列表项名称和URL的JSON,现在我需要的是如果我点击一个列表项应该检索相应的URL并且应该将值传递给下一个Intent.This is我试过的......

     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);*/
            } 

    }); 


}

1 个答案:

答案 0 :(得分: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();
    }
 });