MainActivity
package com.example.baseadapter;import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ListView;
public class MainActivity extends Activity {
List<String> names=new ArrayList<String>();
List<String> phoneNumber=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
readAllContacts();
}
private void readAllContacts() {
Context ctx=getApplicationContext();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
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));
names.add(name);
System.out.println("Name" + name);
int hasPhoneNumber = Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
// Query and loop for every phone number of the contact
Cursor phoneCursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
phoneCursor.moveToNext();
String phomber = phoneCursor
.getString(phoneCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumber.add(phomber);
System.out.println(" Phone number:" + phoneNumber);
//phoneCursor.close();
}
else {
phoneNumber.add("No Number");
System.out.println("No Number");
}
}
}
BaseAdp adp=new BaseAdp(names, phoneNumber, getApplicationContext());
ListView lst=(ListView)findViewById(R.id.listView1);
lst.setAdapter(adp);
}
}
Baseadapter Class
package com.example.baseadapter;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class BaseAdp extends BaseAdapter {
List<String> names;
List<String> phones;
Context ctx;
public BaseAdp(List<String> name, List<String> phn, Context cnt) {
names=name;
phones=phn;
ctx=cnt;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return phones.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflate=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflate.inflate(R.layout.listitem, null);
TextView txtName=(TextView)convertView.findViewById(R.id.textView1);
TextView txtPhone=(TextView)convertView.findViewById(R.id.textView2);
txtName.setText(names.get(position));
txtPhone.setText(phones.get(position));
Button btn=(Button)convertView.findViewById(R.id.button2);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ctx, position+"", Toast.LENGTH_SHORT).show();
names.remove(position);
phones.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
}