我编写代码来从移动设备中获取联系人。现在我想使用共享首选项保存联系人的活动。我在下面分享我的代码。请告诉我要做什么?还有一件事是当所有联系人都保存到活动时。我想阻止保存到活动的联系人的传入和传出呼叫。
import java.util.ArrayList;
import java.util.Collections;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class BlackListActivity extends Activity implements OnClickListener {
private static final String TAG = "BlackListActivity";
Button add;
ListView lv;
String[] contactList ;
ArrayList<String> list = new ArrayList<String>();
ArrayList<String> blcklist = new ArrayList<String>();
ArrayAdapter<String> ad;
String spref_identifier = "com.example.app";
String entryIdentifierPrefix = "selectionState_listEntry_";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_black_list);
add = (Button)findViewById(R.id.add);
add.setOnClickListener(this);
lv = (ListView)findViewById(R.id.listView1);
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
list.add(name);
Log.d(TAG, name);
}
pCur.close();
}
}
Collections.sort(list);
}
cur.close();
int x = list.size();
contactList = new String[x];
contactList = (String[]) list.toArray(contactList);
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v,final int index, long arg3) {
// Toast.makeText(BlackListActivity.this,lv.getItemAtPosition(index).toString(), Toast.LENGTH_LONG).show();
new AlertDialog.Builder(BlackListActivity.this)
.setMessage("Are you sure want to delete "+lv.getItemAtPosition(index).toString()+" contact number ?")
.setTitle("Deleting Contact....")
.setIcon(R.drawable.delete)
.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(BlackListActivity.this, lv.getItemAtPosition(index).toString()+" contact number deleted", Toast.LENGTH_LONG).show();
blcklist.remove(index);
ad.notifyDataSetChanged();
ad.notifyDataSetInvalidated();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
return false;
}
});
}
@Override
public void onClick(View v) {
if( v == add){
new AlertDialog.Builder(this)
.setTitle("Contact List")
.setCancelable(false)
.setMultiChoiceItems(contactList, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
blcklist.add(contactList[indexSelected]);
list.remove(contactList[indexSelected]);
}
else if (!isChecked) {
blcklist.remove(contactList[indexSelected]);
}
}
})
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ad = new ArrayAdapter<String>(BlackListActivity.this,android.R.layout.simple_list_item_1,blcklist);
lv.setAdapter(ad);
contactList=(String[]) list.toArray(contactList);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
}
}
答案 0 :(得分:1)
共享偏好保存键值对中的数据..它很容易保存/获取共享pref中的少量数据但是对于大数据,如果你的情况很难,如果你有很多联系人那么很难获取特定的联系人,所以它更好地将数据存储到数据库中