Assinging The Values Of Cursor To Object
I'm working on showing list of contacts with checkbox. I tried implementing my own cursor adapter and it is checkable. But once I check that value I want the contacts to be stored
Solution 1:
Check this code. It worked for me. Best of luck.
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
publicclassContactListActivityextendsActivity {
ListView list;
ArrayList<ContactPerson> selectedList;
Set<String> phoneID= newTreeSet<String>();;
Button doneButton;
CheckBox cb;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_list);
selectedList = newArrayList<ContactPerson>();
list = (ListView) findViewById(R.id.ListView2);
doneButton = (Button) findViewById(R.id.selected_done_1);
Cursor cur= getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null,null);
list.setAdapter(newcontactAdapter(getApplicationContext(), cur));
doneButton.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stubfor (String ids : phoneID) {
Log.d("PhoneIds", ids);
Cursor cursorPhoneNumber = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
newString[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
newString[]{ids},
null);
Log.d("Number cursor", String.valueOf(cursorPhoneNumber.getCount()));
if (cursorPhoneNumber.moveToFirst()) {
// phoneNumber = cursorPhoneNumber.getString(cursorPhoneNumber.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));Log.d("Phone number", cursorPhoneNumber.getString(cursorPhoneNumber.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
cursorPhoneNumber.close();
Cursor cursorName = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
newString[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
newString[]{ids},
null);
Log.d("Name cursor", String.valueOf(cursorName.getCount()));
if (cursorName.moveToFirst()) {
Log.d("Phone number", cursorName.getString(cursorName.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY)));
// Name = cursorName.getString(cursorName.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY));
}
cursorName.close();
// selectedList.add(new ContactPerson(Name,phoneNumber));
}
}
});
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.contact_list, menu);
returntrue;
}
privateclassViewHolder{
TextViewContactName;
CheckBox contactCheck;
}
classcontactAdapterextendsCursorAdapter{
StringName, phoneNumber;
privateCursor cursor;
privateContext ccontext;
privateLayoutInflater inflater;
publiccontactAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
cursor = c;
ccontext = context;
inflater = LayoutInflater.from(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@OverridepublicvoidbindView(View view, Context arg1, Cursor arg2) {
// TODO Auto-generated method stubViewHolder holder = (ViewHolder) view.getTag();
if (holder == null) {
holder = newViewHolder();
holder.ContactName = (TextView) view.findViewById(R.id.contact_name);
holder.contactCheck = (CheckBox) view.findViewById(R.id.contact_check);
view.setTag(holder);
holder.contactCheck.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stub
cb = (CheckBox) v;
cb.setChecked(cb.isChecked());
// ContactPerson selected = (ContactPerson)cb.getTag();Log.d("selcted", cb.getTag().toString());
if(cb.isChecked()){
phoneID.add(cb.getTag().toString());
}
}
});
}else{
holder = (ViewHolder) view.getTag();
}
holder.ContactName.setText(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
holder.contactCheck.setTag(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)));
holder.contactCheck.setChecked(false);
}
@OverridepublicViewnewView(Context arg0, Cursor arg1, ViewGroup arg2) {
// TODO Auto-generated method stubreturn inflater.inflate(R.layout.checkbox_item, arg2, false);
}
@OverridepublicViewgetView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub// Log.d("don't know", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));returnsuper.getView(arg0, arg1, arg2);
}
}
}
Solution 2:
Here i changed little bit code of your adapter and activity. just look at it:
publicclassMainActivityextendsActivity {
private ArrayList<ContactPerson> arr;
private Context context;
private ListView list;
private ContactArrayAdapter adapter;
private String strName,strNumber;
private View view;
publicstaticboolean[] arrBoolean = null;
@OverridepublicvoidonCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_main);
ProgressDialogpd=newProgressDialog(this);
list = (ListView)findViewById(R.id.ListView2);
arr = newArrayList<ContactPerson>();
context = MainActivity.this;
arr = displayContacts();
adapter = newContactArrayAdapter(this, R.layout.checkbox_item, arr);
list.setAdapter(adapter);
}
public ArrayList<ContactPerson> displayContacts() {
ContentResolvercr= getContentResolver();
Cursorcur= cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
ContactPerson contact;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
contact = newContactPerson();
Stringid= cur.getString(cur.getColumnIndex(People._ID));
Stringname= cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));
ContactPersoncp=newContactPerson();
cp.setName(name);
cp.setNumber(id);
cp.setSelected(false);
arr.add(cp);
}
}
return arr;
}
//********* Adapter *****publicclassContactArrayAdapterextendsArrayAdapter<ContactPerson> {
// private final List<Contact> list;private Context context;
private LayoutInflater mInflater;
private List<ContactPerson> list;
publicContactArrayAdapter(Context context, int textViewResourceId,
ArrayList<ContactPerson> stateList) {
super(context, textViewResourceId, stateList);
this.list = newArrayList<ContactPerson>();
this.list.addAll(stateList);
}
classViewHolder {
TextView text;
CheckBox checkbox;
}
public View getView(int position, View convertView, ViewGroup parent) {
Viewview= convertView;
ViewHolderviewHolder=null;
if (view == null) {
LayoutInflatervi= (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.checkbox_item, null);
viewHolder = newViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.contact_name);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.contact_check);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
finalintpos= position;
ContactPersoncp= list.get(position);
viewHolder.text.setText(cp.getName());
viewHolder.checkbox.setChecked(cp.isSelected());
viewHolder.checkbox.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View arg0) {
// TODO Auto-generated method stubCheckBoxcb= (CheckBox) arg0 ;
list.get(pos).setSelected(cb.isChecked());
System.out.println(cb.isChecked());
System.out.println(list.get(pos).getName());
}
});
//viewHolder.text.setTag(cp);return view;
}
}
}
Post a Comment for "Assinging The Values Of Cursor To Object"