我有来自mysql的数据的列表视图,我想将所选项目传递给另一个活动,但总是错误空值。
这是我的活动
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);
}
}
}
帮我发送listview
中的值您的帮助将不胜感激。
答案 0 :(得分:0)
将lv.getSelectedItem()
更改为
parent.getAdapter().getItem(position)
默认情况下,当您单击ListView项目时,它不会将状态更改为 selected ,因此,当事件触发时您执行以下操作:
lv.getSelectedItem()
该方法无法返回任何内容。
答案 1 :(得分:0)
lv.getSelectedItem()
应该是reportList.get(position);
但是你有4个值,所以我不确定你想做什么。
正确的方法来做你想要的是一个可以Parcelable的POJO,你通过Intent传递那个对象