共享首选项不适用于 ListView 中的联系人
Posted
技术标签:
【中文标题】共享首选项不适用于 ListView 中的联系人【英文标题】:Shared Preferences not working with Contacts in ListView 【发布时间】:2015-04-24 07:06:12 【问题描述】:我基本上得到了这个列表视图,它可以提取设备中现有用户联系人的电话和号码并填充它。只要设备还在,我希望能够选择电话/号码并保存它们。我的三个问题:
共享首选项没有正确保存任何想法? 我还想添加一个复选框或颜色来标识哪些是已选择和未选择的想法?如何使用contactscontract显示手机中用户联系人的缩略图? (我试过了)->
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_
android:layout_ 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_
android:layout_ />
</RelativeLayout>
【问题讨论】:
第一个问题,如果您使用的是光标适配器,您可以从光标值中获取电话号码/姓名并将其保存在共享首选项中。 【参考方案1】:这里您使用了布尔类型并尝试保存字符串值,因为您没有存储在首选项中
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();
【讨论】:
为快速响应干杯,但是,当我将布尔值更改为字符串时,如果它们是布尔值,我会收到错误(Java 语言异常)无法更改字符串。关于图像,但我到底应该把它放在哪里,因为我把它放在 String[] 和 Int[] 之间,我得到了我没有声明的变量的错误?抱歉,我是新手。 使用这一行在 onitemclick saveselectedstate(position, true) 中保存状态; 我已经修改了代码,检查上面的参考,获取和保存状态方法仍然错误。它说在编辑器中放置字符串不能应用于.../get string can't be applied to Shared Pref to string boolean 我已经检查过...仍然没有骰子...它没有正确引用上一节中的其他代码位。 int/string 之间的冲突。 试着花点时间写代码,你就会明白以上是关于共享首选项不适用于 ListView 中的联系人的主要内容,如果未能解决你的问题,请参考以下文章
PreferenceManager.setDefaultValues 不适用于自定义首选项