Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
As you see, every item in the list view contains an image and 3 lines of text
indicating Name, Phone Number and Email Address.
Min SDK: 10
+ One layout for main screen display, which is the list view
+ One layout for each item in the list view, which will be set to list view
On the right, there are three lines of text, one is for Name, one for Mobile Number and the other for
Email Address.
Of course, we can use just one single TextView to display all three lines but in my opinion, it would be
very inconvenient when we need to reset data text to display on one of these three lines. So, it’s
better to use 3 TextView controls, one for each.
For layout display, we need one layout container for 3 lines of text and one layout container for the
ImageView and the other layout container (containing 3 TextView).
<ImageView
android:id="@+id/imgAvatar"
android:layout_width="70dip"
android:layout_height="70dip"
android:scaleType="fitCenter"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/tvName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
/>
<TextView
android:id="@+id/tvPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tvEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
2. Main Layout
Simply, just one ListView to display all items.
<ListView
android:id="@+id/listPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
package pete.android.study;
import android.graphics.Bitmap;
2. PhoneBookAdapter: the adapter used to handle data when it is changed. However, I’ve just
wanted to show how to create the custom list view, so I won’t mention about handling list item data.
package pete.android.study;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
@Override
public int getCount() {
return mListPhoneBook.size();
}
@Override
public Object getItem(int pos) {
return mListPhoneBook.get(pos);
}
@Override
public long getItemId(int pos) {
return pos;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
// get selected entry
PhoneBook entry = mListPhoneBook.get(pos);
// set avatar
ImageView ivAvatar =
(ImageView)convertView.findViewById(R.id.imgAvatar);
ivAvatar.setImageBitmap(entry.getAvatar());
// set name
TextView tvName = (TextView)convertView.findViewById(R.id.tvName);
tvName.setText(entry.getName());
// set phone
TextView tvPhone = (TextView)convertView.findViewById(R.id.tvPhone);
tvPhone.setText(entry.getPhone());
// set email
TextView tvEmail = (TextView)convertView.findViewById(R.id.tvEmail);
tvEmail.setText(entry.getEmail());
return convertView;
}
3. MainActivity: this is the entry point of application to create and display the list, which will
use “<<override>>onCreate()” method.
package pete.android.study;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvPhone = (ListView)findViewById(R.id.listPhone);
E – Note
- The image display in the ImageView is scaled by setting in the layout, which uses “dip” unit.
G – Final Words
- Did you learn something from this?
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null).build());
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID,
rawContactInsertIndex)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, telephone).build());
ops.add(ContentProviderOperation
.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID,
rawContactInsertIndex)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, nameSurname)
.build());
try {
ContentProviderResult[] res = ctx.getContentResolver()
.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
Log.d(TAG, e.getMessage());
}
}
}
try {
cr = ctx.getContentResolver();
try {
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,
null, null);
} catch (Exception ex) {
Log.i(TAG, ex.getMessage());
}
try {
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// Log.i("Names", name);
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))
> 0) {
// Query phone here. Covered next
Cursor phones = ctx
.getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + id, null, null);
while (phones.moveToNext()) {
String phoneNumberX = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// Log.i("Number", phoneNumber);
}
phones.close();
}
}
}
} catch (Exception ex) {
Log.i(TAG, ex.getMessage());
return false;
}
cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_LOOKUP_URI,
lookupKey);
ctx.getContentResolver().delete(uri, null, null);
} while (cur.moveToNext());
}
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
return false;
}
}
Using the functions in the Contact Operation
Class
Collapse | Copy Code
ContactOperations.Insert2Contacts(getApplicationContext(),"Yildirim Kocdag", "05321000000");
if (ContactOperations.isTheNumberExistsinContacts(getApplicationContext(), "05321000000")) {
Log.i(ContactOperations.TAG, "Exists");
} else {
Log.i(ContactOperations.TAG, "Not Exists");
}
ContactOperations.deleteContact(getApplicationContext(), "05321000000");
if (ContactOperations.isTheNumberExistsinContacts(getApplicationContext(), "05321000000")) {
Log.i(ContactOperations.TAG, "Exists");
} else {
Log.i(ContactOperations.TAG, "Not Exists");
}