【问题标题】:Setting extra variables to a buttonclicklistener in listview将额外变量设置为列表视图中的按钮单击侦听器
【发布时间】:2014-12-23 12:37:48
【问题描述】:

在我的应用程序中,我有一个 listview(asynctask) 从外部数据库获取所有用户。我的列表视图的每一行都包含一个用户可以交互的图像。当用户按下该图像时,我尝试使用我已经拥有的数据(通过在启动意图时使用 putExtra)来启动新意图。Listview 工作正常 - 它从数据库中收集所有数据。但是,每一行的 putExtra 值都是相同的。如果您能就此事帮助我,我将不胜感激。

代码如下; 重要的部分在下面

ImageView showlastonmap = (ImageView)v.findViewById(R.id.showlastonmap);

我尝试添加 putExtra 的地方。

 @Override
    protected void onPostExecute(JSONObject json) {
        pDialog.dismiss();
        try {

            if (json.getString(KEY_SUCCESS) != null) {
                String res = json.getString(KEY_SUCCESS);

                //Invitation count

                Integer inv =  json.getInt("invitation");
                setNotifCount(inv);

                if(Integer.parseInt(res) == 1) {

                    // Getting JSON Array from URL

                    android1 = json.getJSONArray(TAG_LOCATIONDATA);
                    for (int i = 0; i < android1.length(); i++) {
                        JSONObject c = android1.getJSONObject(i);

                        // Storing  JSON item in a Variable
                        final String ver = c.getString("username"); 
                        final String dataenlem = c.getString("enlem");
                        final String databoylam = c.getString("boylam");
                        final String datazaman = c.getString("zaman");

                        /*----------to get City-Name from coordinates ------------- */
                        StringBuffer address = new StringBuffer();
                        Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
                        List<Address> addresses;
                        try {
                            addresses = gcd.getFromLocation(Double.parseDouble(dataenlem), Double.parseDouble(databoylam), 1);

                            if (addresses.size() > 0)
                               // System.out.println(addresses.get(0).getLocality());
                                address.append(addresses.get(0).getAddressLine(1))
                                        .append(",").append(addresses.get(0).getAddressLine(2));

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        String datalokasyon = address.toString();
                        /*----------to get City-Name from coordinates ------------- */

                        // Adding value HashMap key => value
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(TAG_VER, ver);
                        map.put("enlem", dataenlem);
                        map.put("boylam", databoylam);
                        map.put("zaman", datazaman);
                        map.put("lokasyon", datalokasyon);


                        oslist.add(map);

                        list = (ListView) findViewById(R.id.list);

                        BaseAdapter adapter = new SimpleAdapter(UserList.this, oslist,
                                R.layout.listview_userlist,
                                new String[]{TAG_VER,"zaman","lokasyon"}, new int[]{
                                R.id.vers,R.id.lastlocationinput,R.id.lastupdatedinput}) {

                            public View getView (int position, View convertView, ViewGroup parent)
                            {

                                View v = super.getView(position, convertView, parent);

                                ImageView showlastonmap = (ImageView)v.findViewById(R.id.showlastonmap);

                                showlastonmap.setOnClickListener(new View.OnClickListener() {

                                    public void onClick(View v) {



                                        Intent login = new Intent(getApplicationContext(),
                                                HaritaLastSolo.class);
                                        login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                        login.putExtra("enlem", dataenlem);
                                        login.putExtra("boylam", databoylam);
                                        startActivity(login);

                                    }
                                });
                                return v;
                            }

                        };

                       SwingBottomInAnimationAdapter animationAdapter = new SwingBottomInAnimationAdapter(adapter);
                        animationAdapter.setAbsListView(list);

                        list.setAdapter(animationAdapter);
       }

                    //admob
                    interstitial.setAdListener(new AdListener() {
                        public void onAdLoaded() {
                            displayInterstitial();
                        }
                    });
                    //admob


                }
                if(Integer.parseInt(res) == 0) {

                    new SweetAlertDialog(UserList.this, SweetAlertDialog.ERROR_TYPE)
                            .setTitleText("Oops...")
                            .setContentText(getString(R.string.userlist_nofriend))
                            .show();
                }

            }

            else {

                return;
            }
             }catch
             (JSONException e) {
            e.printStackTrace();
        }
    }
}

【问题讨论】:

    标签: android listview android-listview onclicklistener simpleadapter


    【解决方案1】:

    你应该这样做:

     Intent login = new Intent(getApplicationContext(),
               HaritaLastSolo.class);
             login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
               login.putExtra("enlem", oslist.get(position).get("enlem"));
               login.putExtra("boylam", oslist.get(position).get("boylam"));
              startActivity(login);
    

    【讨论】:

    • 哇。就像一个魅力:) 我希望我早点问过,我花了 5 个小时来找到解决方案。非常感谢。
    【解决方案2】:

    您必须创建包含必填字段的新对象。

    class Data { 
        String dataenlem;
        String databoylam;
        Data(String dataenlem, String databoylam) {
         ...
        }
    }
    

    然后使用setTag方法设置对应每一行数据

     ImageView showlastonmap = (ImageView)v.findViewById(R.id.showlastonmap);
      // add this
     showlastonmap.setTag(new Data(dataenlem, databoylam));
    

    并在您的 onClick 上获取它

    public void onClick(View v) {
       // and this
       Data data = (Data)v.getTag();
    
       Intent login = new Intent(getApplicationContext(),HaritaLastSolo.class);
       login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
       login.putExtra("enlem", data.dataenlem);
       login.putExtra("boylam", data.databoylam);
       startActivity(login);
    }
    

    【讨论】:

    • 感谢您的宝贵时间。我试过了,但没用。
    猜你喜欢
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    相关资源
    最近更新 更多