更改膨胀布局中的文本值

发布于 2024-10-15 08:51:23 字数 9647 浏览 4 评论 0原文

我有自定义适配器,可以在其中充气布局。 在适配器中,我尝试更改 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);

我认为这没问题,但我有一个错误!为什么 ?

如果不修改此值,所有编译和工作都会正确。

布局代码(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>

适配器类代码:

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

}

并且错误:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱人如己 2024-10-22 08:51:23
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)

表明有问题:

     dateText.setText(position);

为什么这会导致一条消息说无法找到资源 id 0x0

您正在调用 setText(int) 的整数值不是有效的资源 ID。您是否想调用 setText(String) 的字符串版本?:

     dateText.setText(Integer.toString(position)); // or just setText(position+"")
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+"")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文