这里我使用内容提供商来获取所有联系人并将其放入有多个选项的列表中,现在我想将选中的名称保存到共享首选项中。 下次打开活动时,应该存在已检查的项目。
我是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;
}
}
答案 0 :(得分:0)
将列表存储在共享首选项中。
下次活动开始时,使用共享首选项检索数组列表。
在getView()
中添加if条件,如果数组列表包含位置,则将复选框状态设置为已选中。
例如: -
if(array_list.get(position) == listview_item) // Just for reference. Not actual code
checkbox1.setChecked(true);
else
checkbox1.setChecked(false);
我希望你能得到它。
答案 1 :(得分:0)
项目点击监听器上的ListView: -
final Integer index = Integer.valueOf(position);
if(!checkedPositions.contains(index))//This is a global array list which contains the checked item states
checkedPositions.add(index);
else
checkedPositions.remove(index);
check.setChecked(checkedPositions.contains(index));
GetView()内部侦听器: -
holder.checkbox1.setChecked(flag);
if(flag){
if(!checkedPositions.contains(position))
checkedPositions.add(position);
}
else
{
checkedPositions.clear();
}
final Integer index = Integer.valueOf(position);
holder.checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if(isChecked){
if(!checkedPositions.contains(index))
checkedPositions.add(index);
}
else
checkedPositions.remove(index);
}
});
holder.checkbox1.setChecked(checkedPositions.contains(index));