簡體   English   中英

如何訪問頁腳xml布局內的視圖? 安卓系統

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

在我的代碼中一切正常,直到我將此處顯示的代碼放在其他代碼的任何位置,以訪問xml頁腳布局內的TextView:

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

total_text是一個TextView,位於我用作頁腳的xml文件中。

這種名為footer.xml的頁腳xml布局顯示在ListView的底部,該列表填充了適配器使用其他xml布局填充的其他項目。 此頁腳布局文件中唯一的東西是名為total_text的TextView。

如何在頁腳xml布局內訪問此名為total_text TextView

這是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)

這是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);

嵌套在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;
    }

  }

編輯:根據要求,這里是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>

根據堆棧跟蹤,看起來您正在嘗試通過int參數調用totalTextView.setText() 如果是這種情況,則將參數視為資源id 您可能需要使用

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

這應該為您解決問題,基本上TextViews setText方法需要一個String。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM