Hafizudin Johari
Hafizudin Johari

Reputation: 63

Using Arraylist to display some of its data to Android setMultiChoiceItems(Alert Dialog)

I have a JSON file that stored in ArrayList using
public static ArrayList<UserData> userDataList = new ArrayList<>();
I also have a class namely as UserData to store multi data in it. Here is the code:

private String sid;
private String symp_name;
private String symp_part;

public UserData(String sid, String symp_name, String symp_part){
    this.sid = sid;
    this.symp_name = symp_name;
    this.symp_part = symp_part;
}

public String getSid() {
    return sid;
}
public String getSymp_name() {
    return symp_name;
}
public String getSymp_part() {
    return symp_part;
}

After requesting the JSON object, I use userDataList.add(data), to stored the data in array.

Now I would like to display symp_name into android setMultipleChoiceItem. I notice that setMultipleChoiceItem accept CharSequence as its parameter. So I have no idea on how to convert the symp_name into char sequence. I try to use this code:

ArrayList<UserData> spart = MainActivity.userDataList;
UserData symp = spart.get(spart.size());
CharSequence[] symptom = symp.getSymp_name();

Unfortunately, it does not working. Any help would be appreciated. Thanks :)

Upvotes: 3

Views: 323

Answers (2)

Renu Yadav
Renu Yadav

Reputation: 316

ArrayList<UserData> spart = MainActivity.userDataList;
UserData symp = spart.get(spart.size());// it will return a single object

CharSequence[] symptom = symp.getSymp_name(); // this will return a single name which you are trying to feed into Array.

You can create a Char array while traversing spart arraylist

CharSequence[] symtoms= new CharSequence[spart.size()] for(int i =0;i<spart.size();i++){ charSequences[i]= spart.get(i). getSymp_name(); }

And now set this charSequence to alertDialog using setMultichoice call. Hope this resolves your issue.

Upvotes: 2

leo liao
leo liao

Reputation: 81

Visit How to convert a String to CharSequence? This question can let you know how to convert the symp_name into char sequence. If your problem still wasn't solved.I suggest you can debug your project to see variable "symp" is correct or not

Upvotes: 0

Related Questions