1

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.

enter image description here

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();
    }
}
Anthony Boley
  • 107
  • 12

3 Answers3

2

If you are using a listView, i can assume you're using an adapter also, therefore you can get the total number with "getCount()".

However it is not really clear if you want the number of displayed views on screen or the total number of view. Ignore my answer if you're looking for the number of view displayed.

Edit : For the current number of a view in a listview (not the listview count as in question title) you need to update your adapter. You must create a custom adapter, and override

public View getView(int position, View convertView, ViewGroup parent)

The number you are looking for is "position". I assume you already know how to make a custom adapter given the application screenshoots. I'd suggest to search (and ask) another question if you need help creating a custom adapter.

Feuby
  • 708
  • 4
  • 7
  • I guess I'm not really sure how to answer that. I know that for each listView, I want the running total of listViews thus far. So the first listView would say Delivery 1, the second would say Delivery 2, and so on. – Anthony Boley Apr 16 '17 at 07:43
1

listView.getAdapter().getCount() should give you the number of rows in the listview. You can use this to set your TextView as:

myTextView.setText("Delivery #"+listView.getAdapter().getCount());

UPDATE:

For what you want to achieve, do not use getCount(). This is because, get count will give you the total number of rows in the list. Hence all your rows will have the same value. Please use the following code in getView() method of your adapter.

//First add a text field to your contact_entry layout
//lets say the text field id is deliveryNum
TextView deliveryNumber = (TextView) convertView.findViewById(R.id.deliveryNum);

//Now set text as position variable + 1 because position start from 0
deliveryNumber.setText("Delivery #"+(position+1));

That's it

Nirup Iyer
  • 1,215
  • 2
  • 14
  • 20
  • I think this is what I need. Where can I put this in my code? I've tried putting it in my onClick method, which, in this case, is when I click the "Save" button to save it to the database, but it throws the error: java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.ListAdapter android.widget.ListView.getAdapter()' on a null object reference. What I did was....deliveryNumber = (TextView) findViewById(R.id.deliveryNumber); and deliveryNumber.setText("Delivery #" + listView.getAdapter().getCount()); But even that throws an error that says I can't concatenate them. – Anthony Boley Apr 19 '17 at 08:31
  • does each row have its own "Delivery #" textview or only one textview for whole list? If only one, the second method you tried must work, and concatenation shouldn't be an error, it must be a warning. If you want that textview for every row, please post the custom adapter code, ill help you out.. – Nirup Iyer Apr 19 '17 at 13:58
  • I have posted the adapter activity. I guess I would need one for each row, since all of the fields in each listview are on the same row in my database. In the screenshot I posted of how my database looks, those are 3 deliveries. And I would like to have textView fields underneath the "DEL" button that says what the delivery number is. So Delivery #1 on the first listView, Delivery #2 on the second and so on...But if I delete it from the database, the number goes down of course. – Anthony Boley Apr 19 '17 at 14:41
  • That's what I needed! Thank you very much for the help! – Anthony Boley Apr 19 '17 at 15:02
  • May the source be with you! – Nirup Iyer Apr 19 '17 at 15:03
0

If you are using a listView,you have also adapter, therefore you can get the total number with "getCount()".

If you want to suggestion for layout I have attached one Image please check it.enter image description here

leave label name and Tab Name you can Customize like Delete button as Delivery and remove Edit button

for this I used RecyclerListView.

Dilip
  • 2,622
  • 1
  • 20
  • 27