dimanche 26 avril 2015

Unexpected Token in Fragment

I am trying to embed an if-statement in a fragment to verify if a certain contact has a phone number. If so, I would like to pull the contact data. For some reason, no matter where I try to place an if-statement in the code, I receive an "unexpected token" error in Android Studio / Error: illegal start of type when I try to compile.

import android.provider.ContactsContract.Contacts;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.database.Cursor;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.content.Context;
public class ContactsFragment extends ListFragment implements LoaderCallbacks<Cursor> {

private CursorAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // create adapter once
    Context context = getActivity();
    int layout = android.R.layout.activity_list_item;
    Cursor c = null; // there is no cursor yet
    int flags = 0; // no auto-requery! Loader requeries.
    mAdapter = new SimpleCursorAdapter(context, layout, c, FROM, TO, flags);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // each time we are started use our listadapter
    setListAdapter(mAdapter);
    // and tell loader manager to start loading
    getLoaderManager().initLoader(0, null, this);
}

// columns requested from the database
private static final String[] PROJECTION = {


        Contacts.HAS_PHONE_NUMBER,
        Contacts._ID, // _ID is always required
        Contacts.DISPLAY_NAME_PRIMARY // that's what we want to display
};

// and name should be displayed in the text1 textview in item layout

public String[] phone = { Contacts.HAS_PHONE_NUMBER};
if (phone > 0) {
private static final String[] FROM = { Contacts.DISPLAY_NAME_PRIMARY };
private static final int[] TO = { android.R.id.text1 };
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {

    // load from the "Contacts table"
    Uri contentUri = Contacts.CONTENT_URI;

    // no sub-selection, no sort order, simply every row
    // projection says we want just the _id and the name column
    return new CursorLoader(getActivity(),
            contentUri,
            PROJECTION,
            null,
            null,
            null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    // Once cursor is loaded, give it to adapter
    mAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    // on reset take any old cursor away
    mAdapter.swapCursor(null);
}

}

In this case, ContactsContract.Contacts.HAS_PHONE_NUMBER is a string with value 0 or 1. I want to remove all the e-mail addresses from my list (so I am verifying by phone number).

Thanks!

Aucun commentaire:

Enregistrer un commentaire