如何从android中的ArrayList中检索值

时间:2012-08-20 07:58:34

标签: android json arraylist

我正在尝试从json api中检索值,然后在listView中显示。 listView包含3个元素,我也在实现onItemClickListner(),当单击Item时,它将显示与该项相关的详细视图。我使用ArrayList来存储所有json值。现在我想从该ArrayList中检索一个值,以便OnClickListner()获取该值,并从该值开始显示详细视图。

我正在使用AsyncTask来检索所有json值

@Override
protected String doInBackground(String... DATA) {
if(rqst_type.equals("top5"))
    {
        String url = DATA[1];
        JsonParser jParser = new JsonParser();
        JSONObject json = jParser.getJSONfromUrl(url);
        try
        {
            JSONArray top5 = json.getJSONArray(TAG_TOP5);
            public static ArrayList<HashMap<String, String>> top5List = new ArrayList<HashMap<String, String>>();
            top5List.clear();
            for(int i=0; i<top5.length(); i++)
            {
                Log.v(TAG_LOG, "Value of i: "+String.valueOf(i));
                JSONObject t = top5.getJSONObject(i);
                course_id = t.getString(TAG_CRSID);
                created_date = t.getString(TAG_CRTDATE);
                golfcourse_name = t.getString(TAG_GLFCRSNAME);
                facilities = t.getString(TAG_FCLTY);
                holes = t.getString(TAG_HOLES);

                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_CRSID, course_id);
                map.put(TAG_CRTDATE, created_date);
                map.put(TAG_GLFCRSNAME, golfcourse_name);
                map.put(TAG_FCLTY, facilities);
                map.put(TAG_HOLES, holes);

                top5List.add(map);
                Log.v(LoadingScreen.TAG_LOG, "top5List: "+String.valueOf(top5List));
            }
        }
        catch(JSONException e)
        {
            Log.v(TAG_LOG, String.valueOf(e));
        }
}

protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    if(rqst_type.equals("top5"))
    {
        Intent in = new Intent(context, MyTop5.class);
        in.putExtra(TAG_CRSID, course_id);
        in.putExtra(TAG_CRTDATE, created_date);
        in.putExtra(TAG_GLFCRSNAME, golfcourse_name);
        in.putExtra(TAG_FCLTY, facilities);
        in.putExtra(TAG_HOLES, holes);
        Log.v(TAG_LOG, "Valuse to MyTop5: "+course_id+" "+created_date+" "+golfcourse_name+" "+
                facilities+" "+holes);
        context.startActivity(in);
    }

这是我显示列表和onItenClickListner()的文件..

 @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_top5);

    Intent in = getIntent();
    golfcourse_name = in.getStringExtra(TAG_GLFCRSNAME);
    course_id = in.getStringExtra(TAG_CRSID);
    created_date = in.getStringExtra(TAG_CRTDATE);
    facilities = in.getStringExtra(TAG_FCLTY);
    holes = in.getStringExtra(TAG_HOLES);
    Log.v(LoadingScreen.TAG_LOG, "course id: "+String.valueOf(course_id));
    ListAdapter adapter = new SimpleAdapter(this, LoadingScreen.top5List, R.layout.top5_list,
            new String[] { TAG_GLFCRSNAME, TAG_CRSID, TAG_CRTDATE, TAG_FCLTY, TAG_HOLES },
            new int[] { R.id.top_golfname, R.id.top_courseid, R.id.top_createdate, R.id.top_fclty, R.id.top_holes });

    setListAdapter(adapter);
    Log.v(LoadingScreen.TAG_LOG, "course id: "+String.valueOf(course_id));
    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            String url = "http://mygogolfteetime.com/iphone/viewdeal/127";
            new LoadingScreen(MyTop5.this).execute("view_detail", url);
        }
    });
}

在给定的URL中,我想将127更改为存储在top5List中的值。

String url =“http://mygogolfteetime.com/iphone/viewdeal/127”;

我想在top5List中找到的值是“course_id”的值

提前致谢..

1 个答案:

答案 0 :(得分:3)

top5List.get(你的位置).get(你的钥匙); 有了它,你可以找到你想要改变的价值。