【发布时间】:2012-12-04 12:34:58
【问题描述】:
我有一个关于活动的片段,上面有一个列表视图元素。无法将数据添加到列表视图。谁能告诉我怎么了?
public class MainActivity extends FragmentActivity {
public String[] listS ={"item1","item2","item3"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
MainFragment f = new MainFragment();
ft.replace(R.id.fragcontainer, f);
ft.commit();
//here the problem occurs
ListView lv = (ListView)findViewById(R.id.listf);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row,R.id.labeld, listS));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
片段的类:
public class MainFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sis){
return inflater.inflate(R.layout.fragment_layout, container, false);
}
}
【问题讨论】:
-
您的
listf元素在哪里?在activity_main.xml? -
不,在片段的布局中...
-
使用 f.getView().findViewById();
标签: android android-listview android-fragmentactivity