2016-11-14 35 views
-1

我有MySQL的數據列表視圖,我想傳遞選定的項目到另一個活動,但總是錯誤空值。從ListView傳遞數據到另一個活動

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

    } 

} 
} 

幫助我從列表視圖

您的幫助將不勝感激送價值..

回答

0

變化lv.getSelectedItem()

parent.getAdapter().getItem(position) 

默認情況下,當你點擊一個ListView項目時,它不會將狀態更改爲選擇,因此,當事件觸發時,你做的事:

lv.getSelectedItem() 

的方法並沒有什麼可以回報。

+0

工作的感謝,但在陣列中的數據不是每個變量 –

+0

的價值,會以選擇當前點擊的項目在列表視圖中顯示的值。 –

+0

好的非常感謝你 –

0
lv.getSelectedItem() 

應該是reportList.get(position);

雖然你有4個值,所以我不確定你想要做什麼。

正確的方法做你想要的是一個POJO是Parcelable,你傳遞一個對象通過意向

相關問題