ListView上的onClickListener没有响应
在我的Android项目中,我创建了一个ListView上的onClickListener没有响应,listview,android-listview,listadapter,onclicklistener,Listview,Android Listview,Listadapter,Onclicklistener,在我的Android项目中,我创建了一个列表视图,同时还添加了一个onClickListener,但在运行我的项目时,列表视图没有响应我的点击 代码 public class MyDayListActivity extends ListActivity { // JSON Node names private static final String TAG_RESULTS = "results"; private stat
列表视图
,同时还添加了一个onClickListener
,但在运行我的项目时,列表视图
没有响应我的点击
代码
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);
}
});
}
}
}
公共类MyDayListActivity扩展了ListActivity{
//JSON节点名称
私有静态最终字符串标记_RESULTS=“RESULTS”;
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串标记_RATING=“RATING”;
私有静态最终字符串标记_REFERENCE=“REFERENCE”;
私有静态最终字符串标记\u ICON=“ICON”;
//联系JSONArray
JSONArray结果=null;
字符串url,引用;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent in=getIntent();
字符串位置=in.getStringExtra(“标记位置”);
字符串类型=in.getStringExtra(“标记类型”);
//发出请求的url
url=”https://maps.googleapis.com/maps/api/place/textsearch/json?query=“+type+”+in+“+location+”&传感器=true&key=AIzaSyD38pak_katUfSR92WE2_O2b9y0JSp5htA”;
新建LoadData().execute();
}
类LoadData扩展异步任务
{
//ListView的Hashmap
ArrayList contactList=新建ArrayList();
受保护的字符串doInBackground(字符串…args){
//创建JSON解析器实例
JSONParser jParser=新的JSONParser();
//从URL获取JSON字符串
JSONObject json=jParser.getJSONFromUrl(url);
试一试{
//获取一系列结果
result=json.getJSONArray(TAG_RESULTS);
//通过所有触点循环
for(int i=0;ivalue
map2.put(标签ID,ID);
地图2.put(标签名称、名称);
map2.put(标签图标,图标);
图2.put(标签额定值、额定值);
地图2.put(标签参考,参考);
//将哈希列表添加到ArrayList
联系人列表。添加(map2);
}
}
捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(字符串文件\u url){
//将解析的JSON数据更新到ListView中
ListAdapter adapter1=新的SimpleAdapter(MyDayListActivity.this,contactList,R.layout.listview\u item\u行,
新字符串[]{TAG_NAME},新int[]{
R.id.txtTitle});
setListAdapter(适配器1);
//选择单个ListView项
//在选择单个列表项时启动新屏幕
ListView lv1=getListView();
lv1.setOnItemClickListener(新的OnItemClickListener()
{
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//开始新的意图
Intent in=新的Intent(getApplicationContext(),ProfileViewActivity.class);
in.putExtra(标签参考,参考);
星触觉(in);
}
});
}
}
}
getListView().setOnItemLongClickListener(此);
公共布尔值长单击(最终适配器查看我的适配器,视图arg1,最终整数arg2,
长arg3){
返回false;
}