更改膨胀布局中的文本值
我有自定义适配器,可以在其中充气布局。 在适配器中,我尝试更改 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
表明有问题:
为什么这会导致一条消息说无法找到资源 id
0x0
?您正在调用 setText(int) 的整数值不是有效的资源 ID。您是否想调用
setText(String)
的字符串版本?:suggests there's something wrong with:
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)
?: