如何返回列表视图中选择项的ID

编程入门 行业动态 更新时间:2024-10-19 18:28:37
本文介绍了如何返回列表视图中选择项的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

朋友你好我是新来的Andr​​oid和我正在学习关于An​​droid编程。我和我的朋友合作。我从我显示在列表视图城市名单的Web服务,现在我的朋友给我的任务是,当我会点击城市则该城市的ID还给我,所以我不会怎么做,请任何人都可以帮我这样做。

Hello friends i am new to android and i am learning about android programming. I am working with my friends. I have web service from which i am showing list of cities in listview now my friend give me task that is when i will click on city then id of that city return me so i don't how to do so please anybody can help me to do so.

public class CityNameActivity extends ListActivity{ private TextView displayText; ListView list; private ProgressDialog pDialog; // URL to get Cities JSON private static String url = "14.140.200.186/Hospital/get_city.php"; // JSON Node names private static final String TAG_CITIES = "Cities"; private static final String TAG_ID = "city_id"; private static final String TAG_NAME = "city_name"; // Cities JSONArray JSONArray Cities = null; // Hashmap for ListView ArrayList<HashMap<String, String>> citylist; //ArrayList<String> citylist; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.cityname_activity_main); ListView listView=getListView(); citylist = new ArrayList<HashMap<String, String>>(); new GetCities().execute(); } /** * Async task class to get json by making HTTP call * */ private class GetCities extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); // Showing progress dialog pDialog = new ProgressDialog(CityNameActivity.this); pDialog.setMessage("Please wait..."); pDialog.setCancelable(false); pDialog.show(); } @Override protected Void doInBackground(Void... arg0) { // Creating service handler class instance ServiceHandler sh = new ServiceHandler(); // Making a request to url and getting response String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); Log.d("Response: ", "> " + jsonStr); if (jsonStr != null) { try { JSONObject jsonObj = new JSONObject(jsonStr); // Getting JSON Array node Cities = jsonObj.getJSONArray(TAG_CITIES); // looping through All Cities for (int i = 0; i < Cities.length(); i++) { JSONObject c = Cities.getJSONObject(i); String id = c.getString(TAG_ID); String name = c.getString(TAG_NAME); HashMap<String, String> Cities = new HashMap<String, String>(); Cities.put(TAG_ID, id); Cities.put(TAG_NAME, name); // adding contact to Cities list citylist.add(Cities); } } catch (JSONException e) { e.printStackTrace(); } } else { Log.e("ServiceHandler", "Couldn't get any data from the url"); } return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); // Dismiss the progress dialog if (pDialog.isShowing()) pDialog.dismiss(); /**`enter code here` * Updating parsed JSON data into ListView * */ final ListAdapter adapter = new SimpleAdapter(CityNameActivity.this, citylist, R.layout.city_list_item, new String[] { TAG_NAME}, new int[] { R.id.name}); setListAdapter(adapter); } }}

cityname_mai_activity.xml

cityname_mai_activity.xml

<ListView android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView4" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Select City" android:id="@+id/textView4" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="25dp"/>

city_list_item:

city_list_item:

<TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="2dip" android:paddingTop="6dip" android:textSize="16sp" android:textStyle="bold" />

推荐答案

你检查在点击监听器您的ListView ??我认为,这将解决您的问题。使用 onClikcListener的在项目单击事件,这将给你的位置,并基于该位置你可以从你的 ArrayList中获取项目,这将给你 ID 从..

Did you check On Click Listener for your ListView ?? I think that will solve your problem.. Use onClikcListener's on ItemClick event which will give you position and based on that position you can get item from your Arraylist which will give you id from that..

更多推荐

如何返回列表视图中选择项的ID

本文发布于:2023-11-10 22:22:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1576619.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   列表   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!