在我的应用程序中,我想将联系人存储为 android 应用程序中的安全列表。出于存储目的,我想从电话簿中获取联系人。我已经完成了获取联系号码,但我无法将其存储在应用程序中,所以当evar 应用程序关闭时,获取的号码被清除并且列表将变为空,请帮助..!!!
public class SafeList extends ListActivity implements OnClickListener {
String number;
ArrayAdapter<String> adapter;
LayoutInflater layoutInflater = null;
List<String> safelist = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.safelist_layout);
Button addContact = (Button) findViewById(R.id.addContact);
addContact.setOnClickListener(this);
safelist = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,safelist);
setListAdapter(adapter);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
// Intent intent = new Intent(Intent.ACTION_PICK,
// ContactsContract.Contacts.CONTENT_URI);
// startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
Uri uri = data.getData();
if (uri != null) {
Cursor c = null;
try {
c = getContentResolver()
.query(uri,
new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE },
null, null, null);
if (c != null && c.moveToFirst()) {
number = c.getString(0);
int type = c.getInt(1);
safelist.add(number);
adapter.notifyDataSetChanged();
showSelectedNumber();
}
} finally {
if (c != null) {
c.close();
}
}
}
}
}
public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
{
ViewHolder localViewHolder;
TextView number;
if (paramView == null)
{
paramView = SafeList.this.layoutInflater.inflate(R.layout.safelist_item, null);
localViewHolder = new SafeList.ViewHolder();
localViewHolder.number = ((TextView)paramView.findViewById(R.id.textContactNumber));
paramView.setTag(localViewHolder);
}
while (true)
{
localViewHolder = (SafeList.ViewHolder)paramView.getTag();
return paramView;
}
}
static class ViewHolder {
public TextView number;
}
private void showSelectedNumber() {
// TODO Auto-generated method stub
Toast.makeText(this, number, Toast.LENGTH_LONG).show();
}
}