gpt4 book ai didi

android - ListView 只显示在第二个 Activity 的最后一行

转载 作者:行者123 更新时间:2023-11-30 03:01:37 25 4
gpt4 key购买 nike

我创建了一个使用 json 解析器从 web 获取数据并将其显示在 ListView 中的 android 应用程序。用户选择一行后,系统会使用 Intent 在第二个 Activity 中显示详细信息。

问题是无论用户选择什么,系统都会在 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");
}

行_项目

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;
}

}

最佳答案

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

关于android - ListView 只显示在第二个 Activity 的最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22430923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com