Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 共享首选项不适用于ListView中的联系人_Java_Android_Listview_Sharedpreferences_Android Contacts - Fatal编程技术网

Java 共享首选项不适用于ListView中的联系人

Java 共享首选项不适用于ListView中的联系人,java,android,listview,sharedpreferences,android-contacts,Java,Android,Listview,Sharedpreferences,Android Contacts,我基本上得到了这个列表视图,它可以调出设备中现有用户联系人的电话和号码并填充它。我希望能够选择电话/号码,并在设备仍在时保存它们。我的三个问题: 共享的首选项不能正确保存任何想法 此外,我想添加一个复选框或颜色,以确定选择和未选择的想法 如何使用Contacts在手机上显示用户联系人的缩略照片?(我已经试过了)-> 字符串[]from={ContactsContract.commondatatypes.Phone.DISPLAY\u PHOTO,ContactsContract.commond

我基本上得到了这个列表视图,它可以调出设备中现有用户联系人的电话和号码并填充它。我希望能够选择电话/号码,并在设备仍在时保存它们。我的三个问题:

  • 共享的首选项不能正确保存任何想法
  • 此外,我想添加一个复选框或颜色,以确定选择和未选择的想法
  • 如何使用Contacts在手机上显示用户联系人的缩略照片?(我已经试过了)->

    字符串[]from={ContactsContract.commondatatypes.Phone.DISPLAY\u PHOTO,ContactsContract.commondatatypes.Phone.PHOTO\u ID,ContactsContract.commondatatypes.Phone.PHOTO\u FILE\u 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();
    }


}
导入android.app.AlertDialog;
导入android.app.ListActivity;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.database.Cursor;
导入android.os.Bundle;
导入android.provider.contacts合同;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ListView;
导入android.widget.SimpleCursorAdapter;
公共类UserContacts扩展了ListActivity{
ListView lv;
游标游标1;
字符串spref_identifier=“com.example.app”;
字符串entryIdentifierPrefix=“selectionState\u listEntry”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u user\u contacts);
cursor1=getContentResolver().query(ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,null,null,null);
开始管理游标(游标1);
字符串[]from={ContactsContract.commondatatypes.Phone.DISPLAY\u NAME,ContactsContract.commondatatypes.Phone.NUMBER,ContactsContract.commondatatypes.Phone.\u ID};
int[]to={android.R.id.text1,android.R.id.text2};
SimpleCrsorAdapter listAdapter=新的SimpleCrsorAdapter(这个,android.R.layout.simple\u列表\u项目\u已激活\u 2,游标1,从,到);
setListAdapter(listAdapter);
lv=getListView();
lv.setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE);
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
saveSelectedState(位置,true);
刷新列表();
}
});
}
私有void saveSelectedState(int entryPosition,布尔selectedState){
SharedReferences.Editor spe=this.getSharedReferences(spref_标识符,Context.MODE_PRIVATE).edit();
spe.putString(entryIdentifierPrefix+entryPosition,selectedState);spe.commit();}
私有布尔getSelectedState(int entryPosition){
SharedReferences sp=this.getSharedReferences(spref\u标识符,Context.MODE\u PRIVATE);
返回sp.getString(entryIdentifierPrefix+entryPosition,false);
//默认值设置为false。如有必要,请对此进行调整。
}
私有无效刷新列表(){
对于(int i=0;i
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>

这里您使用了布尔类型并试图保存字符串值,因为您的字符串值没有存储在首选项中

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. 
}
以及用于显示显示pic

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

对于第一个问题,如果您使用的是游标适配器,您可以从游标值中获取电话号码/名称,并将其保存在共享的prefs中。为快速响应干杯,但是,当我将布尔值更改为字符串时,我得到一个错误(Java语言异常),如果它们是布尔值,则无法更改字符串。关于图像,但我应该把它放在哪里,因为