I'm currently writing a tip tracker application for my Mobile App Development class that will accept user input from EditText fields and save the information to a database. Each time the "Save" button is clicked, all entries from the entire layout are added into a single listView that is displayed in a different layout. I have the database working, to where I can add and delete the elements, but I'm wondering if there is a way to get the amount of listViews that are currently displayed? I know there are ways to count the number of rows in a database, but is it possible to get the amount of listViews?
This is how my layout currently looks. Each listView is a pizza delivery that I have taken and all of the information is displayed in each one. I'm wondering if it's possible to get the amount of listViews on the page so that I can create a TextView below the "DEL" button that says "Delivery # " and have the current amount of entries.
My code that adds each one to the database is as follows:
public void onClick(View view){
name = (EditText) findViewById(R.id.name);
number = (EditText) findViewById(R.id.number);
address = (EditText) findViewById(R.id.address);
orderTotal = (EditText) findViewById(R.id.orderTotal);
amountReceived = (EditText) findViewById(R.id.amountReceived);
tip = (EditText) findViewById(R.id.tip);
mileage = (EditText) findViewById(R.id.mileage);
grandTotal = (EditText) findViewById(R.id.grandTotal);
String cName = name.getText().toString();
String num = number.getText().toString();
String cAddress = address.getText().toString();
String cOrderTotal = orderTotal.getText().toString();
String cAmountReceived = amountReceived.getText().toString();
String cTip = tip.getText().toString();
String cMileage = mileage.getText().toString();
String cGrandTotal = grandTotal.getText().toString();
int id = db.addContact(new PhoneBook(cName, num, cAddress, cOrderTotal,
cAmountReceived, cTip, cMileage, cGrandTotal));
contactList.add(new PhoneBook(id, cName, num, cAddress, cOrderTotal,
cAmountReceived, cTip, cMileage, cGrandTotal));
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Entry Successfully Created.", Toast.LENGTH_LONG).show();
}
If there is anything that I'm missing that would point me in the right direction to obtain a solution from someone I will gladly add it. Thank you for your time.
EDIT:
My code for the custom adapter:
@Override
public int getCount() {
return contactList.size();
}
@Override
public Object getItem(int position) {
return contactList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.contact_entry, null);
TextView name= (TextView) convertView.findViewById(R.id.contactName);
TextView contact = (TextView) convertView.findViewById(R.id.contactNumber);
TextView address = (TextView) convertView.findViewById(R.id.addressConverted);
TextView orderTotal = (TextView) convertView.findViewById(R.id.orderTotalConverted);
TextView amountReceived = (TextView) convertView.findViewById(R.id.amountReceivedConverted);
TextView tip = (TextView) convertView.findViewById(R.id.tipConverted);
TextView mileage = (TextView) convertView.findViewById(R.id.mileageConverted);
TextView grandTotal = (TextView) convertView.findViewById(R.id.grandTotalConverted);
Button delete = (Button) convertView.findViewById(R.id.delete);
PhoneBook list = contactList.get(position);
name.setText(list.getName());
contact.setText(list.getPhoneNumber());
address.setText(list.getAddress());
orderTotal.setText(list.getOrderTotal());
amountReceived.setText(list.getAmountReceived());
tip.setText(list.getTip());
mileage.setText(list.getMileage());
grandTotal.setText(list.getGrandTotal());
delete.setOnClickListener(new ListItemClickListener(position, list));
return convertView;
}
private class ListItemClickListener implements View.OnClickListener {
int position;
PhoneBook list;
public ListItemClickListener(int position, PhoneBook list){
this.position = position;
this.list = list;
}
@Override
public void onClick(View v) {
PhoneBookHandler db = new PhoneBookHandler(activity);
db.deleteContact(list);
contactList.remove(position);
notifyDataSetChanged();
}
}