当我通过MainActivity调用ListViewAdpater时,它工作正常。但是,如果我创建ListViewFragment它不会被调用。我在MainActivity中添加了ListViewFragment listViewFragment = new ListViewFragment();
。我有ListViewAdapter作为ListView适配器没有通过片段调用
public class ListViewAdapter extends ArrayAdapter<String> {
public ListViewAdapter(Context context, String[] sensorArray) {
super(context, R.layout.listview_adapter, sensorArray);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View view = layoutInflater.inflate(R.layout.listview_adapter, parent, false);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
TextView textView = (TextView) view.findViewById(R.id.textView);
imageView.setImageResource(R.drawable.image);
textView.setText(getItem(position));
return view;
}
}
但是,当我经过片段致电下面我看不出有任何的ListView
public class ListViewFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
String[] sensorArray = getResources().getStringArray(R.array.sensor_array);
ListView listView = (ListView) view.findViewById(R.id.listView);
listView.setAdapter(new ListViewAdapter(getActivity(),sensorArray));
return view;
}
}
当我运行调试器调用ListViewFragment但它不走onCreate方法内
我改变的xml文件如下 所以在activity_main.xml中我加`
<fragment android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment"
android:name="com.app.fragment.ListViewFragment"
tools:layout="@layout/listviewfragment"/>
and created listviewfragment.xml as
“
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
/>
这是正确的?
您可以将代码在其中添加ListViewFragment的活动? – Naveed 2015-04-02 19:31:17
我在MainActivity中添加了这行'ListViewFragment listViewFragment = new ListViewFragment();'这不行? – user2661518 2015-04-02 19:34:05
不是。我将添加一个答案 – Naveed 2015-04-02 19:35:44