F0xcr4f7 F0xcr4f7 - 6 months ago 17
Java Question

How to settext with Map<String, Object>?

ORIGINAL POST:

How do you set a textview with an object? I am pulling data from a webservice database and setting it to an object. I am then attempting to set the data in the object to a textview.

What I need help with:

1) setting a textview with Map String, Object

Previous post regarding webservice database: Populating a ListView with Kumulos?

UPDATING CODE:

The object:

static class Person{

public long personID;
public String lastName;
public String middleName;
public String firstName;
public String dateOfBirth;
public String personAddress;
public int phoneNumber;
public int driversLicense;
public int socialSecurity;
public String personRace;
public String personSex;
public String personAge;

public static Person createFromGenericMap(Map<String, Object> object) {

Person p = new Person();

p.personID = Long.valueOf(object.get("personID").toString());
p.lastName = (String) object.get("lastName");
p.middleName = (String) object.get("middleName");
p.firstName = (String) object.get("firstName");
p.dateOfBirth = (String) object.get("dob");
p.personAddress = (String) object.get("address");
p.phoneNumber = Integer.valueOf(object.get("phone").toString());
p.driversLicense = Integer.valueOf(object.get("dl").toString());
p.socialSecurity = Integer.valueOf(object.get("ss").toString());
p.personRace = (String) object.get("race");
p.personSex = (String) object.get("sex");
p.personAge = (String) object.get("age");

return p;
}
}


My adapter:

static class PersonAdapter extends BaseAdapter {

private List<Person> people;
private static LayoutInflater inflater = null;

public PersonAdapter(Context context, List<Person> people){
this.people = people;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


@Override
public int getCount() {
return people.size();
}

@Override
public Object getItem(int position) {
return people.get(position);
}

@Override
public long getItemId(int position) {
Person p = people.get(position);
return p.personID;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View view = convertView;

LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) {

view = inflater.inflate(R.layout.personlist_row, null);

TextView dl = (TextView) view.findViewById(R.id.tvdl);
TextView last = (TextView) view.findViewById(R.id.tvLastName);
TextView first = (TextView) view.findViewById(R.id.tvFirstName);
TextView middle = (TextView) view.findViewById(R.id.tvMiddleName);
TextView ss = (TextView) view.findViewById(R.id.tvSS);

Person mperson = people.get(position);

dl.setText(mperson.driversLicense);
last.setText(mperson.lastName);
first.setText(mperson.firstName);
middle.setText(mperson.middleName);
ss.setText(mperson.socialSecurity);
}


ERROR:

05-09 23:34:02.000 2418-2418/com.f0xcr4f7.intelwatch E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.f0xcr4f7.intelwatch, PID: 2418
android.content.res.Resources$NotFoundException: String resource ID #0x2d9f9219
at android.content.res.Resources.getText(Resources.java:299)
at android.widget.TextView.setText(TextView.java:4132)
at com.f0xcr4f7.intelwatch.PersonSearchPop$PersonAdapter.getView(PersonSearchPop.java:112)

Answer

As for me this part looks quite weird.

   --------------------------------------------------------------------
        Map<String, Object> mperson = new HashMap<String, Object>();
        mperson = people.get(position);

        dl.setText(mperson.getString("dl"));
    --------------------------------------------------------------------

You define mperson as HashMap, but you recieve a Person object from people, which is List of Person. So what I would do

 Person mperson = people.get(position);
 d1.setText(mPerson.driverLicense + "");