Skip to content Skip to sidebar Skip to footer

Android Spinner Adapters And Lollipop

Like many others, I'm using Parse.com in my android application. When I use a ParseQueryAdapter in a fragment to retrieve data and pass it to a spinner a java.lang.IllegalArgumentE

Solution 1:

this is how i solved the problem

first i created a new class like this

publicclassMyParseAdapterextendsParseQueryAdapter<ParseObject> {

    publicMyParseAdapter(Context context, QueryFactory<ParseObject> clazz) {
        super(context, clazz);
    }

    @OverridepublicintgetViewTypeCount() {
        return1;
    }
}

and i replaced every ParseQueryAdapter with MyParseAdapter even the declaration

try this

publicvoidaddItemsOnSpinner (){

    // Instantiate a QueryFactory to define the ParseQuery to be used for fetching items in this// Adapter.
    MyParseAdapter.QueryFactory<ParseObject> factory =
            new MyParseAdapter.QueryFactory<ParseObject>() {
                public ParseQuery create() {
                    ParseQuery query = new ParseQuery("Books");
                    //query.whereEqualTo("activated", true);
                    query.orderByAscending("title");
                    return query;
                }
            };

    // Pass the factory into the ParseQueryAdapter's constructor.
    MyParseAdapter adapter = new MyParseAdapter(getActivity(), factory);


    adapter.setTextKey("title");


   /* // Perhaps set a callback to be fired upon successful loading of a new set of ParseObjects.
    adapter.addOnQueryLoadListener(new OnQueryLoadListener<ParseObject>() {
        public void onLoading() {
            // Trigger any "loading" UI
        }

        public void onLoaded(List<ParseObject> objects, ParseException e) {
            // Execute any post-loading logic, hide "loading" UI
        }
    });*/

    spinnerBook.setPopupBackgroundResource(R.drawable.spinner_style);
    spinnerBook.setAdapter(adapter);

    spinnerBook.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        publicvoidonItemSelected(AdapterView<?> parent, View view, int pos, long id) {

            Toast.makeText(parent.getContext(),
                    "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
                    Toast.LENGTH_SHORT).show();


            // Log.d("test", spinnerBook.getSelectedItem().toString());// Log.d("test2", parent.getItemAtPosition(pos).toString());
            ParseObject item = (ParseObject) parent.getAdapter().getItem(pos);
            objectID = item.getObjectId().toString();

            //Log.d("test3",objectID);

            ParseQuery<ParseObject> query = ParseQuery.getQuery("Books");
            query.getInBackground(objectID, new GetCallback<ParseObject>() {
                publicvoiddone(ParseObject object, ParseException e) {
                    if (e == null) {
                        // object will be your game score
                        bookTitle = object.getString("title");
                        txtBookAuthor.setText(object.getString("author"));
                        txtBookISBN.setText(object.getString("isbn"));
                        //  category = object.getString("category");
                    } else {
                        // something went wrong
                    }
                }
            });
        }

        publicvoidonNothingSelected(AdapterView<?> arg0) {

        }
    });

}

Post a Comment for "Android Spinner Adapters And Lollipop"