我正在尝试创建一个List,在其中我使用CheckBoxes和Images获取我在Android手机中的联系人。目前我只是想通过复选框获取列表中的联系人。
代码如下:
我面临的问题: 1)无法正确创建ViewHolder
2)无法将联系人填充到适配器
3)无法从联系人中检索图像
main.java
package com.example.curs;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.provider.ContactsContract;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState );
setContentView(R.layout.activity_main);
Cursor mCursor = getContacts();
startManagingCursor(mCursor);
my_custom_adapter adapter = new my_custom_adapter(this, // Context.
android.R.layout.simple_list_item_multiple_choice, // Specify the row template
// to use (here, two
// columns bound to the
// two retrieved cursor
// rows).
mCursor, // Pass in the cursor to bind to.
// Array of cursor columns to bind to.
new String []{ ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME },
// Parallel array of which template objects to bind to those
// columns.
new int[] { android.R.id.text1,android.R.id.text2});
setListAdapter(adapter);
}
private Cursor getContacts() {
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ ("1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs,
sortOrder);
}
}
my_custom_adapter.java
package com.example.curs;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class my_custom_adapter extends SimpleCursorAdapter{
private Context context ;
//ArrayList<String> elements ;
private ArrayList<Boolean> itemChecked ;
Cursor c;
String contact_id;
String contact_name[];
String []from;
int to1;
int to2;
String elements;
public my_custom_adapter(Context context, int layout, Cursor c, String []from, int[] to) {
super(context, layout, c, from, to);
// set_checked(false);
}
/*
// AS EVERY TIME LISTVIEW INFLATE YOUR VIEWS WHEN YOU MOVE THEM SO YOU NEED TO SAVE ALL OF YOUR CHECKBOX STATES IN SOME ARRAYLIST OTHERWISE IT WILL SET ANY DEFAULT VALUE.
private void set_checked(boolean is_checked)
{
for (int i=0; i < c.getCount(); i++) {
itemChecked.add(i, is_checked);
}
}
*/
//THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
static class ViewHolder
{
public TextView tv;
public CheckBox cb;
public ImageView iv;
}
public View getView (final int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
View view = null;
if (rowView == null) {
LayoutInflater inflator = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
rowView = inflator.inflate(R.layout.inflated_layout, null);
final ViewHolder holder = new ViewHolder();
holder.cb = (CheckBox) rowView.findViewById(R.id.checkBox1);
holder.tv = (TextView) rowView.findViewById(R.id.textView1);
holder.iv = (ImageView) rowView.findViewById(R.id.imageView1);
rowView.setTag(holder);
holder.cb
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
/* Model element = (Model) viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
*/
}
});
view.setTag(holder);
holder.cb.setTag(c.getPosition());
} else {
view = convertView;
((ViewHolder) view.getTag()).cb.setTag(c.getPosition());
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.tv.setText(c.getString(c.getPosition()));
holder.iv.setImageBitmap(BitmapFactory.decodeResource(Resources.getSystem(),c.getPosition()));
//holder.cb.setChecked(c.getPosition().isSelected());
return view;
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
inflated_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="17dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/checkBox1"
android:layout_toRightOf="@+id/imageView1"
android:singleLine="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:padding="15dp"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>