kamilsparrow
kamilsparrow

Reputation: 63

Button ID's inside OnPostExecute

I am trying to use else if statement inside OnPostExecute so when a button is clicked API with data is downloaded, a correct title (in this case Honda or Suzuki) fetched from String source file, both of them are put in a bundle and transferred to another activity. Then the title is used as the title on my DisplayDataActivity and data is put into TextView. So I use the same activity to display my data, just with different titles and data. There are 20 buttons in total but once I know how to get id's for two of them I will extend it to the rest of them. However I cannot pass my button IDs inside OnPostExecute (so I can select the correct else_if statement based on the button id clicked). Is there any way to do that? I would be super grateful for any help!

!UPDATED CODE:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public class Wrap
{
    public String dataPassedInOnClick;
    public String resultData;
}

public class JSONTask extends AsyncTask<String, String, Wrap> {
    private Context mContext;
    private View rootView;
    ProgressDialog pDialog;
    JSONTask(Context mContext) {
        this.mContext = mContext;
    }

    public JSONTask(Context context, View rootView){
        this.mContext=context;
        this.rootView=rootView;
    }

    @Override
    protected Wrap doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();
            String line = "";

            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            Wrap wrapper=new Wrap();
            wrapper.dataPassedInOnClick=params[0];
            wrapper.resultData=buffer.toString();

            return wrapper;


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPreExecute(){
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Loading data...");
        pDialog.show();
    }
    protected void onPostExecute(Wrap wrapResult) {
        super.onPostExecute(wrapResult);
        this.pDialog.dismiss();

        if (wrapResult.dataPassedInOnClick.contains("api_suzuki")) {
            String activity_title =
                    getResources().getString(R.string.suzuki);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
        else if (wrapResult.dataPassedInOnClick.contains("api_honda")) {
            String activity_title =
                    getResources().getString(R.string.honda);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
        else if (wrapResult.dataPassedInOnClick.contains("api_kawasaki")) {
            String activity_title =
                    getResources().getString(R.string.kawasaki);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
        else if (wrapResult.dataPassedInOnClick.contains("api_triumph")) {
            String activity_title =
                    getResources().getString(R.string.triumph);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
        else if (wrapResult.dataPassedInOnClick.contains("api_bmw")) {
            String activity_title =
                    getResources().getString(R.string.bmw);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
        else if (wrapResult.dataPassedInOnClick.contains("api_hyousung")) {
            String activity_title =
                    getResources().getString(R.string.hyousung);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
        else if (wrapResult.dataPassedInOnClick.contains("api_ducati")) {
            String activity_title =
                    getResources().getString(R.string.ducati);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
        else if (wrapResult.dataPassedInOnClick.contains("api_harley")) {
            String activity_title =
                    getResources().getString(R.string.harley);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
        else if (wrapResult.dataPassedInOnClick.contains("api_aprilia")) {
            String activity_title =
                    getResources().getString(R.string.aprilia);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
        else if (wrapResult.dataPassedInOnClick.contains("api_ktm")) {
            String activity_title =
                    getResources().getString(R.string.ktm);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
        else if (wrapResult.dataPassedInOnClick.contains("api_mv")) {
            String activity_title =
                    getResources().getString(R.string.mv);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
        else if (wrapResult.dataPassedInOnClick.contains("api_bajaj")) {
            String activity_title =
                    getResources().getString(R.string.bajaj);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
        else if (wrapResult.dataPassedInOnClick.contains("royal")) {
            String activity_title =
                    getResources().getString(R.string.royal);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
        else if (wrapResult.dataPassedInOnClick.contains("api_hero")) {
            String activity_title =
                    getResources().getString(R.string.hero);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
        else if (wrapResult.dataPassedInOnClick.contains("api_indian")) {
            String activity_title =
                    getResources().getString(R.string.indian);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
    }
}
public void onSuzuki(View view) {
    JSONTask objJSONTask=new JSONTask(MainActivity.this);
    objJSONTask.execute("api_suzuki");
}
public void onHonda(View view) {
    JSONTask objJSONTask=new JSONTask(MainActivity.this);
    objJSONTask.execute("api_honda");
}

public void onKawasaki(View view) {
    JSONTask objJSONTask=new JSONTask(MainActivity.this);
    objJSONTask.execute("api_kawasaki");
}
public void onTriumph(View view) {
    JSONTask objJSONTask=new JSONTask(MainActivity.this);
    objJSONTask.execute("api_triumph");
}

public void onBmw(View view) {
    JSONTask objJSONTask=new JSONTask(MainActivity.this);
    objJSONTask.execute("api_bmw");
}
public void onHyuosung(View view) {
    JSONTask objJSONTask=new JSONTask(MainActivity.this);
    objJSONTask.execute("api_hyuosung");
}
public void onDucati(View view) {
    JSONTask objJSONTask=new JSONTask(MainActivity.this);
    objJSONTask.execute("api_ducati");
}
public void onHarley(View view) {
    JSONTask objJSONTask=new JSONTask(MainActivity.this);
    objJSONTask.execute("api_harley");
}
public void onAprilia(View view) {
    JSONTask objJSONTask=new JSONTask(MainActivity.this);
    objJSONTask.execute("api_aprilia");
}
public void onKtm(View view) {
    JSONTask objJSONTask=new JSONTask(MainActivity.this);
    objJSONTask.execute("api_ktm");
}
public void onMv(View view) {
    JSONTask objJSONTask=new JSONTask(MainActivity.this);
    objJSONTask.execute("api_mv");
}
public void onBajaj(View view) {
    JSONTask objJSONTask=new JSONTask(MainActivity.this);
    objJSONTask.execute("api_bajaj");
}
public void onRoyal(View view) {
    JSONTask objJSONTask=new JSONTask(MainActivity.this);
    objJSONTask.execute("api_royal");
}
public void onHero(View view) {
    JSONTask objJSONTask=new JSONTask(MainActivity.this);
    objJSONTask.execute("api_hero");
}
public void onIndian(View view) {
    JSONTask objJSONTask=new JSONTask(MainActivity.this);
    objJSONTask.execute("api_indian");
}

Upvotes: 0

Views: 48

Answers (1)

Brandon Zamudio
Brandon Zamudio

Reputation: 2873

You are passing and getting the rootView for every case in your onPostExecute, when your are trying to get the id by calling int id = view.getId (); you are actually getting the id of the rootView every time, so you have to pass the view of every button or figure out another way to do it.

What you can do in this case is compare the string that you are passing in the onClick method, since you already are passing a different string for every case, you already knows from where is called your JSONTask method, so you don't need the view of the button pressed because it would be redundant.

Try this:

public class Wrap
{
    public String dataPassedInOnClick;
    public String resultData;
}

public class JSONTask extends AsyncTask<String, String, Wrap> {
    private Context mContext;
    private View rootView;
    ProgressDialog pDialog;
    JSONTask(Context mContext) {
        this.mContext = mContext;
    }

    public JSONTask(Context context, View rootView){
        this.mContext=context;
        this.rootView=rootView;
    }

    @Override
    protected Wrap doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();
            String line = "";

            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            Wrap wrapper=new Wrap();
            wrapper.dataPassedInOnClick=params[0];
            wrapper.resultData=buffer.toString();

            return wrapper;


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPreExecute(){
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Loading data...");
        pDialog.show();
    }
    protected void onPostExecute(Wrap wrapResult) {
        super.onPostExecute(wrapResult);
        this.pDialog.dismiss();

        if (wrapResult.dataPassedInOnClick.contains("Honda") ||
                wrapResult.dataPassedInOnClick.contains("honda")) {
            String activity_title =
                    getResources().getString(R.string.Honda);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        } else if (wrapResult.dataPassedInOnClick.contains("Suzuki") ||
                wrapResult.dataPassedInOnClick.contains("suzuki")) {
            String activity_title =
                    getResources().getString(R.string.Suzuki);
            Intent intent = new Intent(MainActivity.this,
                    DisplayDataActivity.class);
            Bundle extras = new Bundle();
            extras.putString("title", activity_title);
            extras.putString("JSON_Object", wrapResult.resultData);
            intent.putExtras(extras);
            startActivity(intent);
        }
    }
}

We have to define a class Wrap because the doInBackground method only can return a single object.

Upvotes: 1

Related Questions