我是android和stackoverflow上的新手,所以如果我在问题上犯了一些错误,请指导我。我的问题是,当我单击列表视图中的项目时,每个项目在下一个活动中都显示相同的内容。我有很多项目在列表视图中,我想使用列表中单击项的意图在下一个活动上发送poId,但是当我单击列表中的任何项时,poId是相同的(最后一次是我有3个项目,然后在每个项目中获得第三个项目的poId listview)上的每个项目。请告诉我如何获取可点击的poId并使用该位置的意图发送下一个活动,我的代码在这里,也请指导我并提供帮助:
lstPo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(POListActivity.this, PODetailsActivity.class);
intent.putExtra("poId",poId);
startActivity(intent);
}
});
这是我的asynctask,我从中获得poId
private class GetPoList extends AsyncTask<String,String,String>{
protected void onPreExecute() {
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected String doInBackground(String... strings) {
ContentValues param= new ContentValues();
param.put("buyerId", buyerId);
JSONObject rootObj = parser.makeHttpRequest(Constants.URL_GET_PO_LIST, "GET", param);
try {
if (rootObj != null) {
String status = rootObj.getString(Constants.SvcStatus);
Log.d("Status",status);
if (Constants.Success.equals(status)) {
JSONObject jsonObject = new JSONObject(String.valueOf(rootObj));
Log.d("PO_List ", jsonObject.toString());
JSONArray jsonArray = jsonObject.getJSONArray("lstPO");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
poId = object.getLong("poId");
poNo = object.getString("poNo");
deliveryDt = object.getString("deliveryDt");
metersCompleted = object.getInt("metersCompleted");
totalMeters = object.getInt("totalMeters");
comments = object.getString("comments");
hideStatus = object.getInt("hideStatus");
poListData = new PO(poNo, deliveryDt, metersCompleted, totalMeters, comments,hideStatus);
poListAdapter.add(poListData);
}
} else {
Toast.makeText(POListActivity.this, "not Success", Toast.LENGTH_SHORT).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
catch(Exception e)
{
}
return null;
}
protected void onPostExecute(String string){
super.onPostExecute(string);
lstPo.setAdapter(poListAdapter);
}
}
答案 0 :(得分:1)
您真的可以解决问题。问题是您在for loop
中设置了poId。因此,该值将是最后一次迭代。
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
//poId will be override after each iteration
poId = object.getLong("poId");
poNo = object.getString("poNo");
deliveryDt = object.getString("deliveryDt");
metersCompleted = object.getInt("metersCompleted");
totalMeters = object.getInt("totalMeters");
comments = object.getString("comments");
hideStatus = object.getInt("hideStatus");
poListData = new PO(poNo, deliveryDt, metersCompleted, totalMeters, comments,hideStatus);
poListAdapter.add(poListData);
}
所以我的解决方案是将poId存储在数组列表中:
// a class member
List<Long> poIds = new ArrayList<>();
在您的代码中:
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
//save ids inside List
poIds.add(object.getLong("poId"));
poNo = object.getString("poNo");
deliveryDt = object.getString("deliveryDt");
metersCompleted = object.getInt("metersCompleted");
totalMeters = object.getInt("totalMeters");
comments = object.getString("comments");
hideStatus = object.getInt("hideStatus");
poListData = new PO(poNo, deliveryDt, metersCompleted, totalMeters, comments,hideStatus);
poListAdapter.add(poListData);
}
最后在侦听器中:
stPo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(POListActivity.this, PODetailsActivity.class);
//retrieve correct poId here
intent.putExtra("poId",poIds.get(position));
startActivity(intent);
}
});
答案 1 :(得分:0)
如果PO类没有poId,请添加它。
更改PO类构造函数以接受poId作为参数。
将poId的吸气剂添加到PO类。
在doInBackground方法中,将poId作为参数传递,以在其中创建PO类的新实例。
poListData = new PO(poId, poNo, deliveryDt, metersCompleted, totalMeters, comments,hideStatus);
在onItemClickListener中使用此:
lstPo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
PO current = (PO) parent.getItemAtPosition(position);
Intent intent = new Intent(POListActivity.this, PODetailsActivity.class);
intent.putExtra("poId",current.getPoId());
startActivity(intent);
}
});