onitemclicklistener仅显示json的最后结果

时间:2015-01-22 16:49:53

标签: java android json onitemclicklistener

class LoadProfile extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(EventCategory.this);
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(String... args) {
            // Building Parameters
            String json = null;
            try {
                List<NameValuePair> params = new ArrayList<NameValuePair>();

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(PROFILE_URL);
                httppost.setEntity(new UrlEncodedFormEntity(params));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity resEntity = response.getEntity();
                json = EntityUtils.toString(resEntity);

                Log.i("All Category: ", json.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }

            return json;
        }

        @Override
        protected void onPostExecute(String json) {
            super.onPostExecute(json);
            pDialog.dismiss();
            try{
            event_category = new JSONObject(json);
            final ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
            JSONArray user = event_category.getJSONArray("event_category");

            for (int i = 0; i < user.length(); i++) {
                JSONObject object = user.getJSONObject(i);
                category_id=object.getString("id");
                Log.i("id:", category_id);

                HashMap<String, String> map = new HashMap<String, String>();
                map.put("type", object.getString("type"));
                map.put("images", object.getString("images"));
                arraylist.add(map);    
            }

            String[] from = {"type", "images"};
            int[] to = {R.id.textView1,  R.id.iv_flag};

            ListAdapter adapters = new MyAdapter(EventCategory.this,arraylist,R.layout.event_category,from,to);
            gv1.setAdapter(adapters);

            gv1.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), category_id, Toast.LENGTH_SHORT).show();
                }
            });

            }catch(Exception e)
            {
                e.printStackTrace();
            }

        }
    }

使用onitemclicklistener单击gridview中的任何项目时,它仅显示最后一个结果(category_id)。问题是什么?

有人知道答案吗,因为我是Android新手?请帮忙。

提前致谢!

0 个答案:

没有答案