0

我在Android中創建了一個listview,但現在我試圖讓listview在傳遞參數的同時可以點擊一個新的活動。 listview的數據來自api。代碼如下。點擊進入ListView的新活動

private static String urlString; 

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

    urlString = "http://websiteforapi/api/market"; 
    new ProcessJSON().execute(urlString); 
} 

private class ProcessJSON extends AsyncTask<String, Void, String> { 
    protected String doInBackground(String...strings){ 
     String stream = null; 
     String urlString = strings[0]; 

     HTTPDataHandler hh = new HTTPDataHandler(); 
     stream = hh.GetHTTPData(urlString); 

     // Return the data from specified url 
     return stream; 
    } 

    protected void onPostExecute(String stream){ 

     //..........Process JSON DATA................ 
     if(stream !=null){ 
      try { 
       // Get the full HTTP Data as JSONObject 
       JSONObject reader = new JSONObject(stream); 

       JSONArray businessArray = reader.getJSONArray("data"); 
       ArrayList<String> businesses = new ArrayList<String>(); 
       for(int i = 0; i < businessArray.length(); i++) { 
        JSONObject business_object_0 = businessArray.getJSONObject(i); 
        final String businessname = business_object_0.getString("name"); 
        final String business = business_object_0.getString("business"); 
        businesses.add(businessname); 
       } 

       ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1, businesses); 
       getListView().setAdapter(adapter); 

      }catch(JSONException e){ 
       e.printStackTrace(); 
      } 

     } // if statement end 
    } // onPostExecute() end 
} // ProcessJSON class end 

我從API使用JSON是這樣的......

{"data":[{"id":"3","name":"My Test Business 3","phone":"6785555555","email":"[email protected]","address":"123 This Avenue","city":"Atlanta","state":"GA","zip":"30308","business":"Technology 33333"},{"id":"4","name":"My Test Business 2 ","phone":"6783571004","email":"[email protected]","address":"123 Some Street","city":"Lawrenceville","state":"GA","zip":"30043","business":"Website building"},{"id":"5","name":"My Test Business 3","phone":"6785555555","email":"[email protected]","address":"123 This Avenue","city":"Atlanta","state":"GA","zip":"30308","business":"Technology 3"},{"id":"6","name":"My Test Business 5","phone":"6785555555","email":"[email protected]","address":"123 This Avenue","city":"Atlanta","state":"GA","zip":"30308","business":"Technology 4"}]} 

到目前爲止,我可以得到ListView控件來顯示,但到目前爲止,我無法讓他們點擊到一個新的活動。

回答

0

試試這個作爲你的要求......

yourListView.setOnItemClickListener(new OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
         // Code for start new Activity 
         Intent intent = new Intent(MainActivity.this, NewActivity.class); 
         startActivity(intent); 
        } 
       }); 

但你爲什麼不使用主的細節!

+0

不確定你的意思是主細節。但代碼工作。有沒有辦法將選定的數組列表項目的參數傳遞給下一個活動?我嘗試使用以下內容。 Intent intent = new Intent(MarketPlaceActivity.this,SelectedBusinessActivity.class); intent.putExtra(「name」,business); startActivity(intent); – teej2542

+0

看到這個傳遞數據[鏈接](http://stackoverflow.com/questions/3510649/how-to-pass-a-value-from-one-activity-to-another-in-android) – Esty

0

您可以使用OnItemClickListener

getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      startActivity(this, NewActivity.class); 
     } 
    }); 

當然的newActivity需要在清單登記。