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.
So here it is the screen of my Tabbed Application and the ListView that I am trying to work with. Custom List View Application Picture
And here is my code: MainActivity.java
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);
}
The Fragment with the CustomList, that I am trying to implement the menu button method action_add():
public class Catalogo extends Fragment {
ArrayList<CustomList> custom = null;
ListView lv = null;
ArrayAdapter adapter = null;
ArrayList<CustomList> elementos = new ArrayList<CustomList>();
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<CustomList> 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();
}
}
My CustomItem Structure:
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;
}
}
And at the end, my CustomAdapter:
public class CustomAdapter extends ArrayAdapter<CustomList> {
private final Context context;
private final ArrayList<CustomList> elementos;
public CustomAdapter(Context context, ArrayList<CustomList> 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;
}
}