在android中复制列表项

时间:2015-11-15 06:56:15

标签: android listview

我找到了一个示例,其中显示了列表视图中的所有联系人,并允许您调用点击的联系人,但我想在剪贴板上复制联系号码并在列表项目时显示祝酒词地选择。

public class ContactListActivity extends Activity implements
        OnItemClickListener {

    private ListView listView;
    private List<ContactBean> list = new ArrayList<ContactBean>();

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

        listView = (ListView) findViewById(R.id.list);
        listView.setOnItemClickListener(this);

        Cursor phones = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                null, null);
        while (phones.moveToNext()) {

            String name = phones
                    .getString(phones
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

            String phoneNumber = phones
                    .getString(phones
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            ContactBean objContact = new ContactBean();
            objContact.setName(name);
            objContact.setPhoneNo(phoneNumber);
            list.add(objContact);

        }
        phones.close();

        ContanctAdapter objAdapter = new ContanctAdapter(
                ContactListActivity.this, R.layout.alluser_row, list);
        listView.setAdapter(objAdapter);

        if (null != list && list.size() != 0) {
            Collections.sort(list, new Comparator<ContactBean>() {

                @Override
                public int compare(ContactBean lhs, ContactBean rhs) {
                    return lhs.getName().compareTo(rhs.getName());
                }
            });
            AlertDialog alert = new AlertDialog.Builder(
                    ContactListActivity.this).create();
            alert.setTitle("");

            alert.setMessage(list.size() + " Contacts Found");

            alert.setButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            alert.show();

        } else {
            showToast("No Contacts Found");
        }
    }

    private void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemClick(AdapterView<?> listview, View v, int position,
            long id) {
        ContactBean bean = (ContactBean) listview.getItemAtPosition(position);
        showCallDialog(bean.getName(), bean.getPhoneNo());
    }

    private void showCallDialog(String name, final String phoneNo) {
        AlertDialog alert = new AlertDialog.Builder(ContactListActivity.this)
                .create();
        alert.setTitle("Call?");

        alert.setMessage("Are you sure want to call " + name + " ?");

        alert.setButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alert.setButton2("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                String phoneNumber = "tel:" + phoneNo;
                Intent intent = new Intent(Intent.ACTION_CALL, Uri
                        .parse(phoneNumber));
                startActivity(intent);
            }
        });
        alert.show();
    }
}

2 个答案:

答案 0 :(得分:0)

BaseAdapter

答案 1 :(得分:0)

添加:

public void doCopy(String text) {
    try {
        if(android.os.Build.VERSION.SDK_INT < 11) {
            android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.setText(text);
        } else {
            android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
            android.content.ClipData clip = android.content.ClipData.newPlainText("WordKeeper", text);
            clipboard.setPrimaryClip(clip);
        }
        Toast.makeText(getApplicationContext(), "Text copied to clipboard!", Toast.LENGTH_SHORT);
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Error copying text to clipboard", Toast.LENGTH_SHORT);
    }
}

并改变这一点:

@Override
public void onItemClick(AdapterView<?> listview, View v, int position, long id) {
    ContactBean bean = (ContactBean) listview.getItemAtPosition(position);
    showCallDialog(bean.getName(), bean.getPhoneNo());
}

为:

@Override
public void onItemClick(AdapterView<?> listview, View v, int position, long id) {
    doCopy("" + bean.getPhoneNo());
}