subhash
subhash

Reputation: 41

how to get json array into dialog fragment

I have an issue in a dailog fragment. I want to get a json array into the dailog fragment, but I don't know how to do it, here is what am trying. Any suggetion's ?

public class MyAlertSortDialogFragmentIndividual extends DialogFragment {
public static final String DATA = "items";
public static final String SELECTED = "selected";
CharSequence[] cs;
ArrayList<User> transactionList = new ArrayList<>();
List <User> users;
String s;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
     users=new ArrayList<>();
    Bundle bundle = getArguments();
    AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
    dialog.setTitle("Sort By");
    dialog.setPositiveButton("Cancel", new PositiveButtonClickListener());
    bundle.putSerializable("key", transactionList);

    String str = bundle.getString("str");

    users = new Gson().fromJson(str,ArrayList.class);


    return dialog.create();
}
class PositiveButtonClickListener implements DialogInterface.OnClickListener {
    @Override
    public void onClick(DialogInterface dialog, int which) {

        dialog.dismiss();

    }
}

DialogInterface.OnClickListener selectItemListener = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // process
        //which means position

        dialog.dismiss();
    }
};

how should I get the data from model class.

Upvotes: 0

Views: 440

Answers (2)

Sushant Gosavi
Sushant Gosavi

Reputation: 3835

First the json format you get is not valid json format send json format like

 {
    "Users": [
        {
            "username": "ashish",
            "userid": "97"
        }
    ]
}

not like -"Users": [ { "username": "ashish", "userid": "97" } ]

after that map this into class

public class UsersResponce implements Serializable {

    @SerializedName("Users")
    @Expose
    private List<User> users = null;

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public class User  implements Serializable{

        @SerializedName("username")
        @Expose
        private String username;
        @SerializedName("userid")
        @Expose
        private String userid;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getUserid() {
            return userid;
        }

        public void setUserid(String userid) {
            this.userid = userid;
        }

    }
}

Than pass that str value and get Pojo Class like

UsersResponce users = new Gson().fromJson(str,UsersResponce.class);
if(null!=UsersResponce){
   List<User> usersList =UsersResponce.getUsers();
   if(null!=usersList&&usersList.size()>0){
     for (User item : usersList) {
      if(item.getUserid().equals(CURRENT_LOGIN_USERID)){
         //than set your adapter selcted here 
      }
     }
   }
}

For single choice adapter refer this link

Upvotes: 0

Sharath kumar
Sharath kumar

Reputation: 4132

public class NewItem {

private String name;
private String age;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAge() {
    return age;
}

public void setAge(String age) {
    this.age = age;
 }
}

If this is your model class then to get data:

 String userName,age;
 for (NewItem item : users) {
      if(item.getName().equals(REQUIRED_USERNAME)){
         userName = item.getName();
         age = item.getAge();
      }
  }

Upvotes: 1

Related Questions