0

Another class calls the GridView class below that is populated with images from a parse server, if a user clicks on a grid item, it starts the same GridView class with different bundled strings so it can pull from a different parse database class. Now at this point, when a user clicks an GridView item (from the 2nd gridview that was set up), I want it to start a different activity class.

I tried doing an if/else if statement in the onItemClick that takes the "PARSE_CLASS" bundled string but I can't seem to get that to work. I'm relatively new to android programming so I don't know the best way to do this.

    public class DisplayGrid extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    GridView gridView = null;
    List<ParseObject> obj;
    ProgressDialog loadProgress;
    GridViewAdapter adapter;
    Bundle extras = new Bundle();
    GridViewAdapter itemGridAdapter;
    private List<ImageList> imageArrayList = null;
    private List<String> categoryNameArrayList = null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ws_display_grid);
        imageArrayList = new ArrayList<ImageList>();
        categoryNameArrayList = new ArrayList<String>();
        gridView = (GridView) findViewById(R.id.gridView);
        itemGridAdapter = new GridViewAdapter(DisplayGrid.this, imageArrayList);
        gridView.setAdapter(itemGridAdapter);



        new RemoteDataTask().execute();

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            Intent i = getIntent();
            Bundle itemExtras = i.getExtras();
            final String activeSupplier = itemExtras.getString("SUPPLIER");
            String parseClass = extras.getString("PARSE_CLASS");

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int position,
                                    long arg3) {
                    Intent t = new Intent(getApplicationContext(), DisplayGrid.class);
                    extras.putString("SUPPLIER", activeSupplier);
                    extras.putString("PARSE_CLASS", "Items");
                    t.putExtras(extras);
                    startActivity(t);

            }
        });
    }//end onCreate method


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }


    //RemoteDataTask
    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loadProgress = new ProgressDialog(DisplayGrid.this);
            loadProgress.setTitle("Images");
            loadProgress.setMessage("Loading...");
            loadProgress.setIndeterminate(false);
            loadProgress.show();
        }

        @Override
        protected Void doInBackground(Void... params) {


            try {
                Intent i = getIntent();
                Bundle extras = i.getExtras();

                String parseClass = extras.getString("PARSE_CLASS");
                String activeSupplier = extras.getString("SUPPLIER");

                String category;

                ParseQuery<ParseObject> query = new ParseQuery<>(parseClass);
                query.whereEqualTo("username", activeSupplier);
                obj = query.find();
                if (parseClass == "Items") {
                    category = extras.getString("CATEGORY");
                    query.whereEqualTo("category", category);

                }
                for (ParseObject categories : obj) {
                    //get image
                    ParseFile image = (ParseFile) categories.get("image");
                    ImageList gridBlock = new ImageList();
                    gridBlock.setImage(image.getUrl());
                    imageArrayList.add(gridBlock);
                    Log.i("AppInfo", "image sent to imageArrayList");

                    String categoryName = null;
                    //get category name
                    if (categoryName == null) {

                    } else {
                        categoryName = categories.getString("categoryName");
                        categoryNameArrayList.add(categoryName);
                        Log.i("AppInfo", categoryName);
                    }
                }
            } catch (ParseException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            gridView = (GridView) findViewById(R.id.gridView);
            adapter = new GridViewAdapter(DisplayGrid.this, imageArrayList);
            gridView.setAdapter(adapter);
            loadProgress.dismiss();
            Log.i("AppInfo", "CategoryGrid Populated");
        }


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
            Uri selectedImage = data.getData();
            try {
                Bitmap bitmapImage =
                        MediaStore.Images.Media.getBitmap
                                (this.getContentResolver(), selectedImage);

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();

                //convert to parse file
                ParseFile file = new ParseFile("Img.png", byteArray);

                ParseObject object = new ParseObject("Categories");
                object.put("username", ParseUser.getCurrentUser().getUsername());
                object.put("image", file);

                object.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        if (e == null) {
                            Toast.makeText(getApplication().getBaseContext(), "Your image has been saved", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getApplication().getBaseContext(), "Upload failed, please try again", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(getApplication().getBaseContext(), "Upload failed, please try again", Toast.LENGTH_SHORT).show();
            }
        }
    }

    //begin getTitle method

    //begin createMenu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(getApplication())
                .inflate(R.menu.options, menu);

        return (super.onCreateOptionsMenu(menu));
    }
    //end createMenu

    //begin onOptionsItemSelected
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        if (item.getItemId() == R.id.manufacturers) {
            Intent i = new Intent(getApplicationContext(), SupplierList.class);
            startActivity(i);
            return true;
        } else if (item.getItemId() == R.id.add) {
            Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, 1);
            return true;
        } else if (item.getItemId() == R.id.sign_out) {
            ParseUser.logOut();

            Intent i = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(i);
            return true;
        }

        return (super.onOptionsItemSelected(item));
    }
    //end onOptionsItemSelected
}

This is what I tried. When I tried this, the second GridView wouldn't load. So I'm guessing I'm unable to retrieve the "PARSE_CLASS" bundled string the second time around.

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        Intent i = getIntent();
        Bundle itemExtras = i.getExtras();
        final String activeSupplier = itemExtras.getString("SUPPLIER");
        String parseClass = extras.getString("PARSE_CLASS");

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position,
                                long arg3) {
            if (parseClass == "Categories") {
                Intent t = new Intent(getApplicationContext(), DisplayGrid.class);
                extras.putString("SUPPLIER", activeSupplier);
                extras.putString("PARSE_CLASS", "Items");
                t.putExtras(extras);
                startActivity(t);
            } else if(parseClass == "Items"){
                Intent t = new Intent(getApplicationContext(), ItemDisplay.class);
                //extras.putString("SUPPLIER", activeSupplier);
                //extras.putString("PARSE_CLASS", "Items");
                //t.putExtras(extras);
                startActivity(t);
            }

        }
    }); 

ANSWER Someone on reddit's learn programming was nice enough to point out that I had an issue with string comparisons. had to use

if(parseClass.equals("Categories")){}

instead of

if(parseClass == "Categories"){}

I feel like an idiot, hopefully someone else benefits from this long disastrous effort.

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        Intent t = getIntent();
        Bundle itemExtras = t.getExtras();
        String activeSupplier = itemExtras.getString("SUPPLIER");
        String parseClass = itemExtras.getString("PARSE_CLASS");

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position,
                                long arg3) {
            Log.i("AppInfo", parseClass);

            if (parseClass.equals("Categories")) {
                Log.i("AppInfo", parseClass);
                Intent t = new Intent(getApplicationContext(), DisplayGrid.class);
                String categoryName = categoryNameArrayList.get(position);

                itemExtras.putString("SUPPLIER", activeSupplier);
                itemExtras.putString("PARSE_CLASS", "Items");
                itemExtras.putString("CATEGORY_NAME", categoryName);
                t.putExtras(itemExtras);
                startActivity(t);
            } else if (parseClass.equals("Items")) {
                Intent t = new Intent(getApplicationContext(), ItemDisplay.class);
                //extras.putString("SUPPLIER", activeSupplier);
                //extras.putString("PARSE_CLASS", "Items");
                //t.putExtras(extras);
                startActivity(t);
            }
        }
    });
GCQ
  • 190
  • 3
  • 11
  • question is little bit confusing ,can you give and example what you want to do – Amit Ranjan Mar 31 '16 at 04:58
  • when you want to stay in same activity and when you want to go to different activity – Amit Ranjan Mar 31 '16 at 05:00
  • activity1(not shown here) -> DisplayGrid (populates the gridview) -> user clicks a grid item -> DisplayGrid (populates 2nd gridview) -> user clicks a 2nd gridview grid item -> activity2 – GCQ Mar 31 '16 at 05:41

1 Answers1

0
public class DisplayGrid extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    GridView gridView = null;
    List<ParseObject> obj;
    ProgressDialog loadProgress;
    GridViewAdapter adapter;
    Bundle extras = new Bundle();
    GridViewAdapter itemGridAdapter;
    private List<ImageList> imageArrayList = null;
    private List<String> categoryNameArrayList = null;
    String activeSupplier;
    String parseClass;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ws_display_grid);
        imageArrayList = new ArrayList<ImageList>();
        categoryNameArrayList = new ArrayList<String>();
        Intent i = getIntent();
        if (null != i) {
            Bundle itemExtras = i.getExtras();
            activeSupplier = itemExtras.getString("SUPPLIER");
            parseClass = itemExtras.getString("PARSE_CLASS");
        }
        RemoteDataTask remoteDataAsync = new RemoteDataTask();
        remoteDataAsync.execute();

        if(remoteDataAsync.getStatus() == AsyncTask.Status.PENDING){
            // My AsyncTask has not started yet
        }

        if(remoteDataAsync.getStatus() == AsyncTask.Status.RUNNING){
            // My AsyncTask is currently doing work in doInBackground()
        }

        if(remoteDataAsync.getStatus() == AsyncTask.Status.FINISHED){
            // My AsyncTask is done and onPostExecute was called
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


                @Override
                public void onItemClick(AdapterView<?> arg0, View v, int position,
                                        long arg3) {
                    Intent t = new Intent(getApplicationContext(), DisplayGrid.class);
                    extras.putString("SUPPLIER", activeSupplier);
                    extras.putString("PARSE_CLASS", parseClass);
                    t.putExtras(extras);
                    startActivity(t);

                }
            });
        }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }


    //RemoteDataTask
    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loadProgress = new ProgressDialog(DisplayGrid.this);
            loadProgress.setTitle("Images");
            loadProgress.setMessage("Loading...");
            loadProgress.setIndeterminate(false);
            loadProgress.show();
        }

        @Override
        protected Void doInBackground(Void... params) {


            try {
                Intent i = getIntent();
                Bundle extras = i.getExtras();

                String parseClass = extras.getString("PARSE_CLASS");
                String activeSupplier = extras.getString("SUPPLIER");

                String category;

                ParseQuery<ParseObject> query = new ParseQuery<>(parseClass);
                query.whereEqualTo("username", activeSupplier);
                obj = query.find();
                if (parseClass == "Items") {
                    category = extras.getString("CATEGORY");
                    query.whereEqualTo("category", category);

                }
                for (ParseObject categories : obj) {
                    //get image
                    ParseFile image = (ParseFile) categories.get("image");
                    ImageList gridBlock = new ImageList();
                    gridBlock.setImage(image.getUrl());
                    imageArrayList.add(gridBlock);
                    Log.i("AppInfo", "image sent to imageArrayList");

                    String categoryName = null;
                    //get category name
                    if (categoryName == null) {

                    } else {
                        categoryName = categories.getString("categoryName");
                        categoryNameArrayList.add(categoryName);
                        Log.i("AppInfo", categoryName);
                    }
                }
            } catch (ParseException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            adapter = new GridViewAdapter(DisplayGrid.this, imageArrayList);
            gridView.setAdapter(adapter);
            loadProgress.dismiss();
            Log.i("AppInfo", "CategoryGrid Populated");

        }//end onCreate method
        }


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
            Uri selectedImage = data.getData();
            try {
                Bitmap bitmapImage =
                        MediaStore.Images.Media.getBitmap
                                (this.getContentResolver(), selectedImage);

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();

                //convert to parse file
                ParseFile file = new ParseFile("Img.png", byteArray);

                ParseObject object = new ParseObject("Categories");
                object.put("username", ParseUser.getCurrentUser().getUsername());
                object.put("image", file);

                object.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        if (e == null) {
                            Toast.makeText(getApplication().getBaseContext(), "Your image has been saved", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getApplication().getBaseContext(), "Upload failed, please try again", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(getApplication().getBaseContext(), "Upload failed, please try again", Toast.LENGTH_SHORT).show();
            }
        }
    }

    //begin getTitle method

    //begin createMenu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(getApplication())
                .inflate(R.menu.options, menu);

        return (super.onCreateOptionsMenu(menu));
    }
    //end createMenu

    //begin onOptionsItemSelected
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        if (item.getItemId() == R.id.manufacturers) {
            Intent i = new Intent(getApplicationContext(), SupplierList.class);
            startActivity(i);
            return true;
        } else if (item.getItemId() == R.id.add) {
            Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, 1);
            return true;
        } else if (item.getItemId() == R.id.sign_out) {
            ParseUser.logOut();

            Intent i = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(i);
            return true;
        }

        return (super.onOptionsItemSelected(item));
    }
    //end onOptionsItemSelected
}
Amit Ranjan
  • 567
  • 2
  • 11
  • in gridview onItemClickListner you are sending wrong intent ,Why u are passing items ,extras.putString("SUPPLIER", activeSupplier); extras.putString("PARSE_CLASS", "Items"),here you have found String parseClass = extras.getString("PARSE_CLASS"); and you are getting parseClass here and you are putting extras.putString("PARSE_CLASS", "Items") this , Secondly why are you finding this id twice gridView = (GridView) findViewById(R.id.gridView); – Amit Ranjan Apr 01 '16 at 16:33
  • In the **onItemClickListener**, I'm passing "Items" for **"PARSE_CLASS"** because I want to overwrite the "Categories" that was previously set so that I can use the **PARSE_CLASS** to query a different set of items from a different parse class from the database. The **"SUPPLIER"** I assumed I needed to pass the same thing again since I use it to find which categories and items (both separate classes in the database) belong to which supplier that uploaded the item. The extra gridView was a mistake, thanks for catching that. – GCQ Apr 01 '16 at 17:34
  • ok, so the extra gridView seems to be needed for the program to not crash, I don't understand why though. If I comment it out, the program crashes the on the first run. – GCQ Apr 01 '16 at 17:38
  • you have to initialise the GridView gridview; as a global and find that id on in onCreateView ,then you can use anywhere .I think you are not assigning the GridView as a Global – Amit Ranjan Apr 01 '16 at 17:40