so I've created a list view in my xml file which just has the simple id/list property.
In my class, I've set the name and number parameters in from the ContactsContracts class.
I've got 2 questions, First is when I click a number/name it should be highlighted and be selected and saved unless the user unclicks it. Second question is how do I, import the contacts photo...is it just in the String Array -> ...phone.Photo_ID?
Class:
package com.example.ankhit.saveme;
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.graphics.Color;
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_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, !getSelectedState(position));
refreshList();
}
});
}
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 refreshList() {
for (int i = 0; i < lv.getCount(); i++) {
boolean selected = getSelectedState(i);
((View)lv.getItemAtPosition(i)).setBackgroundColor(selected ? Color.GREEN : Color.RED); // Change colors to whatever you need.
}
}
@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();
}
}
Errors:
02-11 13:48:43.069 7216-7216/com.example.ankhit.saveme E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: android.content.ContentResolver$CursorWrapperInner cannot be cast to android.view.View
at com.example.ankhit.saveme.UserContacts.refreshList(UserContacts.java:75)
at com.example.ankhit.saveme.UserContacts.access$200(UserContacts.java:21)
at com.example.ankhit.saveme.UserContacts$1.onItemClick(UserContacts.java:52)
at android.widget.AdapterView.performItemClick(AdapterView.java:301)
at android.widget.AbsListView.performItemClick(AbsListView.java:1507)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3292)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:4550)
at android.view.View.dispatchTouchEvent(View.java:7685)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2395)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2401)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2134)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2289)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1575)
at android.app.Activity.dispatchTouchEvent(Activity.java:2470)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2237)
at android.view.View.dispatchPointerEvent(View.java:7892)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3976)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3860)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5122)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5101)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5199)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:125)
at android.os.Looper.loop(Looper.java:138)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
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>