以下代码帮助我将所有联系人检索到列表中的列表视图中 活动,但是我想从中获取选定的联系人行 用户将其传递到意图中,但我不确定该怎么做。
示例:用户选择了Suzanne的联系人,我希望能够将“ Suzanne” 名称和数字保存在字符串中并传递给 意向
公共类SendWhoosh_SelectContact扩展了ListActivity {
private static final String TAG = "SendWhoosh";
ListView listView;
Cursor cursor;
@Override
public long getSelectedItemId()
{
// TODO Auto-generated method stub
return super.getSelectedItemId();
}
@Override
public int getSelectedItemPosition()
{
// TODO Auto-generated method stub
return super.getSelectedItemPosition();
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_whoosh_select_contact);
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
startManagingCursor(cursor);
String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID};
int[] to = {android.R.id.text1, android.R.id.text2};
SimpleCursorAdapter listadapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, from, to);
setListAdapter(listadapter);
listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String phone = ContactsContract.CommonDataKinds.Phone.NUMBER;
Log.d(TAG, "SendWhoosh: phone " + phone);
Intent intent = new Intent(SendWhoosh_SelectContact.this, EnterWhooshAmount.class);;
intent.putExtra("Name", name);
intent.putExtra("Number", phone);
startActivity(intent);
}
});
}
----------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/whooshbg"
android:id="@+id/activitywhoosh_screenarea"
tools:context=".SendWhoosh_SelectContact">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/li1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:src="@drawable/back"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp"
android:layout_marginTop="15dp"
android:textColor="@color/white"
android:text="Whoosh to "/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:0)
设置您的onClickListener()并按如下所示启动您的意图;
<div id="maindiv">
<h2 id="signup">Free enrollments end in <img id="myimg" src="https://image.flaticon.com/icons/svg/69/69637.svg"> days!</h2>
<form id="myform">
<label class="labels" for="fname">Firstname</label><br/><input class="inputs" type="text" id="fname"><img id="check1" class="imgs" src=""><br/><br/>
<label class="labels" for="lname">Surname</label><br/><input class="inputs" type="text" id="lname"><img id="check2" class="imgs" src=""><br/><br/>
<label class="labels" for="email">Email</label><br/><input class="inputs" type="text" id="email"><img id="check3" class="imgs" src=""><br/><br/>
<label class="labels" for="pword">Create password</label><br/><input class="inputs" type="password" id="pword"><img id="check4" class="imgs" src=""><br/><br/>
<label class="labels" for="pword2">Confirm password</label><br/><input class="inputs" type="password" id="pword2"><img id="check5" class="imgs" src=""><br/><br/>
<button id="mybtn">Enroll</button>
</form>
</div>
进入EnterWhooshAmount类时,您将获得意图并检索单击的位置,并在此位置下可以检索名称等。可以在EnterWhooshAmount类中获得意图,如下所示;
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
launchAnotherActivity(position);
}
});
}
private void launchAnotherActivity(int position) {
Intent intent = new Intent(this, EnterWhooshAmount.class);
intent.putExtra("The_Position", position);
startActivity(intent);
}
所以,位置是您要查找的整数,因此,您可以在保存该位置的位置检索名称等
答案 1 :(得分:0)
listitem onclick侦听器更改此代码即可获取点击项的名称和编号。
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = ((TwoLineListItem) view).getText1().getText().toString();
String number = ((TwoLineListItem) view).getText2().getText().toString();
Log.d(TAG, "SendWhoosh: phone " + name);
Intent intent = new Intent(SendWhoosh_SelectContact.this, EnterWhooshAmount.class);;
intent.putExtra("Name", name);
intent.putExtra("Number", number);
startActivity(intent);
}
});