je lis les données de mysql avec JSON et php et les mets dans un listView avec AsyncTask et je suis essayant d'obtenir l'identification d'article avec setOnItemClickListener et le griller mais il me montre le mauvais identifiant par exemple quand je clique sur l'identifiant de l'article 4 il me montre 2 merci si vous m'aidez.android-Obtenir l'ID de l'article dans listView
ActivityList public class étend AppCompatActivity {
private String TAG = ActivityList.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
private String[] items ;
// URL to get contacts JSON
private static String url = "My URL";
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
contactList = new ArrayList<>();
lv = (ListView) findViewById(R.id.listItems);
new 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(ActivityList.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("houseDB");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String name = c.getString("name");
String address = c.getString("address");
String telephone = c.getString("telephone");
String option = c.getString("option");
String ID = c.getString("ID");
// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("name", name);
contact.put("address", address);
contact.put("telephone", telephone);
contact.put("option", option);
contact.put("ID", ID);
//put id row in array
items = new String[]{ID};
// 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();
}
});
}
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(
ActivityList.this, contactList,
R.layout.list_item, new String[]{"name", "address",
"telephone", "option", "ID"}, new int[]{R.id.txtName,
R.id.txtAddress, R.id.txtTelephone, R.id.txtOption, R.id.txtID});
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//get item id
TextView itemID = (TextView) findViewById(R.id.txtID);
String text = itemID.getText().toString();
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
});
}
}
}
get id du tableau à la position cliqué –