ShatterStar
Newbie
Hi,
Currently I have an activity and a fragment within that activity that get populated with the contacts on the phone, but I need to implement a custom list "look" to make it look better. How would I do this since my fragment implements Loader callbacks - here is my code
MainActivity.java
LoadContactsClass.java
ActivityMain.xml
list_layout.xml
list_layout_item.xml
Currently I have an activity and a fragment within that activity that get populated with the contacts on the phone, but I need to implement a custom list "look" to make it look better. How would I do this since my fragment implements Loader callbacks - here is my code
MainActivity.java
Java:
package testingloaderappah.com.testingloader;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 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.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}
}
LoadContactsClass.java
Code:
package testingloaderappah.com.testingloader;
import android.Manifest;
import android.app.Activity;
import android.app.Fragment;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.test.mock.MockPackageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class LoadContactsClass extends android.support.v4.app.ListFragment implements
LoaderManager.LoaderCallbacks<Cursor>
{
public LoadContactsClass() {}
ListView mContactsList;
SimpleCursorAdapter mCursorAdapter;
private static final String[] From = {
//ContactsContract.Contacts._ID,
//ContactsContract.Contacts.LOOKUP_KEY,
//ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
ContactsContract.Contacts.DISPLAY_NAME
};
private static final int[] To = new int[] {
R.id.list_item
};
@Override
public void onAttach(Context context){
super.onAttach(context);
}
@Override
public void onDetach(){
super.onDetach();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the fragment layout
return inflater.inflate(R.layout.list_layout, container, false);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Gets the ListView from the View list of the parent activity
mContactsList = (ListView)getActivity().findViewById(android.R.id.list);
// Gets a CursorAdapter
mCursorAdapter = new SimpleCursorAdapter(
getActivity(),
R.layout.list_layout_item,
null,
From,
To,
0);
// Sets the adapter for the ListView
setListAdapter(mCursorAdapter);
// Set the item click listener to be the current fragment.
//mContactsList.setOnItemClickListener(this);
getLoaderManager().initLoader(0, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(
getContext(),
ContactsContract.Contacts.CONTENT_URI,
null,
null,
null,
null
);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mCursorAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
mCursorAdapter.swapCursor(null);
}
}
ActivityMain.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="testingloaderappah.com.testingloader.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Hello World!"
android:onClick="loadFragment"
/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/fragment1"
android:name="testingloaderappah.com.testingloader.LoadContactsClass"
/>
</LinearLayout>
list_layout.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:id="@android:id/list" >
</ListView>
list_layout_item.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:id="@+id/list_item"
>
</TextView>