0

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)

Community
  • 1
  • 1
F0xcr4f7
  • 61
  • 1
  • 1
  • 10

1 Answers1

1

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 + "");
thekekc
  • 206
  • 2
  • 12
  • Getting an error on the Person mperson = people.get(position);? Its saying that the variable is already defined in the scope. Only other place that is similar is the Person p = people.get(position); but why would that matter? – F0xcr4f7 May 01 '16 at 02:03
  • So is it worked with p = people.get(position)? If so it means that you have already declared mPerson, if you want to reuse mPerson and if it is of right type, remove type declaration `mPerson = people.get(position);` – thekekc May 01 '16 at 09:23
  • Ok, I am now able to come back to this and see if it works now but it does not. Ill update the original post. – F0xcr4f7 May 10 '16 at 03:28
  • Check if your textViews are not null and try to convert your int fields like phoneNumber to String while setting text for the textView. – thekekc May 10 '16 at 08:09
  • dl.setText(Integer.toString(mperson.driversLicense)); works but the IDE is suggesting that I use String.format instead? – F0xcr4f7 May 10 '16 at 15:39
  • you can just write dl.setText(mperson.driversLicense + ""); or String.valueOf(); or leave your way if it works. – thekekc May 10 '16 at 19:42