带复选框的联系人列表

Posted

技术标签:

【中文标题】带复选框的联系人列表【英文标题】:contact list with checkbox 【发布时间】:2012-06-25 13:50:49 【问题描述】:

我正在尝试创建一个联系人列表,每个联系人旁边都有一个复选框。 我找到了一个对我有很大帮助的代码,但我仍然无法让它工作。 这里是:

    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_
    android:layout_
    >


    <ListView
    android:id="@+id/android:list"
    android:layout_
    android:layout_


    />


    </LinearLayout> 

还有:

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

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

当然我在清单中得到了许可

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

谁能告诉我我做错了什么?

谢谢!!

【问题讨论】:

【参考方案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
        

异步任务

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                    
                
            );

        


【讨论】:

【参考方案2】:

代码对我来说似乎很好。我对 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/

【讨论】:

那么知道为什么我没有看到包含我的联系人的列表吗?我在模拟器中添加了几个联系人,但我看到的只是一个空白列表......

以上是关于带复选框的联系人列表的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 phonegap 作为复选框列表显示从 android 设备获取的联系人?

在颤振应用程序中仅选择一个复选框列表标题

WPF: 实现带全选复选框的列表控件

如何使整行列表视图可点击?

选中复选框时检查Android listview多个项目

C# 模糊查询带出下拉列表的问题