-
안드로이드 평번한예제2과거...../개발일지 2010. 6. 2. 12:37
1) 이름, 전화번호(1개)만 읽으면 됨.(ContentProvider이용하면됨)2) 이름, 전화번호는 menu를 만들어 이름만 나오는 List, 전화번호만 나오는 List가 따로 나오도록 구현(Option menu사용)3) List의 Item을 Click했을 때 Click된 item의 전화번호로 실제 전화연결되도록 구현(Intent 사용)
package com.homework2;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;public class MainActivity extends Activity {
ListView listView01;
ListAdapter adapter;
Cursor c;
String[] projection;public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView01 = (ListView) findViewById(R.id.ListView01);
// defalt_value setting
menu_change(1);listView01.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
if (c.moveToPosition(position)) {
Intent call_pass = new Intent(Intent.ACTION_CALL, Uri
.parse("tel:"
+ c.getString(
c.getColumnIndex(Phone.NUMBER))
.toString()));
startActivity(call_pass);
}
}
});
}
public boolean menu_change(int change_number) {
projection = new String[] { Data._ID, Data.DISPLAY_NAME, Phone.NUMBER,
Phone.TYPE };
c = getContentResolver().query(Phone.CONTENT_URI, projection,
Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", null,
null);
//startManagingCursor(c);
switch (change_number) {
case 1:
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[] { Data.DISPLAY_NAME }, new int[] { R.id.text1 });
listView01.setAdapter(adapter);
return true;
case 2:
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[] { Phone.NUMBER }, new int[] { R.id.text1 });
listView01.setAdapter(adapter);
return true;
}
return false;
}public boolean onCreateOptionsMenu(Menu menu) {
myMenu(menu);
return super.onCreateOptionsMenu(menu);
}private void myMenu(Menu menu) {
menu.add(Menu.NONE, 0, Menu.NONE, "CallName");
menu.add(Menu.NONE, 1, Menu.NONE, "CallNumber");
}public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case 0:
menu_change(1);
return true;
case 1:
menu_change(2);
return true;
}
return false;
}protected void onDestroy() {
super.onDestroy();
}
}