title: Display Listview in Fragment
body: <p>I am working on an Android app with Tab navigation. I have three tabs and I would like to display separate informations on each tab. On the first tab, I would like to display a list of items that are retrieved from an SQlite database. The items are entered through dialogs, which is working well. I keep the logic for the collection and display of data in the Main Activity: </p>
  
  <pre><code>package com.example.TodoList;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.database.Cursor;
  import android.app.AlertDialog;
  import android.widget.SimpleCursorAdapter;
  import android.database.sqlite.SQLiteDatabase;
  import android.location.LocationManager;
  import android.net.Uri;
  import android.os.Bundle;
  import android.support.design.widget.TabLayout;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ListAdapter;
  import android.widget.ListView;
  import android.widget.TextView;
  
  import com.example.TodoList.db.TaskContract;
  import com.example.TodoList.db.TaskDBHelper;
  import com.example.TodoList.fragments.ThreeFragment;
  import com.example.TodoList.fragments.TwoFragment;
  
  import java.util.ArrayList;
  import java.util.List;
  
  
  public class MainActivity extends AppCompatActivity {
  
      private Toolbar toolbar;
      private ListAdapter listAdapter;
      private TaskDBHelper helper;
      private Button btnIconTextTabs;
      private TabLayout tabLayout;
      private ViewPager viewPager;
      private int[] tabIcons = {
              R.drawable.ic_tab_favourite,
              R.drawable.ic_tab_call,
              R.drawable.ic_tab_contacts
      };
  
  
      private void setupTabIcons() {
          tabLayout.getTabAt(0).setIcon(tabIcons[0]);
          tabLayout.getTabAt(1).setIcon(tabIcons[1]);
          tabLayout.getTabAt(2).setIcon(tabIcons[2]);
      }
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_icon_text_tabs);
          //ListView listView = (ListView) findViewById(R.id.list);
          //listView.setAdapter(listAdapter);
          updateUI();
          toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
          btnIconTextTabs = (Button) findViewById(R.id.btnIconTextTabs);
          //btnIconTextTabs.setOnClickListener(this);
          setSupportActionBar(toolbar);
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  
          viewPager = (ViewPager) findViewById(R.id.viewpager);
          setupViewPager(viewPager);
  
          tabLayout = (TabLayout) findViewById(R.id.tabs);
          tabLayout.setupWithViewPager(viewPager);
          setupTabIcons();
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.menu, menu);
          return true;
      }
  
      private void setupViewPager(ViewPager viewPager) {
          ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
          adapter.addFrag(new Fragment(), "ONE");
          adapter.addFrag(new TwoFragment(), "TWO");
          adapter.addFrag(new ThreeFragment(), "THREE");
          viewPager.setAdapter(adapter);
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          final EditText inputField = new EditText(this);
          builder.setNegativeButton("Cancel", null);
  
  
          switch (item.getItemId()) {
              case R.id.action_add_task:
                  builder.setTitle("Add an article to your shopping list");
                  builder.setMessage("What would you like to add?");
                  builder.setView(inputField);
                  builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
                          String task = inputField.getText().toString();
                          helper = new TaskDBHelper(MainActivity.this);
                          SQLiteDatabase db = helper.getWritableDatabase();
                          ContentValues values = new ContentValues();
                          values.clear();
                          values.put(TaskContract.Columns.TASK, task);
                          db.insertWithOnConflict(TaskContract.TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE);
                          updateUI();
                      }
                  });
  
                  builder.create().show();
                  return true;
  
              case R.id.action_remove_task:
                  builder.setTitle("Remove an article from the shopping list");
                  builder.setMessage("Did you found this article?");
                  builder.setNegativeButton("Remove", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
                          String task = inputField.getText().toString();
                          helper = new TaskDBHelper(MainActivity.this);
                          SQLiteDatabase db = helper.getWritableDatabase();
                          ContentValues values = new ContentValues();
                          values.clear();
                          updateUI();
                      }
                  });
  
              case R.id.action_show_mylocation:
                  LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                  Log.d("MyTagGoesHere", "This is my log message at the debug level here");
                  //Intent intent=new Intent(this,LbsGeocodingActivity.class);
                  //startActivity(intent);
                  Intent GeoLocationIntent = new Intent(MainActivity.this, GeoActivity.class);
                  //myIntent.putExtra("key", value); //Optional parameters
                  MainActivity.this.startActivity(GeoLocationIntent);
  
          }
          builder.create().show();
          return true;
      }
  
      private void updateUI() {
          helper = new TaskDBHelper(MainActivity.this);
          SQLiteDatabase sqlDB = helper.getReadableDatabase();
          Cursor cursor = sqlDB.query(TaskContract.TABLE,
                  new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK},
                  null, null, null, null, null);
  
          listAdapter = new SimpleCursorAdapter(
                  this,
                  R.layout.task_view,
                  cursor,
                  new String[]{TaskContract.Columns.TASK},
                  new int[]{R.id.taskTextView},
                  0
          );
      }
  
  
      public void onDoneButtonClick(View view) {
          View v = (View) view.getParent();
          TextView taskTextView = (TextView) v.findViewById(R.id.taskTextView);
          String task = taskTextView.getText().toString();
  
          String sql = String.format("DELETE FROM %s WHERE %s = '%s'",
                  TaskContract.TABLE,
                  TaskContract.Columns.TASK,
                  task);
  
  
          helper = new TaskDBHelper(MainActivity.this);
          SQLiteDatabase sqlDB = helper.getWritableDatabase();
          sqlDB.execSQL(sql);
          updateUI();
      }
  
      public void onSubmitPriceClick(View view) {
          Intent SubmitPriceIntent = new Intent(MainActivity.this, SubmitPriceActivity.class);
          MainActivity.this.startActivity(SubmitPriceIntent);
      }
  
      public void onWebViewButtonClick(View view) {
          Intent intent = new
                  Intent(Intent.ACTION_VIEW,
                  Uri.parse("http://www.batprice.com:1337"));
          startActivity(intent);
          finish();
      }
  
  
      public void onGeoLocationButtonClick(View view) {
          LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          Log.d("MyTagGoesHere", "This is my log message at the debug level here");
          Intent GeoLocationIntent = new Intent(MainActivity.this, GeoActivity.class);
          MainActivity.this.startActivity(GeoLocationIntent);
      }
  
  
      /*@Override
      public void onClick(View view) {
          switch (view.getId()) {
              case R.id.btnIconTextTabs:
                  startActivity(new Intent(MainActivity.this, IconTextTabsActivity.class));
                  break;
          }
      }*/
  
  
      class ViewPagerAdapter extends FragmentPagerAdapter {
          private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
          private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
  
          public ViewPagerAdapter(FragmentManager manager) {
              super(manager);
          }
  
          @Override
          public Fragment getItem(int position) {
              return mFragmentList.get(position);
          }
  
          @Override
          public int getCount() {
              return mFragmentList.size();
          }
  
          public void addFrag(Fragment fragment, String title) {
              mFragmentList.add(fragment);
              mFragmentTitleList.add(title);
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
              return mFragmentTitleList.get(position);
          }
  
  
      }
  }
  </code></pre>
  
  <p>This is my First fragment, where I want to display the list items: </p>
  
  <pre><code>package com.example.TodoList.fragments;
  
  import android.content.Context;
  import android.database.SQLException;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.widget.Toast;
  
  import com.example.TodoList.R;
  
  import java.util.ArrayList;
  
  
  public class OneFragment extends FragmentActivity {
  
      public OneFragment() {
          // Required empty public constructor
      }
  
      private class mylocationlistener implements LocationListener {
          @Override
          public void onLocationChanged(Location location) {
              if (location != null) {
                  Log.d("LOCATION CHANGED", location.getLatitude() + "");
                  Log.d("LOCATION CHANGED", location.getLongitude() + "");
                  Toast.makeText(OneFragment.this,
                          location.getLatitude() + "" + location.getLongitude(),
                          Toast.LENGTH_LONG).show();
              }
          }
          @Override
          public void onProviderDisabled(String provider) {
          }
          @Override
          public void onProviderEnabled(String provider) {
          }
          @Override
          public void onStatusChanged(String provider, int status, Bundle extras) {
          }
      }
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          LocationListener ll = new mylocationlistener();
          lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
  
          return inflater.inflate(R.layout.fragment_one, container, false);
      }
  
      /*
      public static class OneFragment extends Fragment {
  
          ListView list;
          list = (ListView) view.findViewById(R.id.listview);
          DataDB data = new DataDB();
  
          ArrayAdapter&lt;String&gt; listAdapter;
  
          public ListDoctorFragment() {
          }
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
              final View view = inflater.inflate(R.layout.listdoctor, container, false);
  
              ArrayList&lt;String&gt; names = new ArrayList&lt;String&gt;();
              try {
                  names = data.getDoctorlistDB(getActivity());
              } catch (SQLException e) {
                  e.printStackTrace();
              }
  
              listAdapter = new ArrayAdapter&lt;String&gt;(getActivity(), R.layout.support_simple_spinner_dropdown_item, names);
  
              // set the adapter
              list.setAdapter(listAdapter);
  
              return view;
          }
      }
      */
  }
  </code></pre>
  
  <p>This is the activity icon text tabs layout file: </p>
  
  <pre><code>&lt;android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;android.support.design.widget.AppBarLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"&gt;
  
          &lt;android.support.v7.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="?attr/colorPrimary"
              app:layout_scrollFlags="scroll|enterAlways"
              app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /&gt;
  
          &lt;android.support.design.widget.TabLayout
              android:id="@+id/tabs"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:tabGravity="fill"
              app:tabMode="fixed" /&gt;
      &lt;/android.support.design.widget.AppBarLayout&gt;
  
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/viewpager"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          app:layout_behavior="@string/appbar_scrolling_view_behavior" /&gt;
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p>This is my main activity layout file: </p>
  
  <pre><code>&lt;android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;android.support.design.widget.AppBarLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"&gt;
  
          &lt;android.support.v7.widget.Toolbar
              android:id="@+id/toolbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:background="?attr/colorPrimary"
              app:layout_scrollFlags="scroll|enterAlways"
              app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /&gt;
      &lt;/android.support.design.widget.AppBarLayout&gt;
  
     &lt;LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="match_parent"
          android:layout_marginTop="?attr/actionBarSize"
          android:orientation="vertical"&gt;
  
          &lt;ListView
              android:id="@android:id/list"
              android:layout_width="match_parent"
              android:layout_height="350dp"
              android:layout_margin="5dp" /&gt;
  
          &lt;Button
              android:id="@+id/btnIconTextTabs"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="@string/btn_icon_text_tabs"
              android:textSize="15dp" /&gt;
  
      &lt;/LinearLayout&gt;
  
  
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p>This is my main layout file: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"&gt;
  
      &lt;ListView
          android:id="@android:id/list"
          android:layout_width="match_parent"
          android:layout_height="350dp"
          android:layout_margin="5dp" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="clip_vertical"
          android:onClick="onGeoLocationButtonClick"
          android:text="Your Location" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="clip_vertical"
          android:onClick="onSubmitPriceClick"
          android:text="Submit a price" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="clip_vertical"
          android:onClick="onScrollViewButtonClick"
          android:text="Scrollview" /&gt;
  
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="clip_vertical"
          android:onClick="onWebViewButtonClick"
          android:text="Webview" /&gt;
  
      &lt;WebView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/webView1"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>This is my Fragment one layout file: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"&gt;
  
      &lt;ListView
          android:id="@android:id/list"
          android:layout_width="match_parent"
          android:layout_height="350dp"
          android:layout_margin="5dp" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="clip_vertical"
          android:onClick="onGeoLocationButtonClick"
          android:text="Your Location" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="clip_vertical"
          android:onClick="onSubmitPriceClick"
          android:text="Submit a price" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="clip_vertical"
          android:onClick="onScrollViewButtonClick"
          android:text="Scrollview" /&gt;
  
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="clip_vertical"
          android:onClick="onWebViewButtonClick"
          android:text="Webview" /&gt;
  
      &lt;WebView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/webView1"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>I would like to display the entered list items - this whole processus is working well - in fragment One, but the list items are not showing up. I don´t get any errors so I´m a bit stuck here. Any help or hints would be very appreciated, thanks. </p>