2012-06-30 52 views
0

美好的一天,請一直有這個麻煩,我似乎無法看到爲什麼。基本上,我試圖做的是通過聯繫人api選擇聯繫人,保存在數據庫中,然後顯示在列表視圖中。但由於某種原因,在獲取聯繫人詳細信息後,我的自定義適配器不再被調用,它只在首次創建活動時調用,因此爲空。請問我能錯過什麼。謝謝。從數據庫中保存的聯繫人更新Listview的問題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); 
    } 

} 

我不是從數據庫中獲取的任何問題,所以這裏不添加代碼。

+0

有想法的人嗎?或看到任何有點奇怪。我對此很困惑。 – irobotxxx

+0

您是否獲得了if(contactsCursor.moveToFirst()){?可能在保存與數據庫的聯繫之後您需要重新啓動加載程序()以獲取您的數據庫查詢,並讓onLoadFinished再次被調用以允許適配器使用新數據, – sandrstar

+0

@sandrstar,是的。會試一試,讓你知道。謝謝 – irobotxxx

回答

1

保存後聯繫數據庫您需要重新啓動加載器()才能獲得您的數據庫重新查詢,並讓onLoadFinished再次被調用以允許適配器使用新數據。

0

我會努力扭轉這兩條線(在我看來像你說的是列表更新光標之前自行更新):

cursoradapter.notifyDataSetChanged(); 
    getSupportLoaderManager().getLoader(0).onContentChanged(); 

要這樣:

getSupportLoaderManager().getLoader(0).onContentChanged(); 
    cursoradapter.notifyDataSetChanged(); 
相關問題