當我通過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