1

i develop Android app, which needs to get JSON data from outside API - and create the Dialog with MultiChoice options using this data.

I dont really know if it's good idea, but Im getting API data with BaseAdapter, and I want to send them into my custom DialogFragment class. Here is sample of my code and way of thinking:

AsyncTask where I download data and pass them into Adapter:

    public void onTaskComplete(JSONObject result) {

        MyCustomAdapter adapter = new MyCustomAdapter(...);

        try {
            adapter.setOptions(result.getJSONArray("data"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
...

Adapter class:

public class MyCustomAdapter extends BaseAdapter {

public int itemCount = 0;
public CharSequence[] options;
public Context context;

public MyCustomAdapter(Context c) {
    this.context = c;
}

public void setOptions(JSONArray data) {
    options = new CharSequence[data.length()];
    for(int i = 0; i < data.length(); i++) {
        JSONObject obj;

        try {
            obj = data.getJSONObject(i).getJSONObject("someObj");
            System.out.println(o.toString());
            this.options[i] = o.getString("someString");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
...

and there is CustomDialog class with static constructor here:

public class CustomDialog extends DialogFragment {

public static CustomDialog newInstance(CharSequence[] options) {
    CustomDialog f = new CustomDialog();

    Bundle args = new Bundle();
    args.putCharSequenceArray("options", options);
    f.setArguments(args);

    return f;
}
...

That's why I've already coded, I want these classes to work together, but I dont know if it's good design solution - using Adapter to pass data to DialogFragment. To display my dialog I need to put these in activity:

DialogFragment newFragment = CustomDialog.newInstance(someCharsequence);
newFragment.show(myActivity.this.getFragmentManager(), "somedialog");

And I dont know how to use them together in efficient and elegant way, or maybe should I rethink something? Thanks!

pawel
  • 5,976
  • 15
  • 46
  • 68

0 Answers0