我有一个扩展Activity并解析xml文件的类。我希望从xml文件中获取的文本作为参数传递给java类。我的问题是,在Java类中,我想从资源Layout文件夹中引用android TextViews,以便可以将文本设置为传入的String参数。我想我可以从Activity扩展java类,也可以将其作为争论当前活动...他们是唯一在res / layout中引用文件的人吗?

public class xmlClass extends Activity{
    //parse the xml
    MyDataFile mdf = new MyDataFile(arg1, arg2, arg3, arg4);
}

public class MyDataFile{
    public MyDataFile(arg1, arg2, arg3, arg4)
    {

    }
    ******* Here I want to set the Text in a TextView to arg1;
}

最佳答案

我看到两个选择:

首先是将MyDataFile设置为来自您的Activity的嵌套类,如下所示:

public class xmlClass extends Activity{

    //keep TextViews as member
    protected TextView mTextView

    public void onCreate(){
        ...
        setContentView(...)
        mTextView = (TextView)findViewById(R.id.your_text_id);

        //parse your XML
        MyDataFile mdf = new MyDataFile(arg1, arg2, arg3, arg4);
    }

    //make MyDataFile a nested class
    public class MyDataFile{

        public MyDataFile(String arg1,String arg2, ...){
            mTextView.setText(arg1);
        }
    }


第二种解决方案是像在此Constructor中一样,将TextViews作为参数提供

    public MyDataFile(String arg1, TextView textForArg1, ...){
        textForArg1.setText(arg1);
    }

09-10 21:42