2012-06-23 63 views
1

我正在嘗試使用每個聯繫人旁邊的複選框創建聯繫人列表。 我發現了一個代碼,幫助了我很多,但我仍然無法使它工作。 這就是:帶複選框的聯繫人列表

import java.util.ArrayList; 
    import android.app.ListActivity; 
    import android.app.ProgressDialog; 
    import android.content.Context; 
    import android.database.Cursor; 
    import android.os.Bundle; 
    import android.provider.ContactsContract; 
    import android.util.SparseBooleanArray; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.view.View.OnClickListener; 
    import android.widget.ArrayAdapter; 
    import android.widget.CheckBox; 
    import android.widget.Toast; 

    public class ContactReadActivity extends ListActivity { 
    private ArrayList<Contact> contacts = null; 
    private ProgressDialog progressDialog = null; 
    private ContactAdapter contactAdapter = null; 
    private Runnable viewContacts = null; 
    private SparseBooleanArray selectedContacts = new SparseBooleanArray() ; 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main2); 
      contacts = new ArrayList<Contact>(); 
      this.contactAdapter = new ContactAdapter(this, R.layout.read_contacts, contacts); 
      setListAdapter(this.contactAdapter); 

      viewContacts = new Runnable(){ 
       @Override 
       public void run() { 
        getContacts(); 
       } 
      }; 
      Thread thread = new Thread(null, viewContacts, "ContactReadBackground"); 
      thread.start(); 
      progressDialog = ProgressDialog.show(ContactReadActivity.this,"Please wait...", "Retrieving contacts ...", true); 



     } 

     private void getContacts(){ 
      try{ 

       String[] projection = new String[] { 
         ContactsContract.Contacts.DISPLAY_NAME, 
         ContactsContract.Contacts.HAS_PHONE_NUMBER, 
         ContactsContract.Contacts._ID 
       }; 

       Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, projection, ContactsContract.Contacts.HAS_PHONE_NUMBER+"=?", new String[]{"1"}, ContactsContract.Contacts.DISPLAY_NAME); 


       contacts = new ArrayList<Contact>(); 

       while(cursor.moveToNext()){ 
        Contact contact = new Contact(); 


        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 


        contact.setContactName(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))); 

        contacts.add(contact); 



       } 
       cursor.close(); 



       runOnUiThread(returnRes); 
      } 
      catch(Exception e){ 
       e.printStackTrace(); 
      } 
     } 

     private Runnable returnRes = new Runnable() { 

      @Override 
      public void run() { 
      // close the progress dialog 
      if (progressDialog.isShowing()) 
      progressDialog.dismiss(); 

      contactAdapter.notifyDataSetChanged(); 
      } 
      }; 

     public class ContactAdapter extends ArrayAdapter<Contact> { 

      private ArrayList<Contact> items; 

      public ContactAdapter(Context context, int textViewResourceId, ArrayList<Contact> items) { 
        super(context, textViewResourceId, items); 
        this.items = items; 
      } 

      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
        View view = convertView; 
        if (view == null) { 
         LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         view = vi.inflate(R.layout.read_contacts, null); 
        } 
        Contact contact = items.get(position); 
        if (contact != null) { 
          CheckBox nameCheckBox = (CheckBox) view.findViewById(R.id.checkBox); 


          nameCheckBox.setChecked(selectedContacts.get(position)); 


          if (nameCheckBox != null) { 
           nameCheckBox.setText(contact.getContactName()); 
          } 

          nameCheckBox.setOnClickListener(new OnItemClickListener(position,nameCheckBox.getText(),nameCheckBox)); 
        } 

        return view; 
      } 

     } 

     class OnItemClickListener implements OnClickListener{   
      private int position; 
      private CharSequence text; 
      private CheckBox checkBox; 
      OnItemClickListener(int position, CharSequence text,CheckBox checkBox){ 
        this.position = position; 
        this.text = text; 
        this.checkBox = checkBox; 
      } 
      @Override 
      public void onClick(View arg0) { 

       selectedContacts.append(position, true); 


       Toast.makeText(getBaseContext(), "Clicked "+position +" and text "+text,Toast.LENGTH_SHORT).show(); 


      }    
     } 

     public class Contact { 
      private String contactName; 

      public String getContactName() { 
       return contactName; 
      } 
      public void setContactName(String contactName) { 
       this.contactName = contactName; 
      } 


     } 
    } 

我有兩個XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 


    <ListView 
    android:id="@+id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 


    /> 


    </LinearLayout> 

和:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal"> 

      <CheckBox android:text="" 
       android:id="@+id/checkBox" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="25sp"/> 
    </RelativeLayout> 

,當然我在清單

<uses-permission android:name="android.permission.READ_CONTACTS"/> 
許可

任何人可以電話我在做什麼我錯了?

謝謝!

回答

0

代碼對我來說似乎很好。我不太喜歡ArrayAdapter,我更喜歡使用SimpleAdapter。看完你的目的後,我覺得用一個簡單的適配器實現它似乎很容易。 Here's一個簡單的啓動。

更多的ListView,最好在那裏我想,總是對我幫助很大: http://www.vogella.com/articles/AndroidListView/article.html

更加上SimpleAdapter,它真的很簡單: http://wptrafficanalyzer.in/blog/listview-with-images-and-text-using-simple-adapter-in-android/

+0

因此,任何想法,爲什麼我沒有看到填充我的聯繫人列表? 我在模擬器中添加了幾個聯繫人,但我看到的只是一個空白列表... – user1476876

1

試着用的AsyncTask工作,並簡化實施..

活動

public class YourListActivity extends ListActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(..); 

     // DoStuff 

     ListView lv = getListView(); 
     new LoadContacts().execute(); 

     // DoStuff 
    }  
} 

的AsyncTask

class LoadContacts extends AsyncTask<String, String, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // Do Dialog stuff here 
     } 

     protected String doInBackground(String... args) { 
      // Put your implementation to retrieve contacts here 
      getContacts(); 
     } 

     protected void onPostExecute(String file_url) { 
      // Dismiss Dialog 
      runOnUiThread(new Runnable() { 
       public void run() { 
        // Create list adapter and notify it      
       } 
      }); 

     } 

} 
相關問題