2016-03-15 42 views
0

我是新來的android開發,我正在做我的最後一年項目。在我的項目中,我想執行搜索操作。從數據庫中搜索並以列表視圖顯示,因此可能有多個結果。所以我想設置它的點擊監聽器。但每件事情都在工作,但當我試圖點擊列表視圖結果。它沒有工作..請給我一些建議或任何指導。我已經搜索了與我的問題有關的所有帖子,但沒有解決。請 這裏是我的Java代碼和方法SeeProfile OnClick事件設置從數據庫獲得價值,並在列表視圖中顯示並設置點擊監聽器

public class DisplaySearchResults extends ActionBarActivity { 
String json_string, json_string2; 
JSONObject jsonObject, JsonObject2; 
JSONArray jsonArray, jsonArray2; 
AdapterSearchElements adapterSearchElements; 
ListView listView; 

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

    listView = (ListView) findViewById(R.id.listviewsearch); 

    listView.setAdapter(adapterSearchElements); 

    adapterSearchElements = new AdapterSearchElements(this, R.layout.searchresultcutomrow); 
    listView.setAdapter(adapterSearchElements); 
    json_string = getIntent().getExtras().getString("json_data"); 

    try { 
     jsonObject = new JSONObject(json_string); 
     jsonArray = jsonObject.getJSONArray("server_response"); 

     int count = 0; 
     String name, Gender, address; 
     while (count < jsonArray.length()) { 
      JSONObject JO = jsonArray.getJSONObject(count); 
      name = JO.getString("name"); 
      Gender = JO.getString("Gender"); 
      address = JO.getString("address"); 
      SearchResultsElements searchResultsElements = new SearchResultsElements(name, Gender, address); 
      adapterSearchElements.add(searchResultsElements); 

      count++; 


     } 


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


public void SeeProfile(View view){ 
    json_string2 = getIntent().getExtras().getString("json_data"); 
    Intent i = new Intent(DisplaySearchResults.this, FriendsProfile.class); 
    i.putExtra("json_data", json_string2); 
    startActivity(i); 
} 

} 

回答

0

我認爲你是錯的方式來填補listview.and做谷歌搜索如何綁定適配器。

遵循這樣..

json_string = getIntent().getExtras().getString("json_data"); 

    try { 
     jsonObject = new JSONObject(json_string); 
     jsonArray = jsonObject.getJSONArray("server_response"); 

     int count = 0; 
     String name, Gender, address; 
     while (count < jsonArray.length()) { 
      JSONObject JO = jsonArray.getJSONObject(count); 
      name = JO.getString("name"); 
      Gender = JO.getString("Gender"); 
      address = JO.getString("address"); 
      SearchResultsElements searchResultsElements = new SearchResultsElements(name, Gender, address); 
      adapterSearchElements.add(searchResultsElements); 
      count++; 
     } 
     adapterSearchElements = new AdapterSearchElements(this, R.layout.searchresultcutomrow); 
     listView.setAdapter(adapterSearchElements); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

第一數組列表中添加然後將其綁定適配器後,數組值。

+0

這件事情正在工作......數據是完全正常工作,但clicklistener不工作 – Bebo

+0

@Bebo哪種情況下單擊偵聽器不工作? –

+0

clicklistener正在工作,但我想傳遞選定的數據並將其傳遞給另一個活動 – Bebo

相關問題