我想从listview
获取id,而不是位置。该ID在JSON文件中,所以当我在listview
中显示数据时,所以当我点击它时,我从JSON文件中获取该驱动程序的ID。如何从ClickListener的ListView获取id值
[{的lastName = wdkj FSJ,名字= JF JFN,ID = 1},{的lastName =辛格 的firstName =晴天,ID = 2}]
public class DriverData extends AppCompatActivity {
private String TAG = DriverData.class.getSimpleName();
ArrayAdapter<String> adapter;
private ProgressDialog pDialog;
private ListView lv;
// URL to get contacts JSON
private static String url = "http://54.148.120.200:3000/getDrivers";
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.driver_data);
contactList = new ArrayList<>();
lv = (ListView) findViewById(R.id.simpleListView);
new DriverData.GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
*/
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(DriverData.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("drivers");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("id");
JSONObject user = c.getJSONObject("user");
String firstName = user.getString("firstName");
String lastName = user.getString("lastName");
// Phone node is JSON Object
// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("id", id);
contact.put("firstName", firstName);
contact.put("lastName", lastName);
// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
System.out.println(contactList);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
DriverData.this, contactList,
R.layout.list_item, new String[]{"firstName", "lastName"
}, new int[]{R.id.name, R.id.lastName});
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
}
});
}
}
}
想从JSON获取ID吗? – deen
尝试先学习Java以及JSON。 :) – deen
当用户点击显示列表视图(具有所有的驱动程序的名字和姓氏),它有我想要的ID驱动程序的ID –