从FragmentAdapter类

时间:2015-08-30 08:29:27

标签: java android fragment android-listfragment listadapter

我正在使用“滑动标签”创建地址簿应用程序,即“ViewPager”。我想在FragmentAdapter类中调用ListFragment。当我想在TestFragmentAdapter类中调用AddressBook类时,我遇到错误。确切地说,错误发生在“新的AddressBoook();”。请有人可以帮助我吗?感谢

这是我的AddressBook.java Fragment类。

package com.rufflez.viewpagerexample;

import android.app.ListFragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentUris;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

public class AddressBook extends ListFragment implements 
LoaderCallbacks<Cursor>{

private CursorAdapter mAdapter;
public ListView listView;
public Cursor cursor;


public static final String[] FROM = { ContactsContract.Contacts.DISPLAY_NAME_PRIMARY, ContactsContract.Contacts.PHOTO_THUMBNAIL_URI };
private static final int[] TO = { R.id.text_name, R.id.contact_thumbnail };

int layout = R.layout.contact_list_item;



// Name should be displayed in the text1 TextView in item layout

private android.content.Context context;
public View view;
public static Uri uri;


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

    // delete list if already there (old version)
    if (!(listView == null)){
        listView.setAdapter(null);
    }

    // create adapter once
    context = getActivity();
    int layout = android.R.layout.simple_list_item_1;
    Cursor c = null; // there is no cursor yet
    int flags = 0; // no auto-requery! Loader requeries.

    // put List in adapter
    mAdapter = new SimpleCursorAdapter(context, layout, c, FROM, TO, flags);



} // end onCreate


// columns requested from the database
private static final String[] PROJECTION = {
    ContactsContract.Contacts._ID,
    ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
    ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
    ContactsContract.Contacts.HAS_PHONE_NUMBER,
    ContactsContract.Contacts.IN_VISIBLE_GROUP
};

// this goes in the CursorLoader parameter list, it filters
// out only those contacts who have a phone number
private static final String SELECTION = 
        ContactsContract.Contacts.HAS_PHONE_NUMBER + "='1'";

// Empty public constructor, required by the system
public AddressBook() {}

// A UI Fragment must inflate its View (all fragments must override onCreateView)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the fragment layout
    View view =  inflater.inflate(R.layout.contact_list_view,
            container, false);
    listView = (ListView) view.findViewById(R.id.list); 

    // set Contact photo to ImageView
    AddressBook frag = new AddressBook();
    ImageView thumb = (ImageView)view.findViewById(R.id.contact_thumbnail);
    frag.getPhotoUriFromID(ContactsContract.Contacts._ID);
    thumb.setImageURI(uri);
    return view;
}

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

    // and tell loader manager to start loading
    getLoaderManager().initLoader(0, null, this);

    listView.setAdapter(mAdapter);
    listView.setFastScrollEnabled(true);

}

// a CursorLoader does a query in the background
@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,
            SELECTION,
            null,
            ContactsContract.Contacts.DISPLAY_NAME
            );
}

private Uri getPhotoUriFromID(String id) {
    try {
        Cursor cur = context.getContentResolver()
                .query(ContactsContract.Data.CONTENT_URI,
                        null,
                        ContactsContract.Data.CONTACT_ID
                        + "="
                        + id
                        + " AND "
                        + ContactsContract.Data.MIMETYPE
                        + "='"
                        + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                        + "'", null, null);
        if (cur != null) {
            if (!cur.moveToFirst()) {
                return null; // no photo
            }
        } else {
            return null; // error in cursor process
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    Uri person = ContentUris.withAppendedId(
            ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
    uri = Uri.withAppendedPath(person,
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    return uri;
}

@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) {
    // Delete the reference to the existing Cursor,
    // so it can recycle it
    mAdapter.swapCursor(null);
}
}

这是我的TestFragmentAdapter.java类

package com.rufflez.viewpagerexample;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;

import com.viewpagerindicator.IconPagerAdapter;

public class TestFragmentAdapter extends FragmentPagerAdapter implements      IconPagerAdapter{
protected static final String[] CONTENT = new String[] {
    "This", "Is", "A", "Test"   
};

private int mCount = CONTENT.length;

public TestFragmentAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public int getIconResId(int index) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public Fragment getItem(int position) {
    Fragment fragment = new Fragment1();
    switch(position){
    case 0:
        fragment = new Fragment1();
        break;
    case 1:
        fragment = new AddressBook();
        break;
    case 2:
        fragment = new Fragment3();
        break;
    case 3:
        fragment = new Fragment4();
        break;
    }
    return fragment;
}

@Override
public int getCount() {
    return mCount;
}

@Override
public CharSequence getPageTitle(int position){
    String title = "";
    switch(position){
    case 0:
        title = "Fragment 1";
        break;
    case 1:
        title = "Fragment 2";
        break;
    case 2:
        title = "Fragment 3";
        break;
    case 3:
        title = "Fragment 4";
        break;
    }

    return title;
}

public void setCount(int count){
    if (count > 0 && count < 10){
        mCount = count;
        notifyDataSetChanged();
    }
}

}

我在调用AddressBook类时遇到错误

case 1:
    fragment = new AddressBook();
break;

请帮助!!!!

1 个答案:

答案 0 :(得分:0)

您可以在AddressBook.java中将android.app.Fragment更改为android.support.v4.app.ListFragment。 AddressBook不是android.support.v4.app.Fragment的子类,这就是为什么你会遇到类型转换错误。