Search code examples
androiddictionarytextviewandroid-arrayadaptersettext

Weird textview.setText() inside adapter result


In my app I'm passing to adapter (which extends ArrayAdapter>) such list: List<Map<String, String>>. Single record from this list looks like: {received=true, text=Some sort of text} Here's my getView method:

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

As you can see I want my TextView to setText which my map contains with key: "KEY_TEXT"; To test if get method works fine I put it in Log and it looks right, but when I'm setting text of my textview to the same text I put in LogCat (text from the map) I'm not getting value for the key : "KEY_TEXT", I'm getting something which looks like: {received=true, text=Some sort of text} so it's simply one record converted to String. Does anybody has idea why this is happening ?


Solution

  • Please use below code:

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