设置按钮单击“值”到Android中的ListView

时间:2014-07-13 13:37:37

标签: android listview android-contacts

我一直在寻找一个解决方案一个星期,但找不到任何可以帮助我解决问题的方法。我有一个按钮,可让您从手机上的联系人列表中选择联系人 我拥有的问题是我希望将选定的联系人填充到列表视图中,然后通过选择另一个联系人再次重复,然后再次将联系人放在列表视图下以创建运行列表。此列表将用于发送所选联系人的群发电子邮件。

任何方向都会很棒。感谢。

这是我到目前为止所拥有的......

xml layout

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:id="@+id/selectGuestListButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/selectGuestList"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/selectGuestListButton"
    android:id="@+id/listView">
    </ListView>

我的代码

 public class GuestListFragment extends Fragment {

private Event mEvent;

private Button mEventGuestListButton;

private static final int REQUEST_CONTACT = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mEvent = new Event();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_CONTACT) {
        Uri contactUri = data.getData();

        //specify which fields you want to query to return values for
        String[] queryFields = new String[]{
                ContactsContract.Contacts.DISPLAY_NAME,

        };

        //perform your query
        Cursor c = getActivity().getContentResolver().query(contactUri, queryFields, null, null, null);

        //double check you get results
        if (c.getCount() == 0) {
            c.close();
            return;
        }

        //pull out first column of the first row of data
        c.moveToFirst();
        String guest = c.getString(0);
        mEvent.setGuest(guest);
        mEventGuestListButton.setText(guest);

        c.close();
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.guest_list, parent, false);


    //guest list button
    mEventGuestListButton = (Button)v.findViewById(R.id.selectGuestListButton);
    mEventGuestListButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(i, REQUEST_CONTACT);
        }
    });

    if (mEvent.getGuest() != null){
        mEventGuestListButton.setText(mEvent.getGuest());
    }


    return v;

}

}

0 个答案:

没有答案