Wilson
Wilson

Reputation: 95

List comes back as being empty after retrieving data from Parse.com? No error messages? Just no data?

Well I've run into an issue in my inventory app. I'm trying to retrieve a list of inventory items from Parse. This isn't the hardest thing in the world to do. At this point, I'm at a loss as to why the data is coming back as empty, when I can clearly see in Parse.com that there is data in the class I have requested from. Any ideas? (NOTE: I am able to add items to the database without a problem... it's just in the retrieval).

MainActivity:

public class MainActivity extends AppCompatActivity {

    private ImageView mAddButton;
    private ImageView mBackButton;
    private Inventory mInventory;
    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAddButton = (ImageView) findViewById(R.id.addItemButton);
        mBackButton = (ImageView) findViewById(R.id.backButton);
        mBackButton.setVisibility(View.INVISIBLE);
        mInventory = new Inventory();

        ParseUser user = ParseUser.getCurrentUser();
        if (user == null) {
            navToLogin();
        } else {
            Toast.makeText(MainActivity.this, "Welcome!", Toast.LENGTH_SHORT).show();
            getInventoryFromParse();
            Toast.makeText(MainActivity.this, mInventory.toString(), Toast.LENGTH_LONG).show();
        }

        mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addItem();
            }
        });
    }


    private void updateView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        InventoryListAdapter adapter = new InventoryListAdapter(this, mInventory.getItemList());
        mRecyclerView.setAdapter(adapter);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
    }

    private void addItem() {
        Intent intent = new Intent(MainActivity.this, AddItemActivity.class);
        startActivityForResult(intent, 1);
    }

    private void navToLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case (1):
                    updateView();
            }
        }
    }

    public void getInventoryFromParse() {
        ParseQuery<ParseObject> query = ParseQuery.getQuery("Item");
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {
                    mInventory.setItemList(objects);
                } else {
                    Toast.makeText(MainActivity.this, "There was an error.", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

The Inventory Class:

public class Inventory {

    private List<ParseObject> mItemList;

    public Inventory() {
        mItemList = new ArrayList<>();
    }

    public List<ParseObject> getItemList() {
        return mItemList;
    }

    public void setItemList(List<ParseObject> itemList) {
        mItemList = itemList;
    }

    public void addItem(ParseObject item) {
        mItemList.add(item);
    }

    @Override
    public String toString() {
        return "Inventory{" +
                "mItemList=" + mItemList +
                '}';
    }
}

Upvotes: 0

Views: 38

Answers (1)

Jake T.
Jake T.

Reputation: 4378

The query creates a new thread which runs in the background, then your main thread moves on, exits the function, and the query still hasn't completed when you go to print out the inventory. setInventory has not been called when the main thread prints mInventory to string.

That's why your code isn't working.

As for a solution, I'm not sure how the Android dev kit works, but my suggestion to keep your code split up the way it is would be to make getInventoryFromParse have a return type, and call return inside of the query callback. I'm not sure if that'll throw errors since the main thread reaches the end of the function... If that doesn't work, you'll have to rewrite your code so that anything that needs to happen after the items are fetched happens inside of the callback.

Upvotes: 1

Related Questions