我基本上得到了这个列表视图,它可以提取设备中现有用户联系人的电话和号码并填充它。我希望能够选择手机/号码并保存,只要设备仍在那里。我的三个问题:
如何使用联系人显示用户联系人的缩略图照片是否包含任何想法? (我试过这个) - >
String [] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_PHOTO,ContactsContract.CommonDataKinds.Phone.PHOTO_ID,ContactsContract.CommonDataKinds.Phone.PHOTO_FILE_ID};
这在我的适配器中产生和错误,不确定如何修复。
类别:
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class UserContacts extends ListActivity {
ListView lv;
Cursor cursor1;
String spref_identifier = "com.example.app";
String entryIdentifierPrefix = "selectionState_listEntry_";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_contacts);
cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
startManagingCursor(cursor1);
String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone._ID};
int[] to = {android.R.id.text1,android.R.id.text2};
SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_activated_2,cursor1,from,to);
setListAdapter(listAdapter);
lv = getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
saveSelectedState(position, true);
refreshList();
}
});
}
private void saveSelectedState(int entryPosition, boolean selectedState) {
SharedPreferences.Editor spe = this.getSharedPreferences( spref_identifier, Context.MODE_PRIVATE).edit();
spe.putString(entryIdentifierPrefix + entryPosition, selectedState); spe.commit(); }
private boolean getSelectedState(int entryPosition) {
SharedPreferences sp = this.getSharedPreferences( spref_identifier, Context.MODE_PRIVATE);
return sp.getString(entryIdentifierPrefix + entryPosition, false);
// Default value is set as false. Tweak this if necessary.
}
private void refreshList() {
for (int i = 0; i < lv.getCount(); i++) { lv.setItemChecked(getSelectedItemPosition(), getSelectedState(i)); }
}
@Override
public long getSelectedItemId() {
return super.getSelectedItemId();
}
@Override
public int getSelectedItemPosition() {
return super.getSelectedItemPosition();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_user_contacts, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.homescreen:
homescreenItem();
return true;
case R.id.dashboard:
dashboardItem();
return true;
case R.id.about:
aboutItem();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void homescreenItem(){
startActivity(new Intent(UserContacts.this, Home.class));
}
private void dashboardItem(){
startActivity(new Intent(UserContacts.this, Dashboard.class));
}
private void aboutItem(){
new AlertDialog.Builder(this)
.setTitle("About")
.setMessage("Welcome to Save Me! An interactive and intuitive way to protect yourself during emergency situations and keep your location privacy. Made for a Dissertation and Developed by Ankhit Sharma")
.setNeutralButton("OK" , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
}
XML布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.ankhit.saveme.UserContacts">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
答案 0 :(得分:0)
这里你使用了boolean类型并尝试保存字符串值,因为你没有存储在首选项中
private void saveSelectedState(int entryPosition, boolean selectedState) {
SharedPreferences.Editor spe = this.getSharedPreferences( spref_identifier, Context.MODE_PRIVATE).edit();
spe.putBoolean(entryIdentifierPrefix + entryPosition, selectedState); spe.commit();
}
private boolean getSelectedState(int entryPosition) {
SharedPreferences sp = this.getSharedPreferences( spref_identifier, Context.MODE_PRIVATE);
return sp.getBoolean(entryIdentifierPrefix + entryPosition, false);
// Default value is set as false. Tweak this if necessary.
}
试试这个。这里我用字符串类型
保存了首选项中的值private void saveSelectedState(String keyname, int position) {
SharedPreferences.Editor spe = this.getSharedPreferences( spref_identifier, Context.MODE_PRIVATE).edit();
spe.putString(keyname,entryIdentifierPrefix + entryPosition); spe.commit(); }
private boolean getSelectedState(String keyname) {
SharedPreferences sp = this.getSharedPreferences( spref_identifier, Context.MODE_PRIVATE);
return sp.getString(keyname, "");
// Default value is set as blank. Tweak this if necessary.
}
用于显示显示图片
image_uri = cur .getString(cur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (image_uri != null) {
System.out.println(Uri.parse(image_uri));
try {
bitmap = MediaStore.Images.Media .getBitmap(this.getContentResolver(), Uri.parse(image_uri));
System.out.println(bitmap);
} catch (FileNotFoundException e) { // TODO Auto-generated catch block
e.printStackTrace(); }
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }