使用片段保存数据

时间:2014-02-18 11:25:25

标签: android mysql android-fragments

我想知道如何使用片段从我的Android应用程序中将数据保存到我的自定义MySQL数据库中,我已经在oncreateview中调用了用于保存数据的函数,但是它给出了错误的二进制XML文件错误,使类片段充分膨胀其他错误,请给我一个想法,我称之为用片段保存数据的函数,  由于代码太冗长,我粘贴了部分代码

以下是我的代码:

public class ContactsListFragment extends ListFragment implements
            AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {

        // Defines a tag for identifying log entries
        private static final String TAG = "ContactsListFragment";

        // Bundle key for saving previously selected search result item
        private static final String STATE_PREVIOUSLY_SELECTED_KEY =
                "com.example.android.contactslist.ui.SELECTED_ITEM";

        private static final Cursor Cursor = null;
         public    DBHandler db;

        private ContactsAdapter mAdapter; // The main query adapter
        private ImageLoader mImageLoader; // Handles loading the contact image in a background thread
        private String mSearchTerm; // Stores the current search query term

        // Contact selected listener that allows the activity holding this fragment to be notified of
        // a contact being selected
        private OnContactsInteractionListener mOnContactSelectedListener;

        // Stores the previously selected search item so that on a configuration change the same item
        // can be reselected again
        private int mPreviouslySelectedSearchItem = 0;

        // Whether or not the search query has changed since the last time the loader was refreshed
        private boolean mSearchQueryChanged;

        // Whether or not this fragment is showing in a two-pane layout
        private boolean mIsTwoPaneLayout;

        // Whether or not this is a search result view of this fragment, only used on pre-honeycomb
        // OS versions as search results are shown in-line via Action Bar search from honeycomb onward
        private boolean mIsSearchResultView = false;

        /**
         * Fragments require an empty constructor.
         */
        public ContactsListFragment() {}

        /**
         * In platform versions prior to Android 3.0, the ActionBar and SearchView are not supported,
         * and the UI gets the search string from an EditText. However, the fragment doesn't allow
         * another search when search results are already showing. This would confuse the user, because
         * the resulting search would re-query the Contacts Provider instead of searching the listed
         * results. This method sets the search query and also a boolean that tracks if this Fragment
         * should be displayed as a search result view or not.
         *
         * @param query The contacts search query.
         */
        public void setSearchQuery(String query) {
            if (TextUtils.isEmpty(query)) {
                mIsSearchResultView = false;
            } else {
                mSearchTerm = query;
                mIsSearchResultView = true;
            }
        }

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

            // Check if this fragment is part of a two-pane set up or a single pane by reading a
            // boolean from the application resource directories. This lets allows us to easily specify
            // which screen sizes should use a two-pane layout by setting this boolean in the
            // corresponding resource size-qualified directory.
            mIsTwoPaneLayout = getResources().getBoolean(R.bool.has_two_panes);

            // Let this fragment contribute menu items
            setHasOptionsMenu(true);
            Log.d("Hope","Hope 3");

            // Create the main contacts adapter
            mAdapter = new ContactsAdapter(getActivity());

            if (savedInstanceState != null) {
                // If we're restoring state after this fragment was recreated then
                // retrieve previous search term and previously selected search
                // result.
                mSearchTerm = savedInstanceState.getString(SearchManager.QUERY);
                mPreviouslySelectedSearchItem =
                        savedInstanceState.getInt(STATE_PREVIOUSLY_SELECTED_KEY, 0);
            }

            /*
             * An ImageLoader object loads and resizes an image in the background and binds it to the
             * QuickContactBadge in each item layout of the ListView. ImageLoader implements memory
             * caching for each image, which substantially improves refreshes of the ListView as the
             * user scrolls through it.
             *
             * To learn more about downloading images asynchronously and caching the results, read the
             * Android training class Displaying Bitmaps Efficiently.
             *
             * http://developer.android.com/training/displaying-bitmaps/
             */
            mImageLoader = new ImageLoader(getActivity(), getListPreferredItemHeight()) {
                @Override
                protected Bitmap processBitmap(Object data) {
                    // This gets called in a background thread and passed the data from
                    // ImageLoader.loadImage().
                    return loadContactPhotoThumbnail((String) data, getImageSize());
                }
            };

            // Set a placeholder loading image for the image loader
            mImageLoader.setLoadingImage(R.drawable.ic_contact_picture_holo_light);

            // Add a cache to the image loader
            mImageLoader.addImageCache(getActivity().getSupportFragmentManager(), 0.1f);
        }

这是oncreateView函数,我倾向于将所有联系人保存到我的自定义mysql数据库中,

  

这就是我错误地将类碎片扩散错误的一点。

   **@Override

* GT; public View onCreateView(LayoutInflater inflater,ViewGroup容器,

              Bundle savedInstanceState) {
          // Inflate the list fragment layout
           SavingContacts savingcontacts=new SavingContacts();
           final String photoUri = Cursor.getString(ContactsQuery.PHOTO_THUMBNAIL_DATA);
               final String ContactID = Cursor.getString(ContactsQuery.ID);
               final String Lookup = Cursor.getString(ContactsQuery.LOOKUP_KEY);
               final String displayName = Cursor.getString(ContactsQuery.DISPLAY_NAME);
          //   SavingContacts savingcontacts=new SavingContacts();
           savingcontacts.savingcontact(ContactID, displayName, photoUri, Lookup);
          return inflater.inflate(R.layout.contact_list_fragment, container, false);
      }***

此联系人查询界面,在此contactlistfragment中

public interface ContactsQuery {

            // An identifier for the loader
            final static int QUERY_ID = 1;

            // A content URI for the Contacts table
            final static Uri CONTENT_URI = Contacts.CONTENT_URI;

            // The search/filter query Uri
            final static Uri FILTER_URI = Contacts.CONTENT_FILTER_URI;
            public  void savedata(Cursor cursor);

            // The selection clause for the CursorLoader query. The search criteria defined here
            // restrict results to contacts that have a display name and are linked to visible groups.
            // Notice that the search on the string provided by the user is implemented by appending
            // the search string to CONTENT_FILTER_URI.
            @SuppressLint("InlinedApi")
            final static String SELECTION =
                    (Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME) +
                    "<>''" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1";

            // The desired sort order for the returned Cursor. In Android 3.0 and later, the primary
            // sort key allows for localization. In earlier versions. use the display name as the sort
            // key.
            @SuppressLint("InlinedApi")
            final static String SORT_ORDER =
                    Utils.hasHoneycomb() ? Contacts.SORT_KEY_PRIMARY : Contacts.DISPLAY_NAME;

            // The projection for the CursorLoader query. This is a list of columns that the Contacts
            // Provider should return in the Cursor.
            @SuppressLint("InlinedApi")
            final static String[] PROJECTION = {

                    // The contact's row id
                    Contacts._ID,

                    // A pointer to the contact that is guaranteed to be more permanent than _ID. Given
                    // a contact's current _ID value and LOOKUP_KEY, the Contacts Provider can generate
                    // a "permanent" contact URI.
                    Contacts.LOOKUP_KEY,

                    // In platform version 3.0 and later, the Contacts table contains
                    // DISPLAY_NAME_PRIMARY, which either contains the contact's displayable name or
                    // some other useful identifier such as an email address. This column isn't
                    // available in earlier versions of Android, so you must use Contacts.DISPLAY_NAME
                    // instead.
                    Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME,

                    // In Android 3.0 and later, the thumbnail image is pointed to by
                    // PHOTO_THUMBNAIL_URI. In earlier versions, there is no direct pointer; instead,
                    // you generate the pointer from the contact's ID value and constants defined in
                    // android.provider.ContactsContract.Contacts.
                    Utils.hasHoneycomb() ? Contacts.PHOTO_THUMBNAIL_URI : Contacts._ID,

                    // The sort order column for the returned Cursor, used by the AlphabetIndexer
                    SORT_ORDER,



            };


            // The query column numbers which map to each value in the projection
            final static int ID = 0;
            final static int LOOKUP_KEY = 1;
            final static int DISPLAY_NAME = 2;
            final static int PHOTO_THUMBNAIL_DATA = 3;
            final static int SORT_KEY = 4;


        }

我不会将整个代码仅仅作为代码的一部分。

0 个答案:

没有答案