2014-08-29 74 views
0

我想在android應用程序中爲arraylist添加值。Java Android爲ArrayList添加值

,我遇到的問題是,該值不被插入到數組列表。

下面的代碼:

for (var c=0; c < SelectedFavContacts.length; c++){ 

     if (SelectedFavContacts[c] !== undefined) { 

      var SelectedContact = document.getElementById(SelectedFavContacts[c]).getElementsByTagName('a')[0].innerHTML; 

      for(i = 0 ; i < arrayKeys.length; i++) 
      { 
       var records = myContacts[arrayKeys[i]]; 

       for(var r_key in records) { 

        if (SelectedContact == records[r_key].name) { 

         if (typeof FavContacts[looper] == 'undefined') 
         { 
          FavContacts[looper] = new Array(); 
         } 

         FavContacts[looper].push({"name":records[r_key].name, "pNumber": records[r_key].pNumber, "img": records[r_key].img}); 
         looper++; 

         alert(records[r_key].name); //displaying successfully the name successfully; 

         window.MainActivity.SetFavSettings(records[r_key].name, records[r_key].pNumber, records[r_key].img); 

         break; 
        } 
       } 
      } 
     } 
    } 

private List<String> listContactName = new ArrayList<String>(); 
private List<String> listContactNo = new ArrayList<String>(); 
private List<String> listFavImg = new ArrayList<String>(); 

@JavascriptInterface 
public void SetFavSettings(String FavContactName, String FavContactNo, String FavImg){ 

    listContactName.add(FavContactName); 
    listContactNo.add(FavContactNo); 
    listFavImg.add(FavImg); 

    DialogInterface.OnClickListener dialogClickListenera = new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       switch (which){ 
       case DialogInterface.BUTTON_POSITIVE: 
        finish(); 
        break; 

       case DialogInterface.BUTTON_NEGATIVE: 
        break; 
       } 
      } 
     }; 

     // favContactName is being displayed. 
     //listContactName.get(0) = Nothing 
     // listContactName.size() = nothing 
     AlertDialog.Builder buildera = new AlertDialog.Builder(MainActivity.this); 
     buildera.setMessage("entered " + FavContactName + " checking :)) = " + listContactName.get(0) + " list size = " + listContactName.size()).setPositiveButton("Yes", dialogClickListenera) 
     .setNegativeButton("No", dialogClickListenera).show(); 
} 

你能告訴我什麼,我做錯了,請

+0

'listContactName.get(0)=如果你傳遞的參數是blank..but Nothing'會發生怎樣ç一個'listContactName.size()=什麼都不會發生?它應該給你至少零。 – 2014-08-29 13:19:01

回答

0

我認爲你必須在第一次測試條件運算符

if (SelectedFavContacts[c] !== undefined) 

我認爲錯誤這是!=不!==