javascript - 使用循环获取“未定义”的简单函数

时间:2016-08-31 10:20:08

标签: javascript jscript

所以我似乎无法弄清楚......代码本质上是有效的,但是在运行函数之前我得到了一个“未定义的”..

public static List<SearchModel> getContactDetailsForQuery(Context context, String query, int limit) {

    final int CONTACT_ID_INDEX = 0;
    final int CONTACT_NAME_INDEX = 1;
    final int CONTACT_THUMBNAIL_INDEX = 2;

    //my custom model to hold my results
    List<SearchModel> results = new ArrayList<SearchModel>();

    final String[] selectUser = new String[]{
        Contacts._ID,
        Contacts.DISPLAY_NAME,
        Contacts.PHOTO_THUMBNAIL_URI};
    String selection = Contacts.DISPLAY_NAME + " LIKE ?";
    String[] selectionArgs = new String[]{"%" + query + "%"};
    String sortOrder = Contacts.DISPLAY_NAME + " ASC";

    Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_URI, selectUser, selection, selectionArgs, sortOrder, null);

    int contactCounter = 0;
    if (cursor != null && cursor.moveToFirst()) {

        do {
            String contactID = cursor.getString(CONTACT_ID_INDEX);
            String displayName = cursor.getString(CONTACT_NAME_INDEX);
            String thumbnail = cursor.getString(CONTACT_THUMBNAIL_INDEX);

            //get user details with user id (this is the query i wanted to change in my question!!)
            Cursor detailCursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
                new String[]{
                    CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS},
                ContactsContract.Data.CONTACT_ID + "=? AND " +
                    CommonDataKinds.StructuredPostal.MIMETYPE + "=?",
                new String[]{String.valueOf(contactID), CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE},
                null);

            if (detailCursor != null && detailCursor.moveToFirst()) {
                //special case: user has several address, query all of them
                do {
                    String formattedAddress = detailCursor.getString(detailCursor.getColumnIndex(CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
                    //user has serveral adress -> init model for each adress
                    SearchModel contact = new SearchModel();
                    results.add(contact);
                    contactCounter++;

                } while (detailCursor.moveToNext() && contactCounter < limit);

            } else {

                //user has no adress -> init model
                SearchModel contact = new SearchModel();
                results.add(contact);
                contactCounter++;
            }

            detailCursor.close();

        } while (cursor.moveToNext() && contactCounter < limit);
    }
    cursor.close();

    return results;
}

另外,我不知道为什么我的“正斜杠也不起作用......我正在逃避”/“..但是它们仍然被省略..

感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

这是正确的做法。

&#13;
&#13;
var navRender = function indexNav(x, y) {

  var mainNav = ["\/", "Inicio", "\/abogados-criminales-los-angeles.html", "Defensa Criminal", "\/delitos-de-drogas-abogados.html", "Delitos de Drogas", "\/area-de-practica-abogados.html", "Areas de Practica", "\/abogados-friendman-los-angeles.html", "Abogados Friedman", "\/contactenos.html", "Contactenos"];

  var html = "";

  for (i = x; i <= y; i = i + 2) {
    html = html + "<li class='pure-menu-item pure-u-1-3'><a class='pure-menu-link' href='" + mainNav[i] + "'>" + mainNav[i + 1] + "</a></li>";
  };

  return html;
};
document.getElementById("demo").innerHTML = navRender(0, 5);
&#13;
<div id="demo">
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

=&GT;从for循环中删除 i ++ 。因为它在for循环中也是加号。

var navRender = function indexNav(x,y) {

     var mainNav = ["\/", "Inicio","\/abogados-criminales-los-angeles.html", "Defensa Criminal", 
     "\/delitos-de-drogas-abogados.html", "Delitos de Drogas", 
     "\/area-de-practica-abogados.html", "Areas de Practica",
     "\/abogados-friendman-los-angeles.html", "Abogados Friedman",
     "\/contactenos.html", "Contactenos"];

  for (i = x; i <= y; i++) 
   {
      document.write("<li class=\"pure-menu-item pure-u-1-3\"><a class=\"pure-menu-link href=\"",mainNav[i]);
      document.write("\">", mainNav[i] , "<\/a><\/li>");
   };
 };
 navRender(0,5);

答案 2 :(得分:0)

&#13;
&#13;
var navRender = function indexNav(x,y) {

     var mainNav = ["\/", "Inicio", "\/abogados-criminales-los-angeles.html", "Defensa Criminal", "\/delitos-de-drogas-abogados.html", "Delitos de Drogas", "\/area-de-practica-abogados.html", "Areas de Practica", "\/abogados-friendman-los-angeles.html", "Abogados Friedman", "\/contactenos.html", "Contactenos"];
  
  var result = '';
  for (var i = x; i <= y; i = i + 2) {
    result += '<li class="pure-menu-item pure-u-1-3\">'
    result += '<a class="pure-menu-link" href="' + mainNav[i] + '">' + mainNav[i + 1];
    result += '</a>';
    result += '</li>';
  }

  return result;
 };
 document.getElementById("demo").innerHTML = navRender(0,5);
&#13;
<ul id="demo"></ul>
&#13;
&#13;
&#13;