简体   繁体   中英

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:

  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.

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. the only thing inside this footer layout file is the TextView named total_text .

How can I access this TextView called total_text inside the footer xml layout?

Here is the stacktrace from 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:

 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:

  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:

  <?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. If this is the case, argument is treated as a resource id . You probably need to use

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM