Denny R.Z Denny R.Z - 1 year ago 80
Android Question

passing data from ListView to another activity

i have list view with data from mysql, i want to passing the selected item to another activity, but always error null value.

this is myactivity

public class ReportActivity extends AppCompatActivity {

private String TAG = ReportActivity.class.getSimpleName();

private ProgressDialog pDialog;
private ListView lv;

// URL to get contacts JSON
private static String url = "";

ArrayList<HashMap<String, String>> reportList;

protected void onCreate(Bundle savedInstanceState) {

reportList = new ArrayList<>();

lv = (ListView) findViewById(;

new ReportActivity.GetLaporan().execute();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getApplicationContext(), ReportDetail.class);
intent.putExtra("area", String.valueOf(lv.getSelectedItem()));
intent.putExtra("note", String.valueOf(lv.getSelectedItem()));
intent.putExtra("alamat", String.valueOf(lv.getSelectedItem()));
intent.putExtra("status", String.valueOf(lv.getSelectedItem()));


public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
return true;
return super.onOptionsItemSelected(item);

* Async task class to get json by making HTTP call
private class GetLaporan extends AsyncTask<Void, Void, Void> {

protected void onPreExecute() {
// Showing progress dialog
pDialog = new ProgressDialog(ReportActivity.this);
pDialog.setMessage("Please wait...");


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("laporan");

// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);

String id = c.getString("id");
String note = c.getString("note_infra");
String status = c.getString("report_status");
String area = c.getString("area_infra");
String alamat = c.getString("alamat_infra");

// 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("note", note);
contact.put("status", status);
contact.put("area", area);
contact.put("alamat", alamat);

// adding contact to contact list
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
public void run() {
"Json parsing error: " + e.getMessage(),

} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
public void run() {
"Couldn't get json from server. Check LogCat for possible errors!",


return null;

protected void onPostExecute(Void result) {
// Dismiss the progress dialog
if (pDialog.isShowing())
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
ReportActivity.this, reportList,
R.layout.list_laporan, new String[]{"area", "note",
"alamat", "status"}, new int[]{,,,});




help me for send the value from listview

Your help will be appreciated..

Answer Source

change lv.getSelectedItem() to


By default, when you click on a ListView item it doesn't change state to selected, so, when the event fires and you do:


the method doesn't have anything to return.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download