【问题标题】:How to settext with Map<String, Object>?如何使用 Map<String, Object> 设置文本?
【发布时间】:2016-04-30 18:45:56
【问题描述】:

原帖:

如何设置带有对象的文本视图?我正在从 web 服务数据库中提取数据并将其设置为对象。然后我尝试将对象中的数据设置为文本视图。

我需要什么帮助:

1) 使用 Map String, Object 设置 textview

上一篇关于网络服务数据库的帖子:Populating a ListView with Kumulos?

更新代码:

对象:

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

我的适配器:

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

错误:

05-09 23:34:02.000 2418-2418/com.f0xcr4f7.intelwatch E/AndroidRuntime: 致命异常:主进程:com.f0xcr4f7.intelwatch,PID:2418 android.content.res.Resources$NotFoundException:字符串资源 ID '#0x2d9f9219 在 android.content.res.Resources.getText(Resources.java:299) 在 android.widget.TextView.setText(TextView.java:4132) 在 com.f0xcr4f7.intelwatch.PersonSearchPop$PersonAdapter.getView(PersonSearchPop.java:112)

【问题讨论】:

    标签: java android listview kumulos


    【解决方案1】:

    对我来说,这部分看起来很奇怪。

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

    您将 mperson 定义为 HashMap,但您从 people 那里收到了一个 Person 对象,即 List of Person。 那我该怎么办

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

    【讨论】:

    • 在 Person mperson = people.get(position); 上出现错误?它说变量已经在范围内定义。只有其他类似的地方是 Person p = people.get(position);但是这有什么关系呢?
    • 那么它是否与 p = people.get(position) 一起使用?如果是这样说明你已经声明了 mPerson,如果你想重用 mPerson 并且如果它是正确的类型,请删除类型声明mPerson = people.get(position);
    • 好的,我现在可以回到这个,看看它现在是否有效,但它没有。我会更新原帖。
    • 检查您的 textViews 是否不为空,并在为 textView 设置文本时尝试将您的 int 字段(如 phoneNumber)转换为 String。
    • dl.setText(Integer.toString(mperson.driversLicense));有效,但 IDE 建议我改用 String.format?
    猜你喜欢
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    相关资源
    最近更新 更多