简体   繁体   English

如何访问页脚xml布局内的视图? 安卓系统

[英]How can I access the views inside of a footer xml layout? Android

Everything works fine in my code until I put this code shown here anywhere in the other code to get access to the TextView inside the xml footer layout: 在我的代码中一切正常,直到我将此处显示的代码放在其他代码的任何位置,以访问xml页脚布局内的TextView:

  TextView totalTextView = (TextView) footer.findViewById(R.id.total_text);
  totalTextView.setText(totalPrice);

total_text is a TextView that is located in the xml file that I use as a footer. total_text是一个TextView,位于我用作页脚的xml文件中。

This footer xml layout named, alternative.xml shows up at the bottom of the ListView that is populated with other items filled by the adapter using a different xml layout. 这种名为footer.xml的页脚xml布局显示在ListView的底部,该列表填充了适配器使用其他xml布局填充的其他项目。 the only thing inside this footer layout file is the TextView named total_text . 此页脚布局文件中唯一的东西是名为total_text的TextView。

How can I access this TextView called total_text inside the footer xml layout? 如何在页脚xml布局内访问此名为total_text TextView

Here is the stacktrace from Logcat: 这是Logcat的堆栈跟踪:

FATAL EXCEPTION`: main
java.lang.RuntimeException: Unable to start activityComponentInfo{com.forever.scrollcheck/com.forever.scrollcheck.MainActivity}:
android.content.res.Resources$NotFoundException: String resource ID #0x543a8
    E/AndroidRuntime(8208): at adroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
    E/AndroidRuntime(8208): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    E/AndroidRuntime(8208): at android.app.ActivityThread.access$600(ActivityThread.java:123)
    E/AndroidRuntime(8208): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    E/AndroidRuntime(8208): at android.os.Handler.dispatchMessage(Handler.java:99)
    E/AndroidRuntime(8208): at android.os.Looper.loop(Looper.java:137)
    E/AndroidRuntime(8208) :at android.app.ActivityThread.main(ActivityThread.java:4424)
    E/AndroidRuntime(8208): at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime(8208): at java.lang.reflect.Method.invoke(Method.java:511)
    E/AndroidRuntime(8208): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

Here is the code in the Activity class: 这是Activity类中的代码:

 listView = (ListView) findViewById(R.id.listView);

  View footer = getLayoutInflater().inflate(R.layout.alternative, null);

  TextView totalTextView = (TextView) footer.findViewById(R.id.total_text);
  totalTextView.setText(totalPrice);

  listView.addFooterView(footer); 

  MobileArrayAdapter adapter = new MobileArrayAdapter(this, R.layout.row_layout, ckbInfo);
  listView.setAdapter(adapter);

code in the ArrayAdapter class that is nested inside of the Activity class: 嵌套在Activity类内部的ArrayAdapter类中的代码:

  public class MobileArrayAdapter extends ArrayAdapter<CheckBoxInfo>{
      CheckBoxInfo[] objects;
      Context context;
      int textViewResourceId;

    public MobileArrayAdapter(Context context, int textViewResourceId,
    CheckBoxInfo[] objects) {
       super(context, textViewResourceId, objects);
       this.context = context;
       this.textViewResourceId = textViewResourceId;
       this.objects = objects;

       }

 @Override
 public View getView(final int position, View convertView, ViewGroup parent) {
 View row_layout_view = convertView;

 if ((row_layout_view == null)){


  LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  row_layout_view = inflater.inflate(R.layout.row_layout, null);
            }               
 //CheckBoxInfo item = objects.get(position);  // for arrayList
 CheckBoxInfo item = objects[position];

 if(item != null){

 TextView textView = (TextView) row_layout_view.findViewById(R.id.textView1);
 final CheckBox checkBox = (CheckBox) row_layout_view.findViewById(R.id.checkBox1);
 TextView priceTextView = (TextView) row_layout_view.findViewById(R.id.price1);

 checkBox.setOnClickListener(new OnClickListener() {

 @Override
    public void onClick(View arg0) {
    final boolean isChecked = checkBox.isChecked();
    if(isChecked==true){
 Toast.makeText(MainActivity.this,"box is checked for position " + position, Toast.LENGTH_SHORT).show();
 }else if(isChecked==false){
 Toast.makeText(MainActivity.this,"box is NOT checked for " + position, Toast.LENGTH_SHORT).show();
     }
   }
      });

   if(item !=null){
      textView.setText(item.checkBoxName);
      checkBox.setChecked(item.checkBoxState);
      priceTextView.setText(String.valueOf(item.price));
    }
      }
    return row_layout_view;
    }

  }

EDIT: As requested here is the code from alternative.xml: 编辑:根据要求,这里是alternative.xml的代码:

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="center" >

  <TextView
    android:id="@+id/total_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="XXXXXX"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

According to stack trace, it looks like you are trying to call totalTextView.setText() passing int argument. 根据堆栈跟踪,看起来您正在尝试通过int参数调用totalTextView.setText() If this is the case, argument is treated as a resource id . 如果是这种情况,则将参数视为资源id You probably need to use 您可能需要使用

totalTextView.setText(Integer.toString(totalPrice));

这应该为您解决问题,基本上TextViews setText方法需要一个String。

totalTextView.setText(String.valueOf((totalPrice));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:如何拥有XML布局以及添加到布局中的视图? - Android : How can I have XML layout and the views that I added to my layout? 如何动态将布局和视图添加到Android XML布局文件? - How can I dynamically add layouts and views to an Android XML layout file? 如何从XML布局访问平台资源(android.R.layout)? - How Can I Access Platform Resources (android.R.layout) from XML Layout? 如何访问android xml布局文件中包含的视图内的元素? - how to access elements inside an included view in a android xml layout file? 如何禁用布局内的所有视图? - How can I disable all views inside the layout? 如何在带有视图的android studio中移动布局? 我一直在选择里面的视图 - How to move a layout in android studio with views inside? I keep selecting the views inside 如何通过单击Android中的按钮来更改布局中的视图? - How can I change Views in Layout by clicking a button in Android? 如何修复我的 android 应用程序中的布局视图? - How can I fix the layout views in my android app? 如何使用Android Studio在应用程序内的xml文件布局中实现? - How can I implement from xml file layout inside the App using Android studio? 如何使用布局XML文件中定义的视图作为模板来以编程方式创建视图 - How can i make use of views defined in layout XML file as template to create views programmatic way
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM