我只是一个 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>
我知道,这有点“混乱”,但我不知道该怎么做。
现在的代码是从电话中获取联系人,但我想在列出的每个联系人旁边放置一个复选框以选择它们,并添加到“邀请列表”或类似的东西。
非常感谢小伙伴们!