访问片段中的视图元素

时间:2014-04-26 19:11:19

标签: android android-fragments

我有一项从其他活动接收数据的活动。此数据必须在活动中膨胀的片段中的TextView中显示。所以,代码是:

public class DetailActivity extends Activity {

private int idEstablecimiento;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detail);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit(); //Here the fragment is added to the activity
    }

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        idEstablecimiento = extras.getInt("idEstablecimiento");
    }

    TextView textView = (TextView) this.findViewById(R.id.nombreView); //Declared in fragmentDetail, which has been already inflated at this point, but still null
    textView.setText(this.idEstablecimiento);   
}

//...
//Non related stuff...
//...

    public static class PlaceholderFragment extends Fragment {
    private int idEstablecimiento;
    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_detail,
                container, false);
        return rootView;
    }
}

问题是onCreate()方法中的textView为null,如果片段已经膨胀,为什么会发生这种情况呢?一旦片段在onCreate()方法中膨胀,我是否能够访问IU元素?或者如果没有,访问它的方法是什么?

2 个答案:

答案 0 :(得分:0)

如果你想从片段布局中获取视图,你应该使用片段的oncreateView中的视图,然后在oncreate视图中获取textView。

示例:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_detail,
            container, false);
    TextView textView = (TextView) rootView.this.findViewById(R.id.nombreView); //Declared in fragmentDetail, which has been already inflated at this point, but still null
textView.setText("I am here");
    return rootView;
}

答案 1 :(得分:0)

您必须在片段中找到TextView。

public static class PlaceholderFragment extends Fragment {
private int idEstablecimiento;
public PlaceholderFragment() {
}
public PlaceholderFragment(int idEstablecimiento) {
this.idEstablecimiento = idEstablecimiento;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_detail,
            container, false);
    TextView text = rootView.findViewById(R.id.nombreView);
    text.setText(idEstablecimiento);
    return rootView;
}

然后你可以获得文本并提交它。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);

Bundle extras = getIntent().getExtras();
if (extras != null) {
    idEstablecimiento = extras.getInt("idEstablecimiento");
}

if (savedInstanceState == null) {
    getFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment(idEstablecimiento)).commit(); 
}
}

此致 尼尔斯