Sunday, 17 July 2016

Akses Phonebook Android part-2

Gambar 1. Akses phonebook
Pada tutorial sebelumnya telah dibahas bagaimana cara mengakses phonebook, maka tutorial ini akan membahas bagaimana  menyusun data phonebook yang sudah diakses menjadi seperti Gambar 1.

Pada cara mengaksesnya data pada phonebook akan sedikit berbeda karena pada kali ini akan mempersoalkan urutan nama pada phonebook yang mana nama harus urut ascending dan tanpa memperhatikan huruf kapital atau tidak.

sourcecode akses data phonebook sebelumnya:


  Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);

menjadi:

 Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                null, null,  "upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");

Data yang sudah diakses akan disusun menggunakan ListView.CHOICE_MODE_MULTIPLE

sourcecode keseluruhan:
pada manifest jangan lupa tambahkan:
<uses-permission android:name="android.permission.READ_CONTACTS" />

PhoneBook.java:
package id.oddsaydev.aksesphonebook;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;



/**
 * Created by Windows on 13/07/2016.
 */
public class PhoneBook  extends AppCompatActivity {
    String[]valueName, valuePhone;
    ListView listview;
    Toolbar toolbar;
    int jumlahnomortelp=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.phonebook);
         toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.drawable.arrowleft);
        listview = (ListView) findViewById(R.id.listView);

        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                null, null,  "upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");
         jumlahnomortelp=phones.getCount();
        valueName=new String [jumlahnomortelp];
        valuePhone=new String [jumlahnomortelp];
        int i=0;
        toolbar.setTitle("0/" + jumlahnomortelp);
        while (phones.moveToNext())
        {
            String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            valueName[i]=name;
            valuePhone[i]=phoneNumber;
  i++;
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(PhoneBook.this,
                android.R.layout.simple_list_item_multiple_choice, valueName );
        listview.setAdapter(adapter);

        listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                toolbar.setTitle(listview.getCheckedItemCount() + "/" + jumlahnomortelp);

            }
        });
        View view = toolbar.getChildAt(2);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();


            }
        });
        RelativeLayout selesaiid = (RelativeLayout) findViewById(R.id.selesaiid);
        selesaiid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SparseBooleanArray checked = listview.getCheckedItemPositions();
                String namadannohp="";
                for (int i = 0; i < listview.getAdapter().getCount(); i++) {
                    if (checked.get(i)) {
                        namadannohp=namadannohp+valueName[i]+":"+valuePhone[i]+"\n";
                    }
                }
                Toast.makeText(PhoneBook.this, namadannohp, Toast.LENGTH_SHORT).show();

            }
        });

    }

    @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);
    }
}

phonebook.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@android:color/holo_blue_dark"
            android:id="@+id/toolbar"
            app:titleTextColor="@android:color/white"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:title="3/20" >
            <RelativeLayout
                android:orientation="vertical" android:layout_width="60dp"
                android:visibility="visible"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_gravity="end"
                android:clickable="true"
                android:background="@drawable/selesaiklik"
                android:id="@+id/selesaiid">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@android:color/white"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="selesai"
                    />
            </RelativeLayout>
        </android.support.v7.widget.Toolbar>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

Artikel Terkait Android

1 comment: