0

在我的应用程序中,我传递给适配器(扩展了 ArrayAdapter>)这样的列表:List<Map<String, String>>. 此列表中的单个记录如下所示:{received=true, text=Some sort of text}这是我的 getView 方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Map<String, String> singleRecord = new HashMap<String, String>();
    singleRecord = records.get(position);
    View rowView = convertView;
    if (rowView == null) {
        LayoutInflater layoutInflater = context.getLayoutInflater();
        rowView = layoutInflater.inflate(record_layout, null);
    }

    //To test get method I do:
    String to_put =  singleRecord.get(KEY_TEXT);
    Log.e("Adapter", to_put);
    ////

    TextView singleText = (TextView) rowView.findViewById(R.id.singleID);
    singleText.setText(singleRecord.get(KEY_TEXT));

    //viewHolder.singleText.setText(tekst);

    return super.getView(position, convertView, parent);
}

如您所见,我希望我的 TextView 设置我的地图包含的带有键的文本:“KEY_TEXT”;为了测试 get 方法是否正常工作,我将它放在 Log 中,它看起来正确,但是当我将 textview 的文本设置为我放入 LogCat 的相同文本(来自地图的文本)时,我没有得到键的值:“KEY_TEXT”,我得到的东西看起来像:{received=true, text=Some sort of text}所以它只是一条转换为字符串的记录。有人知道为什么会这样吗?

4

1 回答 1

0

请使用以下代码:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   Map<String, String> singleRecord = new HashMap<String, String>();
    View vi = convertView;
    singleRecord = records.get(position);
    vi = mLayoutInflater.inflate(R.layout.record_layout, null);

    TextView tv1 = (TextView) vi
            .findViewById(R.id.singleID);

    tv1.setText(singleRecord.get(KEY_TEXT));

    return vi;

}
于 2013-05-04T10:32:59.437 回答