0

Here is my class that works with parse.com API.

 public class ListActivity extends AppCompatActivity {
        RecyclerView recyclerView;
        LinearLayoutManager manager;
        List<Recipe> recipes;

    static int count = 0;

    private ProgressDialog progressDialog;

    NetworkInfo mWifi;
    ConnectivityManager connManager;
    NetworkInfo mInternet;

    Bitmap bmp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        recipes = new ArrayList<Recipe>();
        connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        mInternet = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        //Parse.enableLocalDatastore(getApplicationContext());

        // Enable Local Datastore.
        if(mWifi.isConnected() && count == 0 || mInternet.isConnected() && count == 0) {

            //Parse.initialize(getApplicationContext(), "1", "2");
            ParseQuery<ParseObject> query = ParseQuery.getQuery("Recipes");
            progressDialog = ProgressDialog.show(ListActivity.this, "",
                    "Update", true);

            query.findInBackground(new FindCallback<ParseObject>() {
                public void done(List<ParseObject> recipesList, ParseException e) {
                    if (e == null) {

                        ParseObject.pinAllInBackground(recipesList);
                        Log.d("score", "Retrieved " + recipesList.size() + " recipes");
                        for (ParseObject obj : recipesList) {
                            ParseFile image = (ParseFile) obj.get("Picture");
                            image.getDataInBackground(new GetDataCallback() {
                                public void done(byte[] data, ParseException e) {
                                    if (e == null) {
                                        bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
                                    } else {
                                        Log.d("mytag","coldnt load picture");
                                    }
                                }
                            });
                            initializeData(obj, bmp);
                        }
                    } else {
                        Log.d("score", "Error: " + e.getMessage());
                    }
                    initializeAdapter();
                    progressDialog.dismiss();
                }
            });
            count++;
        }if(!mWifi.isConnected() && !mInternet.isConnected() || count != 0) {//error on image

            ParseQuery<ParseObject> query = ParseQuery.getQuery("Recipes");
            query.fromLocalDatastore();
            query.findInBackground(new FindCallback<ParseObject>() {

                @Override
                public void done(List<ParseObject> list, ParseException e) {
                    if(e == null){
                        Log.d("score", "Retrieved " + list.size() + " recipes");
                        for(ParseObject obj : list){
                            initializeData(obj,bmp);
                        }
                    }else {
                        Log.d("score", "Error: " + e.getMessage());
                    }
                    initializeAdapter();
                }
            });
        }


        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        manager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(manager);

        //added to avoid no adapter set exception
        recipes = new ArrayList<>();
        RVAdapter adapter = new RVAdapter(recipes);
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    private void initializeAdapter(){
        RVAdapter adapter = new RVAdapter(recipes);
        recyclerView.setAdapter(adapter);
    }
    private void initializeData(ParseObject obj,Bitmap bmp){
        recipes.add(new Recipe(obj.getString("Description"), obj.getString("shortDescription"), obj.getString("Name"), bmp));
        Log.d("score", Integer.toString(recipes.size()));
    }
}
                 }

Edit 3 :The problem is that now i have 2 objects.But sometimes when you change the orientation they could not appear.As i think because adapter is set before data.Have no iea how to fix it.

I add my debug log to make you see what is going on.
08-30 18:14:01.943  31442-31442/by.test.roma.myapplication D/score﹕ 1
08-30 18:14:01.943  31442-31442/by.test.roma.myapplication D/score﹕ 2
08-30 18:14:02.088  31442-31442/by.test.roma.myapplication D/OpenGLRenderer﹕ Flushing caches (mode 0)
08-30 18:14:02.488  31442-31443/by.test.roma.myapplication D/dalvikvm﹕ GC_CONCURRENT freed 264K, 8% free 13277K/14279K, paused 28ms+3ms
08-30 18:14:03.113  31442-31442/by.test.roma.myapplication D/score﹕ Retrieved 2 recipes
08-30 18:14:03.403  31442-31443/by.test.roma.myapplication D/dalvikvm﹕ GC_CONCURRENT freed 367K, 8% free 13376K/14471K, paused 36ms+17ms
08-30 18:14:03.553  31442-31442/by.test.roma.myapplication D/picture﹕ 7345
08-30 18:14:03.608  31442-31442/by.test.roma.myapplication D/score﹕ 3
08-30 18:14:03.613  31442-31442/by.test.roma.myapplication D/picture﹕ 7345
08-30 18:14:03.653  31442-31442/by.test.roma.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 79K, 8% free 13435K/14599K, paused 40ms
08-30 18:14:03.658  31442-31442/by.test.roma.myapplication D/score﹕ 4
Void
  • 1,129
  • 1
  • 8
  • 21
  • IMO u have an asnyc problem with initializeData(obj, bmp); in that its not coordinated with the INNER callback that waits for inner query to provide data for instance of your bitmap. As is you are trying to instantiate the bitmap before the async has returned data from Parse query to get the FILE – Robert Rowntree Aug 30 '15 at 14:04
  • As i see if it would be so.The second pair of objects should be with image,but in reality there is no image in all list items.But il try your idea.Will see. – Void Aug 30 '15 at 14:51

0 Answers0