适配器结果中的奇怪textview.setText()

时间:2013-05-04 10:27:00

标签: android map textview android-arrayadapter settext

在我的应用中,我传递给适配器(扩展了ArrayAdapter&gt;)这样的列表: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来设置我的地图所包含的文本:&#34; KEY_TEXT&#34 ;;为了测试get方法是否正常工作我把它放在Log中它看起来是正确的,但是当我将textview的文本设置为我放入LogCat(来自地图的文本)的相同文本时,我没有得到密钥的值:&#34; KEY_TEXT&#34;,我得到的内容如下:{received=true, text=Some sort of text}所以它只是一个转换为String的记录。有人知道为什么会这样吗?

1 个答案:

答案 0 :(得分: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;

}