Когда я вызываю ListViewAdpater через MainActivity, он отлично работает. Но если я создаю ListViewFragment, он не будет вызван. Я добавил ListViewFragment listViewFragment = new ListViewFragment();
в MainActivity. У меня есть 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
Я добавил эту строку в MainActivity 'ListViewFragment listViewFragment = new ListViewFragment();' Это не сработает? – user2661518
Нет. Я добавлю ответ – Naveed