2016-11-15 105 views
-1

我有我的課是基於在線教程,我還沒有完全理解它(工作),但它的工作。從列表視圖中獲取特定的json對象點擊

它填充列表視圖,現在我想要獲取id並在更詳細的活動上顯示與該id相關的數據。

我已經獲得該項目的ID我正在點擊:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View view, int position, 
          long id) { 
     Log.e("item clicks", "selected: " + position); 



    } 
}); 

但現在,我沒有得到我怎麼會做這個,我得到的點擊位置的數據。

我有一個內部類「GetObras」,但我不能使用這些變量從它在我的onCreate,我試圖讓他們全球等

public class MainActivity extends ActionBarActivity implements SearchView.OnQueryTextListener{ 

    private String TAG = MainActivity.class.getSimpleName(); 
    private ProgressDialog pDialog; 
    private ListView list; 

    private static String url = "http://ploran.gear.host/scriptobras6.php"; 

    ArrayList<HashMap<String, String>> obrasList; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     obrasList = new ArrayList<HashMap<String, String>>(); 
     list = (ListView)findViewById(R.id.list1); 

     new GetObras().execute(); 

     list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View view, int position, 
            long id) { 
       Log.e("item clicks", "selected: " + position); 



      } 
     }); 
    } 

    private class GetObras extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // Showing progress dialog 
      pDialog = new ProgressDialog(MainActivity.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); 
        JSONArray obras = new JSONArray(jsonStr); 
        // Getting JSON Array node 
        //JSONArray obras = jsonObj.getJSONArray("obras"); 

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

         String id = c.getString("Id"); 
         String nomeObra = c.getString("NomeObra"); 
         String idCliente = c.getString("idCliente"); 
         String DataLevantamento = c.getString("DataPLevantamento"); 
         String DataRealizacao = c.getString("DataRLevantamento"); 
         String Estado = c.getString("Estado"); 
         String DataMateriais = c.getString("DataRMateriais"); 
         String DataInicioObra = c.getString("DataInicioObra"); 
         String DataConclusao = c.getString("DataConclusao"); 
         String DataVestoria = c.getString("DataVestoria"); 
         String Obs = c.getString("Obs"); 
         String Prompor = c.getString("Prompor"); 
         String Levantpor = c.getString("Levantpor"); 
         String executpor = c.getString("executpor"); 

         // tmp hash map for single contact 
         HashMap<String, String> obra = new HashMap<>(); 

         // adding each child node to HashMap key => value 
         obra.put("Id", id); 
         obra.put("nomeObra", nomeObra); 
         obra.put("idCliente", idCliente); 
         obra.put("DataLevantamento", DataLevantamento); 
         obra.put("DataRealizacao", DataRealizacao); 
         obra.put("Estado", Estado); 
         obra.put("DataMateriais", DataMateriais); 
         obra.put("DataIncioObra", DataInicioObra); 
         obra.put("DataConclusao", DataConclusao); 
         obra.put("DataVestoria", DataVestoria); 
         obra.put("Obs", Obs); 
         obra.put("Prompor", Prompor); 
         obra.put("Levantpor", Levantpor); 
         obra.put("executpor", executpor); 

         // adding contact to contact list 
         obrasList.add(obra); 
        } 
       } 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(
        MainActivity.this, obrasList, 
        R.layout.list_item, new String[]{"nomeObra", "idCliente", 
        "Estado"}, new int[]{R.id.name, 
        R.id.email, R.id.mobile}); 

      list.setAdapter(adapter); 
     } 
    } 

    List<String> cities; 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_search, menu); 

     MenuItem searchItem = menu.findItem(R.id.search); 

     return true; 
    } 

    @Override 
    public boolean onQueryTextSubmit(String query) { 
     // User pressed the search button 
     return false; 
    } 

    @Override 
    public boolean onQueryTextChange(String newText) { 
     // User changed the text 
     return false; 
    } 
} 

如果我認爲是正確的,我可以得到JsonArray從GetObras中的doInBackground方法執行:

JSONObject c = obras.getJSONObject(position); 

謝謝。

回答

2

您可以使用obrasList參考檢索它。當你正在將obrasList傳遞給你的適配器時。

下面是示例代碼:

obrasList.get(position).get(yourkey); 

希望這將幫助你.. :))

相關問題