private void loadEntryNames() {
    try {
      SQLiteConnection db_conn = new SQLiteConnection(getApplicationContext());

      Cursor cursor =
          db_conn.query(
              "SELECT id,name,type_name FROM "
                  + SQLiteConnection.TABLE_ENTRY_NAMES
                  + " ORDER BY sort");

      array_en_id = null;
      array_en_id = new int[cursor.getCount()];
      array_en_name = null;
      array_en_name = new String[cursor.getCount()];
      array_en_type = null;
      array_en_type = new String[cursor.getCount()];

      for (int i = 0; i < cursor.getCount(); i++) {
        cursor.moveToPosition(i);
        array_en_id[i] = cursor.getInt(0);
        array_en_name[i] = cursor.getString(1);
        array_en_type[i] = cursor.getString(2);
      }

      cursor.close();

      db_conn.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
        public void onClick(DialogInterface dialog, int id) {

          SQLiteConnection sql = new SQLiteConnection(getApplicationContext());

          Cursor c =
              getContentResolver()
                  .query(ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI, null, null, null, null);
          try {

            c.moveToFirst();

            Uri rawContactUri =
                ContentUris.withAppendedId(
                    RawContacts.CONTENT_URI, c.getLong(c.getColumnIndex("contact_id")));
            Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
            c =
                getContentResolver()
                    .query(
                        entityUri,
                        new String[] {
                          RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1
                        },
                        null,
                        null,
                        null);

            while (c.moveToNext()) {

              if (!c.isNull(1)) {
                String mimeType = c.getString(2);
                String data = c.getString(3);

                Cursor tmp_c =
                    sql.query(
                        "SELECT id FROM "
                            + SQLiteConnection.TABLE_USER_DATA
                            + " WHERE value='"
                            + data
                            + "'");
                if (tmp_c.getCount() == 0) {

                  ContentValues values = new ContentValues();

                  tmp_c =
                      sql.query(
                          "SELECT id FROM "
                              + SQLiteConnection.TABLE_USER_DATA
                              + " ORDER BY id DESC");
                  tmp_c.moveToFirst();
                  int newID = tmp_c.getInt(0) + 1;

                  int en_id = -1;

                  if (mimeType.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {

                    for (int i = 0; i < array_en_type.length; i++) {
                      if (array_en_type[i].equals("number")) {
                        en_id = array_en_id[i];

                        tmp_c =
                            sql.query(
                                "SELECT id FROM "
                                    + SQLiteConnection.TABLE_USER_DATA
                                    + " WHERE entrynames_id='"
                                    + String.valueOf(en_id)
                                    + "'");
                        if (tmp_c.getCount() == 0) break;
                      }
                    }

                  } else if (mimeType.equals(
                      ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {

                    for (int i = 0; i < array_en_type.length; i++) {
                      if (array_en_type[i].equals("email")) {
                        en_id = array_en_id[i];

                        tmp_c =
                            sql.query(
                                "SELECT id FROM "
                                    + SQLiteConnection.TABLE_USER_DATA
                                    + " WHERE entrynames_id='"
                                    + String.valueOf(en_id)
                                    + "'");
                        if (tmp_c.getCount() == 0) break;
                      }
                    }
                  }

                  if (en_id != -1) {
                    values.put("id", newID);
                    values.put("entrynames_id", en_id);
                    values.put("value", data);
                    sql.insert(SQLiteConnection.TABLE_USER_DATA, values);
                  }
                }
                tmp_c.close();
              }
            }

            new Thread(
                    new Runnable() {

                      @Override
                      public void run() {

                        new UserFunctions(getApplicationContext()).syncMyContact();
                      }
                    })
                .start();

            showDataInMyContact(fragment_mycontact.getView());

          } finally {
            c.close();
            sql.close();
          }
        }
  public void showDataInMyFriends(final View friendsView) {

    if (friendsView == null) return;

    ListView lv_main = (ListView) friendsView.findViewById(R.id.myfriends_lv_main);
    LinearLayout ll_info_no_item =
        (LinearLayout) friendsView.findViewById(R.id.myfriends_ll_info_no_item);

    SQLiteConnection db_conn = new SQLiteConnection(getApplicationContext());

    List<ContactListViewItem> items = new ArrayList<ContactListViewItem>();

    lv_main.setVisibility(View.GONE);
    ll_info_no_item.setVisibility(View.VISIBLE);

    // Anfragen:

    final Cursor requests =
        db_conn.query(
            "SELECT id,name,frontname,birthday,place,pic FROM "
                + SQLiteConnection.TABLE_REQUESTS
                + " ORDER BY frontname, name"); // <> TODO eventuell //
    // nach nachname ordnen

    for (int i = 0; i < requests.getCount(); i++) {
      if (i == 0) {
        lv_main.setVisibility(View.VISIBLE);
        ll_info_no_item.setVisibility(View.GONE);
      }

      requests.moveToPosition(i);

      items.add(
          new ContactListViewItem() {
            {
              String str_pic = requests.getString(5);
              if ((str_pic != null) && (!str_pic.equals("")) && (!str_pic.equals("null"))) {
                try {
                  byte[] imageAsBytes = Base64.decode(str_pic.getBytes(), Base64.DEFAULT);
                  Bitmap bmThumbnail =
                      BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);

                  int x = Functions.dpsToPx(getApplicationContext(), 64);
                  bmThumbnail = Functions.scaleCenterCrop(bmThumbnail, x, x);

                  Thumbnail = bmThumbnail;
                } catch (Exception e) {
                  e.printStackTrace();
                }
              }
              ThumbnailResource = R.drawable.no_user_pic;
              Title = requests.getString(2) + " " + requests.getString(1);
              SubTitle = requests.getString(4);

              Id = (requests.getInt(0) * (-1) - 1);

              BackColor = getResources().getColor(R.color.request_background);
            }
          });
    }

    // Freunde:

    final Cursor friends =
        db_conn.query(
            "SELECT id,name,frontname,birthday,place,pic FROM "
                + SQLiteConnection.TABLE_FRIENDS
                + " ORDER BY frontname, name"); // <> TODO eventuell //
    // nach nachname ordnen

    for (int i = 0; i < friends.getCount(); i++) {

      if (i == 0) {
        lv_main.setVisibility(View.VISIBLE);
        ll_info_no_item.setVisibility(View.GONE);
      }

      friends.moveToPosition(i);

      items.add(
          new ContactListViewItem() {
            {
              String str_pic = friends.getString(5);
              if ((str_pic != null) && (!str_pic.equals("")) && (!str_pic.equals("null"))) {
                try {
                  byte[] imageAsBytes = Base64.decode(str_pic.getBytes(), Base64.DEFAULT);
                  Bitmap bmThumbnail =
                      BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);

                  int x = Functions.dpsToPx(getApplicationContext(), 64);
                  bmThumbnail = Functions.scaleCenterCrop(bmThumbnail, x, x);

                  Thumbnail = bmThumbnail;

                } catch (Exception e) {
                }
              }
              ThumbnailResource = R.drawable.no_user_pic;
              Title = friends.getString(2) + " " + friends.getString(1);
              SubTitle = friends.getString(4);

              Id = friends.getLong(0);
            }
          });
    }

    ContactListViewAdapter adapter = new ContactListViewAdapter(this, items);

    lv_main.setAdapter(adapter);

    lv_main.setOnItemClickListener(
        new OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            // Item - Click

            try {

              SQLiteConnection db_conn = new SQLiteConnection(getApplicationContext());
              Fragment myfraFragment = fragment_friends;

              boolean request = false;

              ListView lv_main = (ListView) friendsView.findViewById(R.id.myfriends_lv_main);

              long tmp_friend_id = ((ContactListViewItem) lv_main.getItemAtPosition(arg2)).Id;
              String[] items = null;
              Cursor friends;
              if (tmp_friend_id < 0) {
                // Anfrage:

                tmp_friend_id = ((tmp_friend_id + 1) * -1);

                friends =
                    db_conn.query(
                        "SELECT id,name,frontname,birthday,place,pic FROM "
                            + SQLiteConnection.TABLE_REQUESTS
                            + " WHERE id='"
                            + tmp_friend_id
                            + "'");
                friends.moveToFirst();

                request = true;

              } else {
                // Freund:
                friends =
                    db_conn.query(
                        "SELECT id,name,frontname,birthday,place,pic FROM "
                            + SQLiteConnection.TABLE_FRIENDS
                            + " WHERE id='"
                            + String.valueOf(tmp_friend_id)
                            + "'");
                friends.moveToFirst();

                Cursor friends_data =
                    db_conn.query(
                        "SELECT entrynames_id,value FROM "
                            + SQLiteConnection.TABLE_FRIENDS_DATA
                            + " WHERE friend_id='"
                            + String.valueOf(tmp_friend_id)
                            + "' ORDER BY entrynames_id");
                items = new String[friends_data.getCount()];

                for (int i = 0; i < friends_data.getCount(); i++) {
                  friends_data.moveToPosition(i);
                  items[i] =
                      String.valueOf(friends_data.getInt(0)) + "|" + friends_data.getString(1);
                }
              }

              String strPic = friends.getString(5);
              Bitmap bmPic;
              if (strPic != null && !(strPic.equals("") && !strPic.equals("null"))) {
                try {
                  byte[] imageAsBytes = Base64.decode(strPic.getBytes(), Base64.DEFAULT);
                  bmPic = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);

                  // int x=Functions.dpsToPx(getApplicationContext(),128);
                  bmPic = Functions.scaleCenterCrop(bmPic, 256, 256);
                } catch (Exception e) {
                  bmPic = null;
                }
              } else {
                bmPic = null;
              }

              Intent details = new Intent(getApplicationContext(), DetailsDialogActivity.class);

              details.putExtra(DetailsDialogActivity.PARAM_NAME, friends.getString(1));
              details.putExtra(DetailsDialogActivity.PARAM_FRONTNAME, friends.getString(2));
              details.putExtra(DetailsDialogActivity.PARAM_BIRTHDAY, friends.getString(3));
              details.putExtra(DetailsDialogActivity.PARAM_PLACE, friends.getString(4));
              details.putExtra(DetailsDialogActivity.PARAM_PIC, bmPic);

              details.putExtra(DetailsDialogActivity.PARAM_ITEMS, items);

              details.putExtra(DetailsDialogActivity.PARAM_ID, tmp_friend_id);

              if (!request) {
                details.putExtra(DetailsDialogActivity.PARAM_ACTION_ID, 3);
              } else {

                details.putExtra(DetailsDialogActivity.PARAM_ACTION_ID, 2);
              }

              myfraFragment.startActivityForResult(details, 1);

              friends.close();
              db_conn.close();

            } catch (Exception e) {
              // Fehlermeldung
              e.printStackTrace();
            }
          }
        });

    db_conn.close();
    lv_main.setAdapter(adapter);
  }
  public void showDataInMyContact(View myContactView) {

    if (myContactView == null) return;

    // lade Daten in UI
    try {

      SQLiteConnection db_conn = new SQLiteConnection(this);

      String[] user_res = db_conn.getUser();

      if (user_res != null) {
        TextView tv_name = (TextView) myContactView.findViewById(R.id.mycontact_tv_name);
        TextView tv_frontname = (TextView) myContactView.findViewById(R.id.mycontact_tv_frontname);
        ImageView iv_pic = (ImageView) myContactView.findViewById(R.id.mycontact_iv_picture);

        tv_name.setText(user_res[2]);
        tv_frontname.setText(user_res[3]);

        if (!(user_res[6] == null || user_res[6].equals("") || user_res[6].equals("null"))) {

          byte[] imageAsBytes = Base64.decode(user_res[6].getBytes(), Base64.DEFAULT);
          Bitmap bmPic = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);

          int x = Functions.dpsToPx(this, 64);
          bmPic = Functions.scaleCenterCrop(bmPic, x, x);

          iv_pic.setImageBitmap(bmPic);
        }
      }

      LinearLayout ll_telephone =
          (LinearLayout) myContactView.findViewById(R.id.mycontact_ll_data_telephone);
      LinearLayout ll_email =
          (LinearLayout) myContactView.findViewById(R.id.mycontact_ll_data_email);
      LinearLayout ll_other =
          (LinearLayout) myContactView.findViewById(R.id.mycontact_ll_data_other);

      // jetzigen Inhalt l�schen
      ll_telephone.removeAllViews();
      ll_email.removeAllViews();

      ll_other.removeAllViews();

      Cursor mycontact_data =
          db_conn.query(
              "SELECT entrynames_id,value FROM "
                  + SQLiteConnection.TABLE_USER_DATA
                  + " ORDER BY entrynames_id");

      for (int i = 0; i < mycontact_data.getCount(); i++) {

        mycontact_data.moveToPosition(i);

        String name = array_en_name[Functions.indexOf(array_en_id, mycontact_data.getInt(0))];
        String type = array_en_type[Functions.indexOf(array_en_id, mycontact_data.getInt(0))];

        String value = mycontact_data.getString(1);
        String[] splitedValue = value.split("\\|");

        createNewContactObject(type, myContactView);
        LinearLayout container;

        if (type.equals("number")) {
          container = ll_telephone;
        } else if (type.equals("email")) {
          container = ll_email;
        } else {
          container = ll_other;
        }

        LinearLayout child = (LinearLayout) container.getChildAt(container.getChildCount() - 1);

        Spinner spinner = (Spinner) child.getChildAt(1);

        String items[] = new String[spinner.getAdapter().getCount()];
        for (int a = 0; a < spinner.getAdapter().getCount(); a++) {
          items[a] = ((String) spinner.getItemAtPosition(a)).toLowerCase(new Locale("en"));
        }

        if (splitedValue.length >= 2) {
          String[] newItems = new String[items.length + 1];
          System.arraycopy(items, 0, newItems, 0, items.length);
          newItems[newItems.length - 1] = splitedValue[0];

          MySpinnerArrayAdapter adapter =
              new MySpinnerArrayAdapter(
                  this, newItems, ((MySpinnerArrayAdapter) spinner.getAdapter()).getLargeScreen());
          spinner.setAdapter(adapter);
          spinner.setSelection(newItems.length - 1);
          ((EditText) child.getChildAt(0)).setText(splitedValue[1]);
        } else {
          spinner.setSelection(Functions.indexOf(items, name.toLowerCase(new Locale("en"))));
          ((EditText) child.getChildAt(0)).setText(value);
        }
      }

      mycontact_data.close();

      db_conn.close();

      myContactIsLoaded = true;
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public boolean saveDataFromMyContact() {
    // speichere Daten von der UI

    try {

      SQLiteConnection db_conn = new SQLiteConnection(this);

      String[][] ui_data = getMyContactObjects();

      if ((!myContactIsLoaded) || (ui_data == null)) {
        db_conn.close();
        return false;
      }

      Cursor data =
          db_conn.query("SELECT entrynames_id,value FROM " + SQLiteConnection.TABLE_USER_DATA);
      String[][] data_edit = new String[data.getCount()][];

      for (int i = 0; i < data.getCount(); i++) {
        data.moveToPosition(i);
        data_edit[i] = new String[] {String.valueOf(data.getInt(0)), data.getString(1)};
      }

      if (!Functions.equalsArray(ui_data, data_edit)) {
        // Mein Kontakt in lokale db schreiben

        db_conn.executeQuery("DELETE FROM " + SQLiteConnection.TABLE_USER_DATA);

        for (int i = 0; i < ui_data.length; i++) {

          String[] str_tmp = ui_data[i];

          ContentValues values = new ContentValues();

          values.put("id", i);
          values.put("entrynames_id", Integer.parseInt(str_tmp[0]));
          values.put("value", str_tmp[1]);

          db_conn.insert(SQLiteConnection.TABLE_USER_DATA, values);
        }

        db_conn.executeQuery(
            "UPDATE " + SQLiteConnection.TABLE_USER + " SET change_date=CURRENT_TIMESTAMP");

        String checksums = db_conn.getChecksums();

        db_conn.setChecksums(
            checksums.replace(checksums.substring(32, 64), "00000000000000000000000000000000"));

        try {
          if (thread_sync != null) thread_sync.interrupt();
        } finally {
        }

        thread_sync =
            new Thread(
                new Runnable() {
                  public void run() {
                    new UserFunctions(getApplicationContext()).sync();
                  }
                });
        thread_sync.start();
      }

      data.close();
      db_conn.close();

    } catch (Exception ex) {
      ex.printStackTrace();
      return false;
    }

    return true;
  }