Denny R.Z Denny R.Z - 19 days ago 6
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 = "http://demoweb.pe.hu/c_report_hp";

ArrayList<HashMap<String, String>> reportList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

reportList = new ArrayList<>();

lv = (ListView) findViewById(R.id.listLaporan);

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

@Override
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()));
startActivity(intent);
}

});
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

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

@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(ReportActivity.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("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
reportList.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(
ReportActivity.this, reportList,
R.layout.list_laporan, new String[]{"area", "note",
"alamat", "status"}, new int[]{R.id.areaInfra,
R.id.noteInfra, R.id.alamatInfra, R.id.statusReport});

lv.setAdapter(adapter);

}

}
}


help me for send the value from listview

Your help will be appreciated..

Answer

change lv.getSelectedItem() to

parent.getAdapter().getItem(position)

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

lv.getSelectedItem()

the method doesn't have anything to return.

Comments