1
我只是個菜鳥,所以不要咬我請!在聯繫人列表中添加複選框,在創建機器人編程的「邀請列表」
我在一個應用程序,你可以從你的手機聯繫人列表,然後使用複選框選擇其中N個一直在努力。之後,我想使用我選擇的聯繫人邀請他們參加派對。正如我所說的這個即時通訊新的,但是這是我到目前爲止有:
Main.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();
}
}
main.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>
List.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>
我知道,這是一個有點「亂」,但我不知道如何做到這一點。
什麼代碼現在所做的是從手機獲得接觸,但我希望把旁邊的複選框以選擇它們列出的每個接觸,並添加到「邀請列表」或者類似的東西。
非常感謝隊友!