Faced with a very ambiguous problem. Perhaps the solution lies in some small detail, but I just can not figure it out. There is a Fragment with a ListView. There is a database from where ArrayList<Class> is taken and sent to FragmentA adapter. At the first transition to this fragment A , the ListView is not displayed. If you return to the previous fragmentB and once again go to fragment A , then the ListView is displayed. What could be the problem? I rewrote the adapter several times, but the problem was not solved. Here is the fragment code:

 public class RegionListFrag extends android.support.v4.app.Fragment { ArrayList<String> names = new ArrayList<>(); RegionAddAdapter regionAddAdapter; ListView listView; DBHelper dbHelper; SQLiteDatabase db; final String LOG_TAG = "myLogs"; RegionNameClass regionNameClass; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); ((MainActivity)getActivity()).changeTbOn(); MainActivity.toolbar.setTitle("Регионы"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.region_layout, container, false); MainActivity.toolbar.setTitle("Регионы"); listView = (ListView) rootView.findViewById(R.id.region_lv); regionAddAdapter = new RegionAddAdapter(getActivity(), setRv()); listView.setAdapter(regionAddAdapter); listView.setPadding(0, 110, 0, 0); return rootView; } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_toolbar, menu); super.onCreateOptionsMenu(menu,inflater); Menu myMenu = menu; MenuItem nextItem = myMenu.findItem(R.id.accept_category); nextItem.setVisible(true); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { getFragmentManager().popBackStack(); getFragmentManager().beginTransaction().commit(); return true; } if (item.getItemId() == R.id.accept_category) { } return super.onOptionsItemSelected(item); } ArrayList<RegionNameClass> setRv(){ dbHelper = new DBHelper(getActivity()); db = dbHelper.getWritableDatabase(); Cursor c = db.query("regiontable", null, null, null, null, null, null); ArrayList<RegionNameClass> rvArray = new ArrayList<RegionNameClass>(); while(c.moveToNext()){ String name = c.getString(c.getColumnIndex("regNames")); RegionNameClass regionObj = new RegionNameClass(name); rvArray.add(regionObj); } Log.d(LOG_TAG, "кол-во массива - " + rvArray.size()); return rvArray; } } 

Adapter code:

 public class RegionAddAdapter extends ArrayAdapter<RegionNameClass> { private static ArrayList<RegionNameClass> list = new ArrayList<RegionNameClass>(); private final Activity context; public ArrayList<RegionNameClass> selectedStrings = new ArrayList<RegionNameClass>(); final String LOG_TAG = "myLogs"; RegionNameClass regionNameClass; public RegionAddAdapter(Activity context, ArrayList<RegionNameClass> top) { super(context, R.layout.region_row, list); this.context = context; list = top; } static class ViewHolder { protected TextView myTv; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = null; RegionNameClass myClass = getItem(position); if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.region_row, parent, false); } TextView tvName = (TextView) convertView.findViewById(R.id.region_tv); tvName.setText(myClass.name); return convertView; } } 
  • Nowhere in the code do you work with hiding and showing views? Perhaps the problem is this. Also, check with the logs if the data comes to the adapter for the first time - whalemare
  • And the getView method isn't it working with views? It seems that he just fills the adapter, or did I misunderstand something? - MrStuff88

2 answers 2

Try this:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.region_layout, container, false); MainActivity.toolbar.setTitle("Регионы"); listView = (ListView) rootView.findViewById(R.id.region_lv); regionAddAdapter = new RegionAddAdapter(getActivity(), setRv()); listView.setAdapter(regionAddAdapter); listView.setPadding(0, 110, 0, 0); regionAddAdapter.notifyDataSetChanged(); // <----------------------- return rootView; } 
  • My ListVIew does not recognize the notifyDataSetChanged(); method notifyDataSetChanged(); maybe this is the problem? - MrStuff88
  • regionAddAdapter.notifyDataSetChanged (); - Android Android
  • Yes, I mixed it up, I copied the wrong variable, I apologize, I'll fix it now. He wrote "by hand", and not in the android studio. - Ivan Dembicki

under the setRv () debag how many items returns for the first time? Try running the cursor like that

  if (cursor.moveToFirst()) { do { String name = c.getString(c.getColumnIndex("regNames")); RegionNameClass regionObj = new RegionNameClass(name); rvArray.add(regionObj); } while (cursor.moveToNext()); 
  • The fact of the matter is that with the first and the second times the same number returns .. - MrStuff88