gpt4 book ai didi

android - 我想使用共享首选项将联系人保存在屏幕上

转载 作者:行者123 更新时间:2023-11-30 02:10:36 24 4
gpt4 key购买 nike

我编写了从手机获取联系人的代码。现在我想使用共享首选项将联系人保存到 Activity 中。我在下面分享我的代码。请告诉我必须做什么?还有一件事是当所有联系人都保存到 Activity 中时。我想阻止保存到 Activity 中的联系人的来电和去电。

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();
}
}
}

最佳答案

共享首选项将数据保存在键值对中。在共享首选项中保存/获取少量数据很容易,但对于大数据,如果您有很多联系人,则很难获取特定联系人,因此它最好将数据存储到数据库中

关于android - 我想使用共享首选项将联系人保存在屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30163626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com