android - 如何获取 listView 元素的数量

标签 android listview

我目前正在为我的移动应用程序开发类(class)编写一个提示跟踪器应用程序,它将接受来自 EditText 字段的用户输入并将信息保存到数据库中。每次单击“保存”按钮时,整个布局中的所有条目都会添加到以不同布局显示的单个 listView 中。我有数据库在工作,我可以在其中添加和删除元素,但我想知道是否有办法获取当前显示的 listView 的数量?我知道有一些方法可以计算数据库中的行数,但是是否可以获取 listView 的数量?

这是我的布局目前的样子。每个 listView 都是我拿过的披萨外卖,所有信息都显示在每个列表中。我想知道是否可以获取页面上的 listView 数量,以便我可以在“DEL”按钮下方创建一个 TextView,上面写着“Delivery #”并具有当前的条目数量。

enter image description here

我将每一个添加到数据库的代码如下:

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();
}

如果有任何我遗漏的东西可以指引我从某人那里获得解决方案的正确方向,我会很乐意添加它。感谢您的宝贵时间。

编辑:

我的自定义适配器代码:

@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();
    }
}

最佳答案

如果您使用的是 listView,我可以假设您也在使用适配器,因此您可以使用“getCount()”获得总数。

然而,如果您想要屏幕上显示的 View 数或 View 总数,则不是很清楚。如果您正在查找显示的 View 数,请忽略我的回答。

编辑:对于 ListView 中 View 的当前数量(不是问题标题中的 ListView 计数),您需要更新适配器。您必须创建一个自定义适配器,并覆盖

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

您要查找的数字是“position”。我假设您已经知道如何根据应用程序屏幕截图制作自定义适配器。如果您需要帮助创建自定义适配器,我建议您搜索(并询问)另一个问题。

关于android - 如何获取 listView 元素的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43433474/

相关文章:

java - 如何滚动我的屏幕

安卓开发 : What type of tool is this?

android - 使用 fragment 从 Parse.com 获取数据

android - Android 上 ListView 中的 MediaPlayer

java - BodyEditorLoader - 读取文件时出错

android - ViewPager 一次可以显示多个 View 吗?

android - Android 的 Cordova 错误 : JDK/Android Target

android - 将两个 ListViews 合并为一个

android - 从其他 fragment 更新 Listview

java - 可以在listview的setListAdapter或SimpleAdapter中显示对象值的Arraylist吗?