0

在我的應用程序中,我有一個listview(asynctask)從外部數據庫獲取所有用戶。我的listview的每一行都包含一個用戶可以交互的圖像。當用戶按下該圖像時,我嘗試使用我已有的數據(通過在啓動intent時使用putExtra)開始新的意圖。Listview工作正常 - 它收集數據庫中的所有數據。但是,putExtra值對於每一行都是相同的。如果您能就此事幫助我,我將不勝感激。將額外的變量設置爲列表視圖中的buttonclicklistener

代碼如下; 最重要的部分是低於

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(); 
     } 
    } 
} 

回答

0

你應該做的:

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); 
+0

哇。工作就像一個魅力:)我希望我早些時候問過它,我花了5個小時找到解決方案。非常感謝你。 – ZaLeX

+0

樂意幫忙.. – Sar

0

您必須創建包含必需字段的新對象。

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); 
} 
+0

感謝你的時間。我嘗試過但沒有工作。 – ZaLeX