在我的 Android 项目中,我创建了一个ListView
并且onClickListener
还添加了一个,但是在运行我的项目时,ListView
它没有响应我对它的点击。
代码
public class MyDayListActivity extends ListActivity {
// JSON Node names
private static final String TAG_RESULTS = "results";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_RATING = "rating";
private static final String TAG_REFERENCE = "reference";
private static final String TAG_ICON = "icon";
// contacts JSONArray
JSONArray result = null;
String url,reference;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent in = getIntent();
String location = in.getStringExtra("TAG_LOCATION");
String type = in.getStringExtra("TAG_TYPE");
// url to make request
url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query="+type+"+in+"+location+"&sensor=true&key=AIzaSyD38pak_katUfSR92WE2_O2b9y0JSp5htA";
new LoadData().execute();
}
class LoadData extends AsyncTask<String, String, String>
{
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
protected String doInBackground(String... args) {
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Results
result = json.getJSONArray(TAG_RESULTS);
// looping through All Contacts
for(int i = 0; i < result.length(); i++){
JSONObject c = result.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String icon = c.getString(TAG_ICON);
reference = c.getString(TAG_REFERENCE);
String rating = c.getString(TAG_RATING);
// creating new HashMap
HashMap<String, String> map2 = new HashMap<String, String>();
// adding each child node to HashMap key => value
map2.put(TAG_ID, id);
map2.put(TAG_NAME, name);
map2.put(TAG_ICON, icon);
map2.put(TAG_RATING, rating);
map2.put(TAG_REFERENCE, reference);
// adding HashList to ArrayList
contactList.add(map2);
}
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// Updating parsed JSON data into ListView
ListAdapter adapter1 = new SimpleAdapter(MyDayListActivity.this, contactList,R.layout.listview_item_row,
new String[] {TAG_NAME}, new int[] {
R.id.txtTitle});
setListAdapter(adapter1);
// selecting single ListView item
// Launching new screen on Selecting Single ListItem
ListView lv1 = getListView();
lv1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Starting new intent
Intent in = new Intent(getApplicationContext(), ProfileViewActivity.class);
in.putExtra(TAG_REFERENCE, reference);
startActivity(in);
}
});
}
}
}