Beranda > Android, Android Studio, Pemrograman > Mengisi AutoCompleteTextView dan Spinner dengan Contact – Android Studio

Mengisi AutoCompleteTextView dan Spinner dengan Contact – Android Studio

logo android studio

spinnerautocomplete

 

Pada tulisan kali ini kita akan mengisi AutoCompleteTextView dan Spinner dengan Contact. Berikut file-file yang diperlukan :

1.  AndroidManifest.xml
2. MainActivity.java
3.  activity_main.xml

Pertama, kita nambah baris kode pada AndroidManifest.xml sebagai berikut:

...
<uses-permission android:name="android.permission.READ_CONTACTS" />
...

Kedua, buat file MainActivity.java yang berisi:

package com.wordpress.yopiardinal.autocomplete;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Spinner;
import java.util.Vector;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Spinner isi_contact;
        //S1 = (Spinner)findViewById(R.id.spinner);

        Vector<String> isi_contact1=new Vector<String>();

        //awal ISI contact
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        while (phones.moveToNext())
        {
            String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            String replace_62_phoneNumber = phoneNumber.replace("+62", "0");
            String replace_spasi_phoneNumber = replace_62_phoneNumber.replace(" ","");
            String replace_strip_phoneNumber = replace_spasi_phoneNumber.replace("-","");
            isi_contact1.add(name + " " + replace_strip_phoneNumber);

        }
        phones.close();
        // Awal Isi contact pada Spinner
        Spinner isi_contact = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> adapter_contact = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, isi_contact1);
        isi_contact.setAdapter(adapter_contact);
        // Akhir Isi contact pada Spinner 

        // Awal Isi contact pada AutoCompleteTextView
        ArrayAdapter<String> adapterautocomplate = new ArrayAdapter<String> (this,android.R.layout.select_dialog_item,isi_contact1);
        AutoCompleteTextView actv= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
        actv.setThreshold(1);//will start working from first character
        actv.setAdapter(adapterautocomplate);//setting the adapter data into the AutoCompleteTextView
        actv.setTextColor(Color.BLACK);
        // Akhir Isi contact pada AutoCompleteTextView

        // Akhir ISI contact

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Ketiga, buat file layout activity_main.xmlyang berisi :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:id="@+id/autocomplete">

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="235dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="AutoComplete"
        android:id="@+id/textView"
        android:layout_marginTop="43dp"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Mengisi AutoCompleteTextView dan Spinner dengan Contact"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Spinner"
        android:id="@+id/textView3"
        android:layout_below="@+id/autoCompleteTextView1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Spinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:layout_below="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

Sumber : Sorry gw lupa… Halamannya dah ketutup… ntar kalo gw inget gw masukkin dah…

  1. Belum ada komentar.
  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: