title: ANDROID - Add new Items in a Fragment List (Tabbed) - Android Studio
body: <p>I am trying to make a simple tabbed application with different ListViews and options.
  I would like to manage the relationship between tabs and MainActivity and the problem right now is I cannot make an addItem() function in a menu button, to add a new item to my ListView (inside a Fragment) in a tabbed application.
  I already searched a lot, and I found only about adding an item in a simple ListView (inside MainActivity) or add items not dynamically in a tabbed application.</p>
  
  <p>So here it is the screen of my Tabbed Application and the ListView that I am trying to work with.
  <a href="https://i.stack.imgur.com/HSRdw.png" rel="nofollow noreferrer">Custom List View Application Picture</a></p>
  
  <p><a href="https://i.stack.imgur.com/HSRdw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HSRdw.png" alt="[1]: https://i.stack.imgur.com/Ze4eu.png"></a></p>
  
  <p>And here is my code: 
  MainActivity.java</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  
  
  private SectionsPagerAdapter mSectionsPagerAdapter;
  
  /**
   * The {@link ViewPager} that will host the section contents.
   */
  private ViewPager mViewPager;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      // Create the adapter that will return a fragment for each of the three
      // primary sections of the activity.
      mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
  
      // Set up the ViewPager with the sections adapter.
      mViewPager = (ViewPager) findViewById(R.id.container);
      mViewPager.setAdapter(mSectionsPagerAdapter);
  
      TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
      tabLayout.setupWithViewPager(mViewPager);
  
  }
  </code></pre>
  
  <p>The Fragment with the CustomList, that I am trying to implement the menu button method action_add():</p>
  
  <pre><code>    public class Catalogo extends Fragment {
      ArrayList&lt;CustomList&gt; custom = null;
      ListView lv = null;
      ArrayAdapter adapter = null;
      ArrayList&lt;CustomList&gt; elementos = new ArrayList&lt;CustomList&gt;();
  
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.catalogo, container, false);
          ListView lv = rootView.findViewById(R.id.catalogoListView);
  
          custom = addItems();
          adapter = new CustomAdapter(this.getContext(), custom);
          lv.setAdapter(adapter);
  
          return rootView;
      }
  
      private ArrayList&lt;CustomList&gt; addItems(){
          CustomList custom = new CustomList("Banana", "4.0");
          elementos.add(custom);
          custom = new CustomList("Morango", "5.0");
          elementos.add(custom);
          return elementos;
      }
  
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
  
          if (id == R.id.action_add){
              AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
              //builder.setTitle("Adicionar novo item"); //THESE ARE NOT WORKING
              //final EditText input = new EditText(this); //THIS SHOULD BE 2 Text Fields
              //builder.setView(input);
              builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      //NEED TO IMPLEMENT HERE
                  }
              });
              builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      dialog.cancel();
                  }
              });
              builder.show();
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
      public static String preferredCase(String original, String price)
      {
          if (original.isEmpty())
              return original;
  
          return original.substring(0, 1).toUpperCase() + original.substring(1).toLowerCase();
      }
  
  }
  </code></pre>
  
  <p>My CustomItem Structure:</p>
  
  <pre><code>   public class CustomList {
      String name;
      String price;
      public CustomList(String name, String price) {
          this.name = name;
          this.price = price;
      }
      public String getName() {
          return name;
      }
      public String getPrice() {
          return price;
      }
  }
  </code></pre>
  
  <p>And at the end, my CustomAdapter:</p>
  
  <pre><code>    public class CustomAdapter extends ArrayAdapter&lt;CustomList&gt; {
      private final Context context;
      private final ArrayList&lt;CustomList&gt; elementos;
  
      public CustomAdapter(Context context, ArrayList&lt;CustomList&gt; elementos){
          super(context, R.layout.modelolista, elementos);
          this.context = context;
          this.elementos = elementos;
      }
      public View getView(int position, View convertView, ViewGroup parent){
          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View rowView = inflater.inflate(R.layout.modelolista, parent, false);
  
          TextView nameItem =  rowView.findViewById(R.id.tvName);
          TextView priceItem =  rowView.findViewById(R.id.tvPrice);
  
          nameItem.setText(elementos.get(position).getName());
          priceItem.setText(elementos.get(position).getPrice());
  
          return rowView;
      }
  }
  </code></pre>