Guest User

Untitled

a guest
Sep 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. Problems updating Listview from Saved contact in database android
  2. public class GroupDetails extends FragmentActivity implements OnClickListener, LoaderCallbacks<Cursor> {
  3.  
  4. //variable for debugging the application
  5.  
  6. public static final String TAG = "MyApp.Debug";
  7.  
  8. //request code for using with action pick intent
  9. static final int PICK_CONTACT = 1;
  10.  
  11. //initial variables
  12. Button add_button;
  13. TextView label;
  14. ListView list;
  15. ResponderDB dbadapter;
  16. DueCustomCursorAdapter cursoradapter;
  17. //DueCustomCursorLoader loader;
  18.  
  19. //cursor to retrieve contact details
  20. private Cursor contactsCursor;
  21.  
  22.  
  23. String groupname;
  24. long rowId;
  25.  
  26.  
  27. public void onCreate(Bundle savedInstanceState){
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.group_detail_list);
  30.  
  31. //only intialize here. database will be open in custom cursor loader
  32.  
  33. dbadapter = new ResponderDB(this);
  34.  
  35. getSupportLoaderManager().initLoader(0, null, this);
  36. /*Read intent and the extras passed*/
  37. Bundle extra = getIntent().getExtras();
  38.  
  39. if(!extra.isEmpty() || extra.equals(null)){
  40. groupname = extra.getString("group_name");
  41. rowId = extra.getLong("rowId");
  42. }
  43.  
  44. list = (ListView)findViewById(android.R.id.list);
  45. add_button = (Button)findViewById(R.id.add_button_id);
  46. label = (TextView)findViewById(R.id.group_label_id);
  47.  
  48.  
  49. Log.d(TAG, "calling custom adapter here now");
  50. cursoradapter = new DueCustomCursorAdapter(GroupDetails.this, null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER );
  51. list.setAdapter(cursoradapter);
  52.  
  53. add_button.setOnClickListener(this);
  54.  
  55. }
  56.  
  57.  
  58. @Override
  59. public void onClick(View view) {
  60. int selection = view.getId();
  61. if(selection == R.id.add_button_id){
  62.  
  63. Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
  64. startActivityForResult(intent, PICK_CONTACT);
  65. }
  66.  
  67. }
  68.  
  69. @Override
  70. public void onActivityResult(int requestCode, int resultCode, Intent data){
  71. if(requestCode == PICK_CONTACT){
  72. getContactInfo(data);
  73. }
  74. }
  75.  
  76.  
  77. private void getContactInfo(Intent intent) {
  78.  
  79. String number = null;
  80. String name = null;
  81.  
  82. String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
  83. ContactsContract.CommonDataKinds.Phone.NUMBER};
  84.  
  85. CursorLoader loader = new CursorLoader(this,intent.getData(),
  86. null,null,null,null);
  87.  
  88. contactsCursor = loader.loadInBackground();
  89. if(contactsCursor.moveToFirst()){
  90.  
  91. String id = contactsCursor.getString(contactsCursor.getColumnIndex(ContactsContract.Contacts._ID));
  92. name = contactsCursor.getString(contactsCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
  93.  
  94. //get the Phone Number
  95. Cursor numberCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI
  96. , null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] {id}, null);
  97. while(numberCursor.moveToNext()){
  98. number = numberCursor.getString(numberCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  99. }
  100.  
  101. }
  102.  
  103. Log.d(TAG, "Successfully added contacts for group");
  104. //dbadapter.updateGroup(rowId, values);
  105.  
  106. dbadapter.saveContacts(name, number, String.valueOf(rowId));
  107. cursoradapter.notifyDataSetChanged();
  108. getSupportLoaderManager().getLoader(0).onContentChanged();
  109. }
  110.  
  111.  
  112. public static final class DueCustomCursorLoader extends SimpleCursorLoader {
  113. public static final String TAG = "MyApp.Debug";
  114. public static int RETRIEVE_CODE = 1;
  115.  
  116. ResponderDB dbadapter1;
  117. int retrieveCode;
  118.  
  119.  
  120. public DueCustomCursorLoader(Context context, ResponderDB dbadapter) {
  121. super(context);
  122. this.dbadapter1= dbadapter;
  123.  
  124. }
  125.  
  126. public DueCustomCursorLoader(Context context, ResponderDB dbadapter, int retrieveCode){
  127.  
  128. super(context);
  129. this.dbadapter1 = dbadapter;
  130. this.retrieveCode = retrieveCode;
  131. }
  132.  
  133.  
  134.  
  135. @Override
  136. public Cursor loadInBackground() {
  137. Cursor cursor = null;
  138.  
  139. dbadapter1.open();
  140.  
  141. cursor = dbadapter1.readContact(retrieveCode);
  142.  
  143. return cursor;
  144. }
  145.  
  146. }
  147.  
  148. public class DueCustomCursorAdapter extends CursorAdapter {
  149. public static final String TAG = "SmsResponder.Debug";
  150. private Context myContext;
  151.  
  152.  
  153.  
  154. public DueCustomCursorAdapter(Context context,Cursor c, int flags) {
  155. super(context, c, flags);
  156. myContext = context;
  157.  
  158. }
  159.  
  160. //never seem to get here
  161. @Override
  162. public void bindView(View view, Context context, Cursor cursor) {
  163.  
  164. ViewHolder holder = (ViewHolder)view.getTag();
  165. String contactName = cursor.getString(cursor.getColumnIndexOrThrow(ResponderDB.NAME));
  166. String contactNumber = cursor.getString(cursor.getColumnIndex(ResponderDB.NUMBER));
  167.  
  168. Log.d(TAG, "contact name is " + contactName);
  169. Log.d(TAG, "contact number is " + contactNumber);
  170.  
  171. holder.contact_name.setText(contactName);
  172. holder.contact_number.setText(contactNumber);
  173. }
  174.  
  175. @Override
  176. public View newView(Context context, Cursor cursor, ViewGroup parent) {
  177. ViewHolder holder = new ViewHolder();
  178.  
  179. View view = LayoutInflater.from(myContext).inflate(R.layout.group_detail_item, parent,false);
  180. holder.contact_name = (TextView)view.findViewById(R.id.group_item_id);
  181. holder.contact_number = (TextView)view.findViewById(R.id.group_subitem_id);
  182. view.setTag(holder);
  183.  
  184. return view;
  185. }
  186.  
  187. }
  188.  
  189. static class ViewHolder {
  190. TextView text;
  191. TextView contact_name;
  192. TextView contact_number;
  193. CheckBox checkbox;
  194. }
  195.  
  196. @Override
  197. public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {
  198.  
  199. return new DueCustomCursorLoader(GroupDetails.this, dbadapter, (int)rowId);
  200. }
  201.  
  202.  
  203. @Override
  204. public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
  205. cursoradapter.swapCursor(data);
  206.  
  207. }
  208.  
  209.  
  210. @Override
  211. public void onLoaderReset(Loader<Cursor> arg0) {
  212.  
  213. cursoradapter.swapCursor(null);
  214. }
  215.  
  216. }
  217.  
  218. cursoradapter.notifyDataSetChanged();
  219. getSupportLoaderManager().getLoader(0).onContentChanged();
  220.  
  221. getSupportLoaderManager().getLoader(0).onContentChanged();
  222. cursoradapter.notifyDataSetChanged();
Add Comment
Please, Sign In to add comment