這裏我使用的是內容提供商獲得的所有聯繫人,並把它與多選名單,現在我想選中的名稱保存到共享偏愛。 下次打開活動時,檢查的項目應該存在。保存複選框項目的狀態在一個多選列表視圖
我是新來的android。 在此先感謝.....
這裏是我的代碼...
public class Blacklist extends Activity {
SharedPreferences pref,selected;
String phoneNo, s;
SharedPreferences.Editor edit;
SharedPreferences.Editor select;
ArrayList<String> temp = new ArrayList<String>();
static ArrayList<String> blocked = new ArrayList<String>();
ContentResolver mcursor;
ListView con;
ArrayList<String> al;
ArrayList<String> ph;
static ArrayList<String> checked;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.blacklist);
con = (ListView) findViewById(R.id.contactlist);
al = new ArrayList<String>();
checked = new ArrayList<String>();
// get contacts
Cursor cur = getContacts();
// display contacts
if (cur != null) {
while (cur.moveToNext()) {
String name = cur.getString(cur
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
al.add(name);
}
// create an array adapter
ArrayAdapter<String> ad = new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_list_item_multiple_choice, al);
con.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
con.setAdapter(ad);
// to find the selected names
con.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
int kunk = 0;
ph = new ArrayList<String>();
SparseBooleanArray sparseBooleanArray = con
.getCheckedItemPositions();
for (int i = 0; i < con.getCount(); i++) {
selected= getSharedPreferences("SELECTED", MODE_PRIVATE);
select=selected.edit();
if (sparseBooleanArray.get(i) == true) {
select.putBoolean("bool "+arg1, true);
String check = con.getItemAtPosition(i).toString();
if (!temp.contains(check)) {
// get the no
Cursor cursor; // Cursor object
String mime; // MIME type
int dataIdx; // Index of DATA1 column
int mimeIdx; // Index of MIMETYPE column
int nameIdx; // Index of DISPLAY_NAME column
// Get the name
cursor = getContentResolver()
.query(ContactsContract.Contacts.CONTENT_URI,
new String[] { ContactsContract.Contacts.DISPLAY_NAME },
null, null, null);
if (cursor.moveToFirst()) {
nameIdx = cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
// Set up the projection
String[] projection = {
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Contacts.Data.DATA1,
ContactsContract.Contacts.Data.MIMETYPE };
// Query ContactsContract.Data
cursor = getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
projection,
ContactsContract.Data.DISPLAY_NAME
+ " = ?",
new String[] { check }, null);
if (cursor.moveToFirst()) {
// Get the indexes of the MIME type and
// data
mimeIdx = cursor
.getColumnIndex(ContactsContract.Contacts.Data.MIMETYPE);
dataIdx = cursor
.getColumnIndex(ContactsContract.Contacts.Data.DATA1);
// Match the data to the MIME type,
// store in variables
do {
ArrayList<String> phtemp = new ArrayList<String>();
mime = cursor.getString(mimeIdx);
if (ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE
.equalsIgnoreCase(mime)) {
phoneNo = cursor
.getString(dataIdx);
phoneNo = PhoneNumberUtils
.formatNumber(phoneNo);
ph.add(phoneNo);
if (!phtemp.contains(ph)) {
phtemp.add(phoneNo);
}
}
} while (cursor.moveToNext());
}
}
}
}
}
if (ph != null) {
HashSet<String> hs = new HashSet();
hs.addAll(ph);
ph.clear();
ph.addAll(hs);
for (String l : hs) {
// save list in shared preference
pref = getSharedPreferences("MY_LOG", MODE_PRIVATE);
edit = pref.edit();
kunk++;
s = Integer.toString(kunk);
edit.putString(s, l);
}
edit.putString("size", s);
edit.commit();
Toast.makeText(getApplicationContext(),
"saved to shared", Toast.LENGTH_LONG).show();
// retrieve the values of blocked numbers and store it
// in an array block list
pref = getSharedPreferences("MY_LOG", MODE_PRIVATE);
String un = pref.getString("size", "0");
for (int i = 1; i <= Integer.parseInt(un); i++) {
String no = pref.getString("" + i, "nill");
String non = returnNumberOnly(no);
.
blocked.add(non);
}
}
}
});
} else {
}
}
String returnNumberOnly(String number) {
String ss[] = number.split("\\+91");
String numberArray[] = null;
if (ss.length == 2) {
numberArray = ss[1].split("-");
} else if (ss.length == 1) {
numberArray = ss[0].split(" ");
}
number = "";
for (int i = 0; i < numberArray.length; i++) {
number += numberArray[i];
}
System.out.println("------------------ IN returnNumberOnly Fn ------ "
+ number + " RETURNED ------------------------------------ ");
return number;
}
private Cursor getContacts() {
Uri uri = ContactsContract.Contacts.CONTENT_URI;
Cursor c;
mcursor = getContentResolver();
c = mcursor.query(uri, null, null, null, null);
return c;
}
}
ü沒有任何谷歌? –
檢查[這](http://stackoverflow.com/a/11601129/2591002) –
看到這個example..it將HEP you..http://stackoverflow.com/questions/18715556/removing-muliple-items-從-列表視圖 - 使用 - 複選框功能於Android的 – Giridharan