Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Problems updating Listview from Saved contact in database android
- public class GroupDetails extends FragmentActivity implements OnClickListener, LoaderCallbacks<Cursor> {
- //variable for debugging the application
- public static final String TAG = "MyApp.Debug";
- //request code for using with action pick intent
- static final int PICK_CONTACT = 1;
- //initial variables
- Button add_button;
- TextView label;
- ListView list;
- ResponderDB dbadapter;
- DueCustomCursorAdapter cursoradapter;
- //DueCustomCursorLoader loader;
- //cursor to retrieve contact details
- private Cursor contactsCursor;
- String groupname;
- long rowId;
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.group_detail_list);
- //only intialize here. database will be open in custom cursor loader
- dbadapter = new ResponderDB(this);
- getSupportLoaderManager().initLoader(0, null, this);
- /*Read intent and the extras passed*/
- Bundle extra = getIntent().getExtras();
- if(!extra.isEmpty() || extra.equals(null)){
- groupname = extra.getString("group_name");
- rowId = extra.getLong("rowId");
- }
- list = (ListView)findViewById(android.R.id.list);
- add_button = (Button)findViewById(R.id.add_button_id);
- label = (TextView)findViewById(R.id.group_label_id);
- Log.d(TAG, "calling custom adapter here now");
- cursoradapter = new DueCustomCursorAdapter(GroupDetails.this, null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER );
- list.setAdapter(cursoradapter);
- add_button.setOnClickListener(this);
- }
- @Override
- public void onClick(View view) {
- int selection = view.getId();
- if(selection == R.id.add_button_id){
- Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
- startActivityForResult(intent, PICK_CONTACT);
- }
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data){
- if(requestCode == PICK_CONTACT){
- getContactInfo(data);
- }
- }
- private void getContactInfo(Intent intent) {
- String number = null;
- String name = null;
- String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
- ContactsContract.CommonDataKinds.Phone.NUMBER};
- CursorLoader loader = new CursorLoader(this,intent.getData(),
- null,null,null,null);
- contactsCursor = loader.loadInBackground();
- if(contactsCursor.moveToFirst()){
- String id = contactsCursor.getString(contactsCursor.getColumnIndex(ContactsContract.Contacts._ID));
- name = contactsCursor.getString(contactsCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
- //get the Phone Number
- Cursor numberCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI
- , null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] {id}, null);
- while(numberCursor.moveToNext()){
- number = numberCursor.getString(numberCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
- }
- }
- Log.d(TAG, "Successfully added contacts for group");
- //dbadapter.updateGroup(rowId, values);
- dbadapter.saveContacts(name, number, String.valueOf(rowId));
- cursoradapter.notifyDataSetChanged();
- getSupportLoaderManager().getLoader(0).onContentChanged();
- }
- public static final class DueCustomCursorLoader extends SimpleCursorLoader {
- public static final String TAG = "MyApp.Debug";
- public static int RETRIEVE_CODE = 1;
- ResponderDB dbadapter1;
- int retrieveCode;
- public DueCustomCursorLoader(Context context, ResponderDB dbadapter) {
- super(context);
- this.dbadapter1= dbadapter;
- }
- public DueCustomCursorLoader(Context context, ResponderDB dbadapter, int retrieveCode){
- super(context);
- this.dbadapter1 = dbadapter;
- this.retrieveCode = retrieveCode;
- }
- @Override
- public Cursor loadInBackground() {
- Cursor cursor = null;
- dbadapter1.open();
- cursor = dbadapter1.readContact(retrieveCode);
- return cursor;
- }
- }
- public class DueCustomCursorAdapter extends CursorAdapter {
- public static final String TAG = "SmsResponder.Debug";
- private Context myContext;
- public DueCustomCursorAdapter(Context context,Cursor c, int flags) {
- super(context, c, flags);
- myContext = context;
- }
- //never seem to get here
- @Override
- public void bindView(View view, Context context, Cursor cursor) {
- ViewHolder holder = (ViewHolder)view.getTag();
- String contactName = cursor.getString(cursor.getColumnIndexOrThrow(ResponderDB.NAME));
- String contactNumber = cursor.getString(cursor.getColumnIndex(ResponderDB.NUMBER));
- Log.d(TAG, "contact name is " + contactName);
- Log.d(TAG, "contact number is " + contactNumber);
- holder.contact_name.setText(contactName);
- holder.contact_number.setText(contactNumber);
- }
- @Override
- public View newView(Context context, Cursor cursor, ViewGroup parent) {
- ViewHolder holder = new ViewHolder();
- View view = LayoutInflater.from(myContext).inflate(R.layout.group_detail_item, parent,false);
- holder.contact_name = (TextView)view.findViewById(R.id.group_item_id);
- holder.contact_number = (TextView)view.findViewById(R.id.group_subitem_id);
- view.setTag(holder);
- return view;
- }
- }
- static class ViewHolder {
- TextView text;
- TextView contact_name;
- TextView contact_number;
- CheckBox checkbox;
- }
- @Override
- public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {
- return new DueCustomCursorLoader(GroupDetails.this, dbadapter, (int)rowId);
- }
- @Override
- public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
- cursoradapter.swapCursor(data);
- }
- @Override
- public void onLoaderReset(Loader<Cursor> arg0) {
- cursoradapter.swapCursor(null);
- }
- }
- cursoradapter.notifyDataSetChanged();
- getSupportLoaderManager().getLoader(0).onContentChanged();
- getSupportLoaderManager().getLoader(0).onContentChanged();
- cursoradapter.notifyDataSetChanged();
Add Comment
Please, Sign In to add comment