Android 将数据从ListView传递到另一个活动

Android 将数据从ListView传递到另一个活动,android,listview,Android,Listview,我有来自mysql的数据列表视图,我想将所选项目传递给另一个活动,但总是错误null值 这是我的活动 public class ReportActivity extends AppCompatActivity { private String TAG = ReportActivity.class.getSimpleName(); private ProgressDialog pDialog; private ListView lv; // URL to get contacts JSON

我有来自mysql的数据列表视图,我想将所选项目传递给另一个活动,但总是错误null值

这是我的活动

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);

    }

}
}
公共类ReportActivity扩展了AppCompatActivity{
私有字符串标记=ReportActivity.class.getSimpleName();
私人对话;
私有ListView lv;
//获取联系人JSON的URL
专用静态字符串url=”http://demoweb.pe.hu/c_report_hp";
ArrayList报告列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_报告);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
reportList=新的ArrayList();
lv=(ListView)findViewById(R.id.listLaporan);
新建ReportActivity.GetLaporan().execute();
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
Intent Intent=新的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());
星触觉(意向);
}
});
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
NavUtils.navigateUpFromSameTask(本);
返回true;
违约:
返回super.onOptionsItemSelected(项目);
}
}
/**
*异步任务类通过HTTP调用获取json
*/
私有类GetLaporan扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=newprogressdialog(ReportActivity.this);
setMessage(“请稍候…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
HttpHandler sh=新的HttpHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
JSONArray contacts=jsonObj.getJSONArray(“laporan”);
//通过所有触点循环
对于(int i=0;ivalue
联系人:put(“id”,id);
联系方式。注意(“注意”,注意);
联系人。放置(“状态”,状态);
联系方式。放置(“区域”,区域);
联系人:put(“阿拉马特”,阿拉马特);
//将联系人添加到联系人列表
报告列表。添加(联系人);
}
}捕获(最终JSONException e){
Log.e(标记“Json解析错误:”+e.getMessage());
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
Json分析错误:“+e.getMessage(),
吐司长度(长)
.show();
}
});
}
}否则{
e(标记“无法从服务器获取json”);
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
Toast.makeText(getApplicationContext(),
“无法从服务器获取json。请检查LogCat以了解可能的错误!”,
吐司长度(长)
.show();
}
});
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
//关闭进度对话框
if(pDialog.isShowing())
pDialog.disclose();
/**
*将解析的JSON数据更新到ListView中
* */
ListAdapter=新的SimpleAdapter(
ReportActivity.this,reportList,
R.layout.list_laporan,新字符串[]{“区域”,“注释”,
“alamat”,“status”},新的int[]{R.id.areaInfra,
R.id.noteInfra、R.id.alamatInfra、R.id.statusReport});
低压设置适配器(适配器);
}
}
}
帮助我从listview发送值


感谢您的帮助。

lv.getSelectedItem()
更改为

parent.getAdapter().getItem(position)
默认情况下,当您单击ListView项目时,它不会将状态更改为selected,因此,当事件激发时,您会:

lv.getSelectedItem()
该方法没有任何要返回的内容

lv.getSelectedItem()
应为reportList.get(位置)

但是,您有4个值,所以我不确定您要对此做什么


正确的方法是使用可打包的POJO,通过Intent传递一个对象,但数组中的数据不是每个变量的值。它将选择lis中显示的当前单击项目的值