无法从点击android中的listitem接收字符串Id

时间:2016-03-24 15:08:50

标签: android json android-intent

我想从listItem中获取变量Id的值,并将其传递给另一个activity.But我没有在变量中收到任何内容。 这是附加的代码:

MainActivity.java

package com.example.hp.citysearchapp;

            import android.app.ProgressDialog;
            import android.content.Intent;
            import android.support.design.widget.TextInputLayout;
            import android.support.v7.app.AppCompatActivity;
            import android.os.Bundle;
            import android.support.v7.view.menu.ExpandedMenuView;
            import android.util.Log;
            import android.view.View;
            import android.widget.AdapterView;
            import android.widget.EditText;
            import android.widget.ImageView;
            import android.widget.ListView;
            import android.widget.TextView;
            import android.widget.Toast;

            import com.android.volley.Response;
            import com.android.volley.VolleyError;
            import com.android.volley.VolleyLog;
            import com.android.volley.toolbox.JsonArrayRequest;

            import org.json.JSONArray;
            import org.json.JSONException;
            import org.json.JSONObject;

            import java.util.ArrayList;
            import java.util.HashMap;
            import java.util.List;

    public class MainActivity extends AppCompatActivity {

        private static final String TAG = MainActivity.class.getSimpleName();
        private ProgressDialog pDialog;
        private List<City> cityList = new ArrayList<City>();
        private ListView listView;
        private static String url;
        ImageView searchIcon;
        String idGet;
        String edittextSearch;
        TextInputLayout searchLayout;
        EditText search;
        private CustomListAdapter adapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView = (ListView)findViewById(R.id.city_listView);
            searchLayout=(TextInputLayout)findViewById(R.id.input_layout_search);
            search=(EditText)findViewById(R.id.input_search);
            searchIcon=(ImageView)findViewById(R.id.imageView);
            adapter = new CustomListAdapter(this,cityList);
            searchIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    edittextSearch=search.getText().toString();
                    Log.d("hello2", search.getText().toString());
                    adapter.notifyDataSetChanged();
                    url = "http://test.maheshwari.org/services/testwebservice.asmx/SuggestCity?tryValue="+edittextSearch;
                    parsingMethod();
                }
            });

            listView.setAdapter(adapter);


            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent = new Intent(MainActivity.this,CityDetailsActivity.class);
                   City city = cityList.get(position);


                    idGet=city.getId();
                    Log.d("dfjdkfj", idGet);     //Not receiving anything ,here is the problem


                    intent.putExtra("gettingId",idGet);
                    startActivity(intent);
                }
            });
                            }

        private void parsingMethod() {

            Log.d("hello", url);
            pDialog = new ProgressDialog(this);
            // Showing progress dialog
            pDialog.setMessage("Loading...");
            pDialog.show();

            // Creating volley request obj
            JsonArrayRequest cityReq = new JsonArrayRequest(url,
                    new Response.Listener<JSONArray>() {

                        @Override
                        public void onResponse(JSONArray jsonArray) {
                            hidePDialog();

                            // Parsing json
                            for (int i = 0; i < jsonArray.length(); i++) {

                                JSONObject obj = null;
                                try {
                                    obj = jsonArray.getJSONObject(i);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                City city = new City();
                                try {
                                    city.setId(obj.getString("Id"));
                                    city.setTitle(obj.getString("Title"));
                                    city.setDescription(obj.getString("Description"));
                                    city.setExv1(obj.getString("ExtraValue1"));
                                    Log.d("hello",obj.getString("ExtraValue1"));
                                    city.setExv2(obj.getString("ExtraValue2"));
                                    city.setExv3(obj.getString("ExtraValue3"));
                                    city.setExv4(obj.getString("ExtraValue4"));
                                    city.setExv5(obj.getString("ExtraValue5"));
                                    city.setExv6(obj.getString("ExtraValue6"));
                                    city.setExv7(obj.getString("ExtraValue7"));
                                    city.setExv8(obj.getString("ExtraValue8"));
                                    city.setExv9(obj.getString("ExtraValue9"));

                                    cityList.add(city);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }


                            }

                            // notifying list adapter about data changes
                            // so that it renders the list view with updated data
                            adapter.notifyDataSetChanged();
                        }

                    }, new Response.ErrorListener()

            {
                @Override
                public void onErrorResponse (VolleyError error){
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hidePDialog();

                }

            });


            // Adding request to request queue
            AppController.getInstance().addToRequestQueue(cityReq);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            hidePDialog();
        }

        private void hidePDialog() {
            if (pDialog != null) {
                pDialog.dismiss();
                pDialog = null;
            }
        }


    }

City.java

package com.example.hp.citysearchapp;

    /**
     * Created by hp on 22-03-2016.
     */
    public class City {
        String title,description,exv1,exv3,exv6,id,exv2,exv4,exv5,exv9,exv7,exv8;


        public City(String title, String description, String id, String exv1, String exv3, String exv6,
                    String exv2, String exv4 , String exv5 , String exv7 , String exv8 , String exv9) {
            this.title = title;
            this.description=description;
            this.id=id;
            this.exv1=exv1;
            this.exv2=exv2;
            this.exv3=exv3;
            this.exv4=exv4;
            this.exv5=exv5;
            this.exv6=exv6;
            this.exv7=exv7;
            this.exv8=exv8;
            this.exv9=exv9;
        }

        public City() {

        }

        public String getId() {
            return id;
        }

        public String getExv2() {
            return exv2;
        }

        public String getExv4() {
            return exv4;
        }

        public String getExv5() {
            return exv5;
        }

        public String getExv7() {
            return exv7;
        }

        public String getExv8() {
            return exv8;
        }

        public String getExv9() {
            return exv9;
        }

        public String getTitle() {
            return title;
        }

        public String getDescription() {
            return description;
        }

        public String getExv1() {
            return exv1;
        }

        public String getExv3() {
            return exv3;
        }

        public String getExv6() {
            return exv6;
        }

        public void setId(String id) {
            this.id = id;
        }

        public void setExv2(String exv2) {
            this.exv2 = exv2;
        }

        public void setExv4(String exv4) {
            this.exv4 = exv4;
        }

        public void setExv5(String exv5) {
            this.exv5 = exv5;
        }

        public void setExv7(String exv7) {
            this.exv7 = exv7;
        }

        public void setExv8(String exv8) {
            this.exv8 = exv8;
        }

        public void setExv9(String exv9) {
            this.exv9 = exv9;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public void setExv1(String exv1) {
            this.exv1 = exv1;
        }

        public void setExv3(String exv3) {
            this.exv3 = exv3;
        }

        public void setExv6(String exv6) {
            this.exv6 = exv6;
        }
    }

2 个答案:

答案 0 :(得分:-1)

请稍微修改您的代码以实现此目的 -

你已经 -

City city = cityList.get(position);

现在添加此行以从ListItem点击 -

获取ID
String Id = city.getExv3(); //Use one of your actual Getter methods accordingly.

希望这有帮助!

答案 1 :(得分:-1)

由于我在列表中使用CustomAdapter,因此我必须使用以下代码:

TextView textView = (TextView)view.findViewById(R.id.id); /*(R.id.id) is the id of the textview */
String gettingID=textView.getText().toString();