1

I need to know how to do that?

I have two views

  • ActivityMain.java
  • FilmActivity

In MainActivity, I created an intent to get some information from the second view:

 @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        CustomGridviewAdapter customGridviewAdapter = new CustomGridviewAdapter(filmList, getApplicationContext());
        simpleGrid.setAdapter(customGridviewAdapter);
        simpleGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getApplicationContext(), FilmActivity.class);
                intent.putExtra("FilmBoster", filmList.get(position).getBackdrop_path())
                        .putExtra("FilmImage", filmList.get(position).getPoster_path())
                        .putExtra("FilmName", filmList.get(position).getTitle())
                        .putExtra("FilmDate", filmList.get(position).getRelease_date())
                        .putExtra("FilmDisc", filmList.get(position).getOverview())
                        .putExtra("isFavFilm", filmList.get(position).getIsLiked());
                startActivityForResult(intent, 2);
            }
        });
    }

Second view :

 private void sendDataToMainActivity(String isPressed) {
    Intent intent = new Intent();
    intent.putExtra("isPressed" , isPressed);
    setResult(1 , intent);
    finish();
}

I have used AsyncTask In MainActivity. The second activity sent the data in onActivityResult but ( onActivityResult ) execute After AsyncTask and in AsyncTask, I set some data on DB. So that the data that returned from the Second Activity is equal to null.

Some codes to help

 @NonNull
    private String convertToString(InputStream in) {
        String res = "";
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder sd = new StringBuilder();
        try {
            while ((res = reader.readLine()) != null) {
                sd.append(res).append("/n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        parseString(sd.toString());
        db.userDao().updateFilmList(filmList);
        return sd.toString();
    }

  private void parseString(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray("results");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject filmObject = jsonArray.getJSONObject(i);
                Film film = new Film(filmObject.getString("title")
                        ,filmObject.getString("overview")
                        , filmObject.getString("poster_path")
                        , filmObject.getString("release_date")
                        ,db.userDao().getIsFave());
                filmList.add(film);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

This is two methods inside AsyncTask that save data to db

What should I do if I want the data return on AsyncTask and set it on DB?

luadham
  • 17
  • 4

2 Answers2

0

Do the following up in your onCreate or onResume methods in your MainActivity class:

    // filmList should be an empty array at this point
    CustomGridviewAdapter customGridviewAdapter = new CustomGridviewAdapter(filmList, getApplicationContext());
    simpleGrid.setAdapter(customGridviewAdapter);
    simpleGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getApplicationContext(), FilmActivity.class);
            intent.putExtra("FilmBoster", filmList.get(position).getBackdrop_path())
                    .putExtra("FilmImage", filmList.get(position).getPoster_path())
                    .putExtra("FilmName", filmList.get(position).getTitle())
                    .putExtra("FilmDate", filmList.get(position).getRelease_date())
                    .putExtra("FilmDisc", filmList.get(position).getOverview())
                    .putExtra("isFavFilm", filmList.get(position).getIsLiked());
            startActivityForResult(intent, 2);
        }
    });

Then, in your postExecute method, you just need to do this (assuming you have repopulated your filmList in the Async Task):

    CustomGridviewAdapter customGridviewAdapter = new CustomGridviewAdapter(filmList, getApplicationContext());
    simpleGrid.setAdapter(customGridviewAdapter);

The onItemClicked handler will send the item clicked on to the FilmActivity.java class activity. In the onCreate of that activity, you need to read the data passed to it from the MainActivity:

 public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

 setContentView(R.layout.film_activity_layout); 

 FilmName = (String) getIntent().getExtra("FilmName");

 ...
 }

You need to show the onActivityResult code from the MainActivity

Michael Dougan
  • 1,698
  • 1
  • 9
  • 13
  • Ok that's true but how i could to set this data in db ?? – luadham Sep 06 '19 at 18:08
  • I assume that the rest of the AsyncTask has code for retrieving data into the filmList array? It would help a lot to see that code. That code is connecting to and reading from the database, likely using an http GET request. To add data to the database you would make an http POST request, and to update data already in the database, you would make an http PUT request. All of this assumes that you are using a RESTful API to access the database. – Michael Dougan Sep 06 '19 at 19:42
-1

Do you have to use Async Task? I would suggest to use RxJava instead.

However, you can create a class that extends AsyncTask and send the Context in the constructor in order to setResult and finish the current activity in onPostExecute.

Example : https://stackoverflow.com/a/16921076/12009871

Tinchoob
  • 1
  • 1