列表视图只显示在第二个活动的最后一行

时间:2014-03-15 23:10:08

标签: android json android-intent android-listview

我使用json解析器创建android应用程序以从web获取数据并将其显示在listview中。 在用户选择一行之后,系统使用意图在第二个活动中显示细节。

问题在于无论用户选择什么系统都会显示列表视图中的最后一行。

任何人都可以帮我解决这个问题吗?

enter image description here

here i did not chose the last row

JsonActivityHttpClient

package com.devleb.jsonparsingactivitydemo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.app.ListActivity;
import android.content.Intent;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class JsonActivityHttpClient extends ListActivity {

    // ***from JsonHandlerClass****//
    JSONArray PostalCodes;
    List<String> result;

    JSONObject jsonObject;
    JSONObject postalCode;

    private static final String PLACE_NAME_TAG = "placeName";
    private static final String LONGITUDE_TAG = "lng";
    private static final String LATITUDE_TAG = "lat";
    // private static final String ADMIN_NAME_TAG = "adminCode3";
    private static final String POSTAL_CODE_TAG = "postalcode";
    private static final String POSTALCODE = "postalcodes";

    // ***from JsonHandlerClass****//

    JSONParserHandler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new HTTPGetTask().execute();

    }

    private class HTTPGetTask extends AsyncTask<Void, Void, List<String>> {

        private static final String USER_NAME = "devleb";

        private static final String URL = "http://api.geonames.org/postalCodeLookupJSON?postalcode=6600&country=AT&username="
                + USER_NAME;

        AndroidHttpClient mClient = AndroidHttpClient.newInstance("");

        @Override
        protected List<String> doInBackground(Void... arg0) {
            // TODO Auto-generated method stub

            HttpGet request = new HttpGet(URL);

            JSONParserHandler responseHandler = new JSONParserHandler();
            try {

                return mClient.execute(request, responseHandler);

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;

        }

        @Override
        protected void onPostExecute(List<String> result) {
            // TODO Auto-generated method stub

            if (null != mClient) {

                mClient.close();

                setListAdapter(new ArrayAdapter<String>(
                        JsonActivityHttpClient.this, R.layout.list_item, result));

            }

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.json_activity_http_client, menu);
        return true;
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub


        try {

            //String item = (String)l.getItemAtPosition(position);


        //  PostalCodes.get(position);


            String  PLACENAME = postalCode.get(PLACE_NAME_TAG).toString();
            String  LATITUDE = postalCode.get(LATITUDE_TAG).toString();
            String  LONGITUDE = postalCode.get(LONGITUDE_TAG).toString();
            String  POSTALCODE = postalCode.get(POSTAL_CODE_TAG).toString();



  Log.e("position of the Item in the list", "you select the item number " + PostalCodes.getString(position));

            Intent in = new Intent(getBaseContext(), Row_Item.class);
            in.putExtra("placename", PLACENAME);
            in.putExtra("lat",  LATITUDE);
            in.putExtra("lng", LONGITUDE);
            in.putExtra("postaCode", POSTALCODE);
            startActivity(in);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            Log.e("Error in the onListItemClick",
                    "LOOK AT THE onListItemClick method");
        }

    }

    // *************JSONParserHandler*******************************//

    private class JSONParserHandler implements ResponseHandler<List<String>> {
        /*
         * private static final String PLACE_NAME_TAG = "placeName"; private
         * static final String LONGITUDE_TAG = "lng"; private static final
         * String LATITUDE_TAG = "lat"; private static final String
         * ADMIN_NAME_TAG = "adminCode3"; private static final String
         * POSTAL_CODE_TAG = "postalcode"; private static final String
         * POSTALCODE = "postalcodes";
         */
        @Override
        public List<String> handleResponse(HttpResponse response)
                throws ClientProtocolException, IOException {
            // TODO Auto-generated method stub

            result = new ArrayList<String>();
            String JSONResponse = new BasicResponseHandler()
                    .handleResponse(response);

            try {

                jsonObject = (JSONObject) new JSONTokener(JSONResponse)
                        .nextValue();

                PostalCodes = jsonObject.getJSONArray(POSTALCODE);

                for (int i = 0; i < PostalCodes.length(); i++) {
                 postalCode = (JSONObject) PostalCodes.get(i);

                    result.add(postalCode.get(PLACE_NAME_TAG) + "\n"
                            + POSTAL_CODE_TAG + ":"
                            + postalCode.get(POSTAL_CODE_TAG) + LATITUDE_TAG
                            + ":" + postalCode.get(LATITUDE_TAG) + "\n"
                            + LONGITUDE_TAG + ":"
                            + postalCode.get(LONGITUDE_TAG));
                }
            } catch (JSONException E) {
                E.printStackTrace();
            }

            return result;
        }
    }

    // *************JSONParserHandler*******************************//

}

我认为问题出在这个方法

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub


        try {

            //String item = (String)l.getItemAtPosition(position);


        //  PostalCodes.get(position);


            String  PLACENAME = postalCode.get(PLACE_NAME_TAG).toString();
            String  LATITUDE = postalCode.get(LATITUDE_TAG).toString();
            String  LONGITUDE = postalCode.get(LONGITUDE_TAG).toString();
            String  POSTALCODE = postalCode.get(POSTAL_CODE_TAG).toString();



  Log.e("position of the Item in the list", "you select the item number " + PostalCodes.getString(position));

            Intent in = new Intent(getBaseContext(), Row_Item.class);
            in.putExtra("placename", PLACENAME);
            in.putExtra("lat",  LATITUDE);
            in.putExtra("lng", LONGITUDE);
            in.putExtra("postaCode", POSTALCODE);
            startActivity(in);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            Log.e("Error in the onListItemClick",
                    "LOOK AT THE onListItemClick method");
        }

Row_Item

package com.devleb.jsonparsingactivitydemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class Row_Item extends Activity {

    TextView Place_Name, txtlat, txtlng, txtPostalCode;

    String value1, value2, value3, value4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_row__item);

        Place_Name = (TextView) findViewById(R.id.textView1);
        txtlat = (TextView) findViewById(R.id.textLAT);
        txtlng = (TextView) findViewById(R.id.textLNG);
        txtPostalCode = (TextView) findViewById(R.id.textPSTC);

        Intent i = getIntent();
        if (null != i) {
            value1 = i.getStringExtra("placename");
            value2 = i.getStringExtra("lat");
            value3 = i.getStringExtra("lng");
            value4 = i.getStringExtra("postaCode");
        }
        Place_Name.setText(value1);
        txtlat.setText(value2);
        txtlng.setText(value3);
        txtPostalCode.setText(value4);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.row__item, menu);
        return true;
    }

}

1 个答案:

答案 0 :(得分:1)

我看到的问题是你没有指定选择哪个对象,所以它显然选择了最后一个。在第二次看,当你解析json文档时,你留下了postalCode变量中的最后一个对象(方法handleRespone())。这就是原因。你可以做的是重新分析指定的邮政编码或将所有邮政编码保存在一个数组中,并在OnListItemClick()方法中使用它们。