Change text value in inflate layout

Hawkins picture Hawkins · Feb 3, 2011 · Viewed 10.6k times · Source

I have custom adapter in which i inflate layout. In adapter i try to change some text value in TextView (@id/datetextcal)

LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            cellView = inflater.inflate(R.layout.calendarcell, null);


         TextView dateText = (TextView) cellView.findViewById(R.id.datetextcal);
             dateText.setText(position);

I think that's all right but i have an error! why ?

Without modify this value all compiles and work correct.

code for layout(R.layout.calendarcell):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:background="@drawable/cell"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="40dp"
    android:layout_height="40dp">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">
        <TextView
            android:id="@+id/datetextcal"
            android:text="2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <ImageView
            android:layout_alignRight="@id/datetextcal"
            android:paddingRight="2dp"
            android:src="@drawable/dropper"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
    <TextView
        android:gravity="center_horizontal"
        android:textSize="9dp"
        android:text="заметка"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

code for adapter class:

package com.android.wcalendar;

import java.util.Date;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ImageAdapter extends BaseAdapter
{
    private static final String TAG = "trace";
    private Context             mContext;

    public ImageAdapter(Context c)
    {
        // TODO Auto-generated constructor stub
        mContext = c;
    }

    @Override
    public int getCount()
    {
        // TODO Auto-generated method stub
        return 7 * 5;
    }

    @Override
    public Object getItem(int arg0)
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0)
    {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View cellView = convertView;
        if (cellView == null)
        { // if it's not recycled, initialize some attributes
            cellView = new View(mContext);
            Log.d(TAG, "cellView null");
        } else
        {
            Log.d(TAG, "cellView not null");
            cellView = (View) convertView;
        }
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        cellView = inflater.inflate(R.layout.calendarcell, null);

        // Date date = new Date();
        // date.setDate(position);

         TextView dateText = (TextView)
         cellView.findViewById(R.id.datetextcal);
         dateText.setText(position);
        return cellView;
    }

}

and that errors :

02-03 11:58:45.774: ERROR/AndroidRuntime(286): Uncaught handler: thread main exiting due to uncaught exception
02-03 11:58:45.845: ERROR/AndroidRuntime(286): android.content.res.Resources$NotFoundException: String resource ID #0x0
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.content.res.Resources.getText(Resources.java:200)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.TextView.setText(TextView.java:2813)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at com.android.wcalendar.ImageAdapter.getView(ImageAdapter.java:67)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.AbsListView.obtainView(AbsListView.java:1274)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.GridView.onMeasure(GridView.java:934)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.View.measure(View.java:7964)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.View.measure(View.java:7964)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:619)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:280)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.View.measure(View.java:7964)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.View.measure(View.java:7964)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.View.measure(View.java:7964)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.View.measure(View.java:7964)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:888)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:350)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.View.measure(View.java:7964)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.View.measure(View.java:7964)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.View.measure(View.java:7964)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:464)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:278)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.View.measure(View.java:7964)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3023)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.View.measure(View.java:7964)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.ViewRoot.performTraversals(ViewRoot.java:763)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.os.Looper.loop(Looper.java:123)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.app.ActivityThread.main(ActivityThread.java:4363)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at java.lang.reflect.Method.invoke(Method.java:521)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at dalvik.system.NativeStart.main(Native Method)

Answer

Bert F picture Bert F · Feb 3, 2011
02-03 11:58:45.845: ERROR/AndroidRuntime(286): android.content.res.Resources$NotFoundException: String resource ID #0x0
...
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at android.widget.TextView.setText(TextView.java:2813)
02-03 11:58:45.845: ERROR/AndroidRuntime(286):     at com.android.wcalendar.ImageAdapter.getView(ImageAdapter.java:67)

suggests there's something wrong with:

     dateText.setText(position);

Why would this lead to a message that says resource id 0x0 could not be found?

You're calling the resource version of setText(int) with an integer value that is not a valid resource id. Did you mean to call the string version of setText(String)?:

     dateText.setText(Integer.toString(position)); // or just setText(position+"")