我正在尝试将列表项的一些细节传递给新活动。 不幸的是,我总是得到新活动列表第一项的详细信息。这是由于使用相同名称命名的textview(我猜)的名称。如何点击与项目相关的内容?
list
下面是我创建的用于获取第一个活动传递的额外数据的类。
public class GetFloor extends ListActivity {
private Context context;
private static String url = "http://indoorlocation.altervista.org/JSON/floor.php";
private static final String FLOORID = "FloorID";
private static final String BUILDINGID = "BuildingID";
private static final String FLOORNAME = "FloorName";
private static final String DESCRIPTION = "Description";
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
ListView lv ;
TextView txFloorid;
TextView txBuildingid;
TextView txFloorname;
TextView txDescription;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getfloor);
new ProgressTask(GetFloor.this).execute();
}
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
private ListActivity activity;
// private List<Message> messages;
public ProgressTask(ListActivity activity) {
this.activity = activity;
context = activity;
dialog = new ProgressDialog(context);
}
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
@Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
ListAdapter adapter = new SimpleAdapter(context, jsonlist, R.layout.floorlist, new String[] { FLOORID, BUILDINGID, FLOORNAME, DESCRIPTION}, new int[] { R.id.FloorID, R.id.BuildingID, R.id.FloorName, R.id.Description});
setListAdapter(adapter);
// select single ListView item
lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
txFloorid = (TextView) parent.findViewById(R.id.FloorID);
txBuildingid = (TextView) parent.findViewById(R.id.BuildingID);
txFloorname = (TextView) parent.findViewById(R.id.FloorName);
txDescription = (TextView) parent.findViewById(R.id.Description);
Intent intent = new Intent(GetFloor.this, FloorDetails.class);
intent.putExtra("floorid", txFloorid.getText().toString());
intent.putExtra("buildingid", txBuildingid.getText().toString());
intent.putExtra("floorname", txFloorname.getText().toString());
intent.putExtra("description", txDescription.getText().toString());
startActivity(intent);
}
});
}
protected Boolean doInBackground(final String... args) {
JSONParser jParser = new JSONParser();
// get JSON data from URL
JSONArray json = jParser.getJSONFromUrl(url);
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
String floorid = c.getString(FLOORID);
String buildingid = c.getString(BUILDINGID);
String floorname = c.getString(FLOORNAME);
String description = c.getString(DESCRIPTION);
HashMap<String, String> map = new HashMap<String, String>();
// Add child node to HashMap key & value
map.put(FLOORID, floorid);
map.put(BUILDINGID, buildingid);
map.put(FLOORNAME, floorname);
map.put(DESCRIPTION, description);
jsonlist.add(map);
}
catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
}
}
下面是与楼层列表相关的xml文件。
public class FloorDetails extends Activity {
private static final String FLOORID = "FloorID";
private static final String BUILDINGID = "BuildingID";
private static final String FLOORNAME = "FloorName";
private static final String DESCRIPTION = "Description";
TextView floorid;
TextView buildingid;
TextView floorname;
TextView description;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.floordetails);
floorid = (TextView) findViewById(R.id.FloorID);
buildingid = (TextView) findViewById(R.id.BuildingID);
floorname = (TextView) findViewById(R.id.FloorName);
description = (TextView)findViewById(R.id.Description);
// get the intent from which this activity is called.
Intent intent = getIntent();
// fetch value from key-value pair and make it visible on TextView.
String FloorID = intent.getStringExtra("floorid");
String BuildingID = intent.getStringExtra("buildingid");
String FloorName = intent.getStringExtra("floorname");
String Description = intent.getStringExtra("description");
floorid.setText(FLOORID + ":" + FloorID);
buildingid.setText(BUILDINGID + ":" + BuildingID);
floorname.setText(FLOORNAME + ":" + FloorName);
description.setText(DESCRIPTION + ":" + Description) ;
}
}
任何帮助将不胜感激,我为我的代码格式错误道歉。
提前谢谢你, Celair