コード例 #1
0
  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();
    }
  }
コード例 #2
0
  private void createNewContactObject(String type, View my_contact_view) {
    if (my_contact_view == null) {
      my_contact_view = fragment_mycontact.getView();
    }

    if (my_contact_view == null) return;

    LinearLayout ll_data;
    List<String> list = new ArrayList<String>();
    int it = android.text.InputType.TYPE_CLASS_TEXT;
    int hint = 0;

    // Typen setzen:
    List<Integer> list_number = new ArrayList<Integer>();

    for (int i = 0; i < array_en_type.length; i++) {
      if (array_en_type[i].equals(type)) {
        list_number.add(i);
      }
    }

    for (int i = 0; i < list_number.size(); i++) {
      list.add((array_en_name[list_number.get(i)]));
    }

    final String[] spinnerArray = list.toArray(new String[list.size()]);

    // -----------

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

    // new row
    LinearLayout row = new LinearLayout(this);
    row.setOrientation(LinearLayout.HORIZONTAL);

    DisplayMetrics tmp_metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(tmp_metrics);
    int resolution_width = tmp_metrics.widthPixels;

    final boolean large_screen = (resolution_width >= 720);

    final EditText et = new EditText(this);
    LayoutParams lp_et;
    lp_et = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 1);

    if (type.equals("number")) {
      ll_data = ll_telephone;

      it = InputType.TYPE_CLASS_PHONE;

      hint = R.string.telephone;

    } else if (type.equals("email")) {
      ll_data = ll_email;

      it = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;

      hint = R.string.email;

    } else {
      ll_data = ll_other;

      if (type.equals("address")) {

        et.setMinLines(1);
        et.setMaxLines(3);
        et.setLines(2);

        hint = R.string.address;
        it =
            InputType.TYPE_CLASS_TEXT
                | InputType
                    .TYPE_TEXT_FLAG_MULTI_LINE; // android.text.InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS;
        lp_et = new LayoutParams(LayoutParams.WRAP_CONTENT, large_screen ? 130 : 90, 1);

        row.setLayoutParams(
            new LayoutParams(LayoutParams.MATCH_PARENT, (large_screen ? 130 : 90) + 20));

        et.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
        // et.setGravity(Gravity.LEFT | Gravity.TOP );

      } else if (type.equals("website")) {
        hint = R.string.website;
        it = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI;
      }
    }

    ((LinearLayout) ll_data.getParent()).setVisibility(LinearLayout.VISIBLE);

    et.setTextSize(large_screen ? 16 : 14); // EditText-Textgr��e
    et.setEms(10);
    et.setHint(hint);

    et.setInputType(it);

    lp_et.setMargins(0, 3, 0, 0);
    et.setLayoutParams(lp_et);

    Spinner spinner = new Spinner(this);
    LayoutParams lp_spinner =
        new LayoutParams(
            large_screen ? Functions.dpsToPx(this, 100) : Functions.dpsToPx(this, 80), /*300 : 200*/
            LayoutParams.MATCH_PARENT);

    lp_spinner.setMargins(0, 3, 0, 0);

    MySpinnerArrayAdapter dataAdapter = new MySpinnerArrayAdapter(this, list, large_screen);

    dataAdapter.setDropDownViewResource(
        android.support.v7.appcompat.R.layout.support_simple_spinner_dropdown_item);

    spinner.setAdapter(dataAdapter);
    spinner.setLayoutParams(lp_spinner);

    spinner.setOnItemSelectedListener(
        new OnItemSelectedListener() {

          @Override
          public void onItemSelected(
              final AdapterView<?> adapterView, final View view, int arg2, long arg3) {
            final Spinner spinner = (Spinner) adapterView;
            MySpinnerArrayAdapter adapter = (MySpinnerArrayAdapter) spinner.getAdapter();
            if (adapter
                .getItem(arg2)
                .toLowerCase(new Locale("en"))
                .equals(getString(R.string.en_custom).toLowerCase(new Locale("en")))) {

              LinearLayout ll = new LinearLayout(MainActivity.this);
              final EditText et = new EditText(MainActivity.this);

              LinearLayout.LayoutParams layoutParams =
                  new LinearLayout.LayoutParams(
                      LinearLayout.LayoutParams.MATCH_PARENT,
                      LinearLayout.LayoutParams.WRAP_CONTENT);
              layoutParams.setMargins(0, 20, 0, 0);

              et.setTextColor(Color.BLACK);
              et.requestFocus();

              // et.setHint(R.string.place);

              ll.addView(et, layoutParams);

              AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
              builder
                  .setView(ll)
                  .setTitle(R.string.message_custom_en)
                  .setPositiveButton(
                      android.R.string.ok,
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {

                          String[] newItems = new String[spinnerArray.length + 1];
                          System.arraycopy(spinnerArray, 0, newItems, 0, spinnerArray.length);
                          newItems[newItems.length - 1] = et.getText().toString();

                          MySpinnerArrayAdapter adapter =
                              new MySpinnerArrayAdapter(
                                  getApplicationContext(),
                                  newItems,
                                  ((MySpinnerArrayAdapter) spinner.getAdapter()).getLargeScreen());
                          spinner.setAdapter(adapter);
                          spinner.setSelection(newItems.length - 1);
                        }
                      })
                  .setNegativeButton(
                      android.R.string.cancel,
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {}
                      })
                  .create()
                  .show();
            }
          }

          @Override
          public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

          }
        });

    Button btn = new Button(this);
    btn.setText("x");
    LayoutParams lp_btn = new LayoutParams(Functions.dpsToPx(this, 32), LayoutParams.WRAP_CONTENT);
    lp_btn.setMargins(-10, 0, -10, 0);
    btn.setLayoutParams(lp_btn);
    btn.setBackgroundColor(Color.TRANSPARENT);

    btn.setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            if (((LinearLayout) v.getParent().getParent()).getChildCount() == 1) {
              ((LinearLayout) v.getParent().getParent().getParent()).setVisibility(View.GONE);
            }
            ((LinearLayout) v.getParent().getParent()).removeView((LinearLayout) v.getParent());
          }
        });

    row.addView(et);
    row.addView(spinner);
    row.addView(btn);

    row.setTag(type);

    ll_data.addView(row);
  }