1

我只是一个 android 编程的菜鸟,所以请不要咬我!

我一直在使用一个应用程序,您可以从手机中获取联系人列表,然后使用复选框选择其中 N 个。稍后我想使用我选择的联系人邀请他们参加聚会。正如我所说,我是新来的,但这是我到目前为止所拥有的:

主.java:

package com.example.choosephoneactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


    public class main extends Activity implements OnClickListener {
        private static final int REQUEST_CHOOSE_PHONE = 1;
        private TextView vPhone;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            vPhone = (TextView) findViewById(R.id.TextView01);
            findViewById(R.id.Button01).setOnClickListener(this);
            vPhone.setText("");
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if ((requestCode == REQUEST_CHOOSE_PHONE)
                    && (resultCode == Activity.RESULT_OK)) {
                try {
                    String phone = data.getStringExtra("phone");
                    vPhone.setText(phone);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        public void onClick(View v) {
            Intent intent = new Intent("org.francho.CHOOSE_PHONE");
            startActivityForResult(intent, REQUEST_CHOOSE_PHONE);
        }
    }

List.java(从手机获取联系人列表的 java 文件)

package com.example.choosephoneactivity;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.CheckBox;
import android.widget.SimpleCursorAdapter;



public class list extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        setTitle("Choose a phone");

        // Query: contacts with phone shorted by name
        Cursor mCursor = getContentResolver().query(
                Data.CONTENT_URI,
                new String[] { Data._ID, Data.DISPLAY_NAME, Phone.NUMBER,
                        Phone.TYPE },
                Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "' AND "
                        + Phone.NUMBER + " IS NOT NULL", null,
                Data.DISPLAY_NAME + " ASC");

        startManagingCursor(mCursor);

        // Setup the list
        ListAdapter adapter = new SimpleCursorAdapter(this, // context
                android.R.layout.simple_list_item_2, // Layout for the rows
                mCursor, // cursor
                new String[] { Data.DISPLAY_NAME, Phone.NUMBER }, // cursor
                                                                    // fields
                new int[] { android.R.id.text1, android.R.id.text2 } // view
                                                                        // fields
        );
        setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent result = new Intent();

        // Get the data
        Cursor c = (Cursor) getListAdapter().getItem(position);
        int colIdx = c.getColumnIndex(Phone.NUMBER);
        String phone = c.getString(colIdx);

        // Save the phone to return it to the caller
        result.putExtra("phone", phone);
        setResult(Activity.RESULT_OK, result);

        // Close this activity (return to caller)
        finish();
    }
}

主要的.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:gravity="center_vertical|center_horizontal">

<TextView 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="@+id/TextView01" 
    android:id="@+id/TextView01">
    /</TextView>

<Button 
    android:id="@+id/Button01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Invitar a Convivencia">

</Button>

</LinearLayout>

列表.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"    />

<TextView
    android:id="@android:id/empty"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="no se han encontrado registros"  />

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

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

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

我知道,这有点“混乱”,但我不知道该怎么做。

现在的代码是从电话中获取联系人,但我想在列出的每个联系人旁边放置一个复选框以选择它们,并添加到“邀请列表”或类似的东西。

非常感谢小伙伴们!

4

1 回答 1

0

如何使用复选框创建自定义联系人列表以从 android 列表中选择多个联系人

public class AddFromContacts extends Activity {
ArrayList<String> listname;
// ArrayList<String> list_no;
Context context;
LayoutInflater inflater;
ListView contactlistView;
String name;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.show);

contactlistView = (ListView) findViewById(R.id.ContactlistView);

listname = new ArrayList<String>();
// list_no = new ArrayList<String>();

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
        null, null, null);
if (cur.getCount() > 0) {
    while (cur.moveToNext()) {
        String id = cur.getString(cur
                .getColumnIndex(ContactsContract.Contacts._ID));

        String name = cur
                .getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        if (Integer
                .parseInt(cur.getString(cur
                        .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

            Cursor pCur = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                            + " = ?", new String[] { id }, null);
            while (pCur.moveToNext()) {
                // Do something with phones
                String phoneNo = pCur
                        .getString(pCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                listname.add(name + "\n" + phoneNo);

                // list_no.add(phoneNo);

            }
            pCur.close();
        }
    }

}
contactlistView.setAdapter(new Contact(this));
}

class Contact extends BaseAdapter {
Context myContext;

public Contact(AddFromContacts contactActivity) {
    // TODO Auto-generated constructor stub
    this.myContext = contactActivity;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return listname.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if (convertView == null) {

        inflater = (LayoutInflater) myContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inflater.inflate(R.layout.checkbox, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.text_name = (TextView) convertView
                .findViewById(R.id.name);
        viewHolder.id = (TextView) convertView.findViewById(R.id.id);
        viewHolder.checkBox = (CheckBox) convertView
                .findViewById(R.id.checkBox);

        convertView.setTag(viewHolder);
    }

    final ViewHolder holder = (ViewHolder) convertView.getTag();
    // holder.text_name.setText(list_no.get(position));
    holder.id.setText(listname.get(position));

    if (holder != null) {
        holder.checkBox
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    public void onCheckedChanged(
                            CompoundButton buttonView, boolean isChecked) {
                        // TODO Auto-generated method stub

                        // Toast.makeText(myContext, name + "     Selected",
                        // Toast.LENGTH_LONG).show();
                    }
                });
    }
    return convertView;
}
}

class ViewHolder {
TextView text_name, id;
CheckBox checkBox;
    // EditText search;
}
}

但是在从列表中选择联系人时,我遇到了这个问题,它从列表中选择了我没有选择的那些联系人

于 2013-09-04T04:39:04.323 回答