1

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) {

        }
    });

}
Community
  • 1
  • 1
frankie015
  • 358
  • 2
  • 10
  • How about changing the targetSdk to 19? You can do that in your app gradle. – Gent Ahmeti May 16 '15 at 07:37
  • "But, I really want to make my app looks Material Design and if my app targetSdkVersion 19 the status bar is always black." – frankie015 May 16 '15 at 13:31
  • Have you tried to do `MyParseAdapter adapter`? – Daniel Nugent May 17 '15 at 06:06
  • Yes and it says MyParseAdapter does not have type parameters. – frankie015 May 17 '15 at 06:07
  • @DanielNugent you still there ? if I declare the `MyParseAdapter adapter = new MyParseAdapter(getActivity(), factory);` any error is given. – frankie015 May 17 '15 at 06:13
  • Yep, I'm here. So is it working for you now? – Daniel Nugent May 17 '15 at 06:14
  • YES ! IS WORKING THANKS ! Now I can present my graduation project with a colored notification bar. How I can give you credit for answering this ? – frankie015 May 17 '15 at 06:17
  • Nice!!! I had no idea if the solution would work, I just coded it up that way I thought it would work after reading up on it. Glad it's working for you now! – Daniel Nugent May 17 '15 at 06:19
  • @AryamSaleh, the last problem with his code was fixed with the info that you provided, so go ahead and post an answer, you deserve the credit for this one! – Daniel Nugent May 17 '15 at 06:30
  • @DanielNugent I have been using stackoverflow for years but I made an account a few days ago. I'm going to be a graduate student in 1 week and I want to ask you a few things. Is there a way to send private messages or chat ? – frankie015 May 17 '15 at 06:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77998/discussion-between-daniel-nugent-and-frankie015). – Daniel Nugent May 17 '15 at 06:35

1 Answers1

2

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) {

        }
    });

}
Aryam Saleh
  • 338
  • 2
  • 12