androidparsingparse-platformandroid-5.0-lollipopandroid-parsequeryadapter

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.IllegalArgumentException: Spinner adapter view type count must be 1

and the application crash.

I have searched for a fix and I know that this is a problem that parse.com and not me have to resolve. But, I really want to make my app looks Material Design and if my app targetSdkVersion 19 the status bar is always black.

There is an answer for this problem that I don't understand and I can't ask directly in the question because I don't have privileges. A member of this community suggest to extend the parseadapter and @override the getViewTypeCount with this code. I'm a newbie in Android and I don't know how to properly follow that suggestion. I will appreciate if you guys help me with this.

Update:

I'm following the code in this answer, but an error says MyParseAdapter does not have type parameters.

Can you explain me what I'm doing wrong ?

Thanks

EDIT:

public void addItemsOnSpinner (){

    // 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.
    ParseQueryAdapter<ParseObject> adapter = new MyParseAdapter<ParseObject>(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() {
        public void onItemSelected(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>() {
                public void done(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
                    }
                }
            });
        }

        public void onNothingSelected(AdapterView<?> arg0) {

        }
    });

}

Solution

  • this is how i solved the problem

    first i created a new class like this

    public class MyParseAdapter extends ParseQueryAdapter<ParseObject> {
    
        public MyParseAdapter(Context context, QueryFactory<ParseObject> clazz) {
            super(context, clazz);
        }
    
        @Override
        public int getViewTypeCount() {
            return 1;
        }
    }
    

    and i replaced every ParseQueryAdapter with MyParseAdapter even the declaration

    try this

    public void addItemsOnSpinner (){
    
        // 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() {
            public void onItemSelected(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>() {
                    public void done(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
                        }
                    }
                });
            }
    
            public void onNothingSelected(AdapterView<?> arg0) {
    
            }
        });
    
    }