Skip to content

Reading Contact Bug Reslove #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,138 @@

import java.util.ArrayList;

import android.Manifest;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.CursorLoader;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class ContactListActivity extends Activity {

ArrayList<String> names = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_list);
loadContacts();
populateListView();
}

private void populateListView() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, names);

ListView listView = (ListView) findViewById(R.id.lvContacts);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ContactListActivity.this, names.get(position), Toast.LENGTH_SHORT).show();
}
});
}

@SuppressLint("NewApi")
private void loadContacts() {
Uri allContacts = Uri.parse("content://contacts/people");
CursorLoader cursorLoader = new CursorLoader(this, allContacts,
null, // the columns to retrive
null, // the selection criteria
null, // the selection args
null // the sort order
);

Cursor c = cursorLoader.loadInBackground();
if (c.moveToFirst()) {
do {
// Get Contact ID
int idIndex = c.getColumnIndex(ContactsContract.Contacts._ID);
String contactID = c.getString(idIndex);

// Get Contact Name
int nameIndex = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String contactDisplayName = c.getString(nameIndex);
names.add(contactDisplayName);

Log.d("debug", contactID + ", " + contactDisplayName);
} while (c.moveToNext());
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_contact_list, menu);
return true;
}


boolean isPermissionGranted = false ;
ArrayList<String> names = new ArrayList<String>();
private RelativeLayout noPermission;
private static final int MY_PERMISSION_READ = 1000 ;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_list);

noPermission = (RelativeLayout) findViewById(R.id.acl_no_permission);
checkPermissions();
}


private void checkPermissions(){
boolean grantedPermissions = ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_GRANTED;

if(grantedPermissions){
loadContacts();
populateListView();
noPermission.setVisibility(View.GONE);
}
else{
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSION_READ);
}
}

private void populateListView() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, names);

ListView listView = (ListView) findViewById(R.id.lvContacts);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ContactListActivity.this, names.get(position), Toast.LENGTH_SHORT).show();
}
});
}

@SuppressLint("NewApi")
private void loadContacts() {
Uri allContacts = Uri.parse("content://contacts/people");
CursorLoader cursorLoader = new CursorLoader(this, allContacts,
null, // the columns to retrive
null, // the selection criteria
null, // the selection args
null // the sort order
);

Cursor c = cursorLoader.loadInBackground();
if (c.moveToFirst()) {
do {
// Get Contact ID
int idIndex = c.getColumnIndex(ContactsContract.Contacts._ID);
String contactID = c.getString(idIndex);

// Get Contact Name
int nameIndex = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
String contactDisplayName = c.getString(nameIndex);
names.add(contactDisplayName);

Log.d("debug", contactID + ", " + contactDisplayName);
} while (c.moveToNext());
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSION_READ: {

// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
loadContacts();
populateListView();
noPermission.setVisibility(View.GONE);
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {

// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(ContactListActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
noPermission.setVisibility(View.VISIBLE);

}
return;
}

// other 'case' lines to check for other
// permissions this app might request
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_contact_list, menu);
return true;
}

}
120 changes: 70 additions & 50 deletions app/src/main/java/codepath/apps/demointroandroid/DemoSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
Expand All @@ -15,57 +16,75 @@
import android.widget.Toast;

public class DemoSelector extends Activity {
ExpandableListView elvChapters;
ChaptersListAdapter elaAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo_selector);
setupChaptersListView();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_demo_selector, menu);
return true;
}
private void setupChaptersListView() {
elvChapters = (ExpandableListView)findViewById(R.id.elvChapters);
elaAdapter = new ChaptersListAdapter();
elvChapters.setAdapter(elaAdapter);
elvChapters.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
String exerciseTitle = (String)elaAdapter.getChild(groupPosition, childPosition);

ExpandableListView elvChapters;
ChaptersListAdapter elaAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo_selector);
setupChaptersListView();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_demo_selector, menu);
return true;
}

private void setupChaptersListView() {
elvChapters = (ExpandableListView) findViewById(R.id.elvChapters);
elaAdapter = new ChaptersListAdapter();
elvChapters.setAdapter(elaAdapter);
elvChapters.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {

String exerciseTitle = (String)elaAdapter.getChild(groupPosition, childPosition);
Class<? extends Activity> exerciseClass = elaAdapter.getExerciseClass(groupPosition, childPosition, id);
if (exerciseClass != null) {
Toast.makeText(DemoSelector.this, exerciseTitle, Toast.LENGTH_LONG).show();
startActivity(new Intent(DemoSelector.this, exerciseClass));
Toast.makeText(DemoSelector.this, exerciseTitle, Toast.LENGTH_LONG).show();
startActivity(new Intent(DemoSelector.this, exerciseClass));
} else {
Toast.makeText(DemoSelector.this, "Exercise Not Available", Toast.LENGTH_SHORT).show();
Toast.makeText(DemoSelector.this, "Exercise Not Available", Toast.LENGTH_SHORT).show();
}
return false;
}
});
return false;
}
});

}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.about:
Toast.makeText(this, getString(R.string.about_clicked), Toast.LENGTH_LONG).show();
return true;
case R.id.ds_help:
Toast.makeText(this, getString(R.string.help_clicked), Toast.LENGTH_LONG).show();
return true;
case R.id.settings:
Toast.makeText(this, getString(R.string.settings_clicked), Toast.LENGTH_LONG).show();
return true;
default:
return super.onMenuItemSelected(featureId, item);
}

}
private class ChaptersListAdapter extends BaseExpandableListAdapter {
}

private class ChaptersListAdapter extends BaseExpandableListAdapter {
private String[] chapters = getResources().getStringArray(R.array.chapters);
private String[][] exercises;

public ChaptersListAdapter() {
super();
exercises = new String[chapters.length][];
for (int i=0; i < exercises.length; i++) {
int resId = getResources().getIdentifier("chap" + (i+1), "array", getPackageName());
exercises[i] = getResources().getStringArray(resId);
}
super();
exercises = new String[chapters.length][];
for (int i = 0; i < exercises.length; i++) {
int resId = getResources().getIdentifier("chap" + (i + 1), "array", getPackageName());
exercises[i] = getResources().getStringArray(resId);
}
}


Expand Down Expand Up @@ -97,7 +116,7 @@ public TextView getGenericView() {
}

public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setPadding(80, 20, 20, 20);
textView.setText(getChild(groupPosition, childPosition).toString());
Expand All @@ -117,7 +136,7 @@ public long getGroupId(int groupPosition) {
}

public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
Expand All @@ -129,12 +148,13 @@ public boolean isChildSelectable(int groupPosition, int childPosition) {

public boolean hasStableIds() {
return true;
}
}

public Class<? extends Activity> getExerciseClass(int groupPosition, int childPosition, long id) {
String exerciseId = "chap" + (groupPosition + 1) + "ex" + (childPosition + 1);
return ExerciseActivityMapper.getExerciseClass(exerciseId);
String exerciseId = "chap" + (groupPosition + 1) + "ex" + (childPosition + 1);
return ExerciseActivityMapper.getExerciseClass(exerciseId);
}
}
}


}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_basic_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
android:ems="10">

<requestFocus />
</EditText>
Expand Down
Loading