如何访问页脚xml布局中的视图? Android的

时间:2013-04-11 07:55:46

标签: android xml android-layout footer

在我的代码中一切正常,直到我将此代码显示在其他代码中的任何位置,以便访问xml页脚布局中的TextView:

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

total_text是一个TextView,位于我用作页脚的xml文件中。

此脚注xml布局命名为alternative.xml,它显示在ListView的底部,该ListView使用不同的xml布局填充了适配器填充的其他项目。此页脚布局文件中唯一的内容是名为total_text的TextView。

如何在页脚xml布局中访问名为TextView的{​​{1}}?

以下是Logcat的堆栈跟踪:

total_text

以下是Activity类中的代码:

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)

ArrayAdapter类中嵌套在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);

编辑:这里要求的是来自alternative.xml的代码:

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

  }

2 个答案:

答案 0 :(得分:2)

根据堆栈跟踪,看起来您正试图调用totalTextView.setText()传递int参数。如果是这种情况,则参数为treated as a resource id。您可能需要使用

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

答案 1 :(得分:1)

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

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