【发布时间】: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_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>
【问题讨论】:
-
第一个问题,如果您使用的是光标适配器,您可以从光标值中获取电话号码/姓名并将其保存在共享首选项中。
标签: java android listview sharedpreferences android-contacts