如何返回列表视图中选择项的ID [英] How to return the id of selected item of listview

查看:182
本文介绍了如何返回列表视图中选择项的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 = "http://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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆