我是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 :(得分:0)
您可以设置listview OnItemClickListener。示例如下:
http://www.vogella.com/tutorials/AndroidListView/article.html
答案 1 :(得分:0)
我认为你填写listview是错误的方法。并且谷歌搜索如何绑定适配器。
按照这种方式进行..
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();
}
首先在绑定适配器之后在Arraylist中添加数组值。