如何将json数组转换为对话框片段

时间:2017-10-12 08:01:39

标签: java android

我在dailog片段中遇到了问题。我想在json片段中获得dailog数组,但我不知道该怎么做,这就是我在尝试的内容。任何建议?

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();
    }
};

我应该如何从模型类中获取数据。

2 个答案:

答案 0 :(得分:1)

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;
 }
}

如果这是你的模型类,那么获取数据:

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

答案 1 :(得分:0)

首先,你得到的json格式是无效的json格式发送json格式,如

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

不喜欢 - “用户”:[{“username”:“ashish”,“userid”:“97”}]

之后将其映射到类

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;
        }

    }
}

比传递那个str值并获得像

这样的Pojo类
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 
      }
     }
   }
}

对于单选适配器,请参阅此link