Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何获取listView元素的计数_Android_Listview - Fatal编程技术网

Android 如何获取listView元素的计数

Android 如何获取listView元素的计数,android,listview,Android,Listview,我目前正在为我的移动应用程序开发类编写一个tip tracker应用程序,它将接受来自EditText字段的用户输入,并将信息保存到数据库中。每次单击“保存”按钮时,整个版面中的所有条目都会添加到一个以不同版面显示的listView中。我让数据库工作,在那里我可以添加和删除元素,但我想知道是否有办法获得当前显示的列表视图的数量?我知道有很多方法可以计算数据库中的行数,但是有可能得到列表视图的数量吗 这就是我的布局当前的外观。每个listView都是我拍摄的一张比萨饼,所有信息都显示在每个list

我目前正在为我的移动应用程序开发类编写一个tip tracker应用程序,它将接受来自EditText字段的用户输入,并将信息保存到数据库中。每次单击“保存”按钮时,整个版面中的所有条目都会添加到一个以不同版面显示的listView中。我让数据库工作,在那里我可以添加和删除元素,但我想知道是否有办法获得当前显示的列表视图的数量?我知道有很多方法可以计算数据库中的行数,但是有可能得到列表视图的数量吗

这就是我的布局当前的外观。每个listView都是我拍摄的一张比萨饼,所有信息都显示在每个listView中。我想知道是否有可能获取页面上的列表视图数量,这样我就可以在“DEL”按钮下面创建一个文本视图,上面写着“Delivery#”,并拥有当前的条目数量

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

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()”获取总数

但是,您是否想要屏幕上显示的视图数或总视图数还不是很清楚。如果要查找显示的视图数,请忽略我的回答

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

public View getView(int position, View convertView, ViewGroup parent)
你要找的号码是“职位”。我假设您已经知道如何根据应用程序屏幕截图制作自定义适配器。如果您需要创建自定义适配器的帮助,我建议您搜索(并询问)另一个问题。

listView.getAdapter().getCount()应提供listView中的行数。您可以使用此选项将TextView设置为:

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

对于您想要实现的目标,不要使用getCount()。这是因为,get count将提供列表中的行总数。因此,所有行都将具有相同的值。请在适配器的getView()方法中使用以下代码

//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));

就是这样

如果您使用的是listView,那么您也有适配器,因此您可以使用“getCount()”获取总数

如果你想为布局建议,我附上了一张图片,请检查它

保留标签名称和选项卡名称,您可以像“删除”按钮一样自定义为“交付”,并删除“编辑”按钮


为此,我使用了RecyclerListView。

我想我不太确定如何回答这个问题。我知道,对于每个listView,我需要到目前为止运行的listView总数。所以第一个列表视图会显示Delivery 1,第二个会显示Delivery 2,依此类推。我想这就是我需要的。我可以把这个放在我的代码中的什么地方?我尝试将其放入onClick方法中,在本例中,当我单击“保存”按钮将其保存到数据库中时,它抛出错误:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.widget.ListAdapter android.widget.ListView.getAdapter()”。我所做的是….deliveryNumber=(TextView)findViewById(R.id.deliveryNumber);和deliveryNumber.setText(“Delivery#”+listView.getAdapter().getCount());但即使这样也会抛出一个错误,说我无法连接它们。是每一行都有自己的“Delivery#”文本视图,还是整个列表只有一个文本视图?如果只有一个,那么您尝试的第二个方法必须工作,并且连接不应该是错误的,它必须是警告。如果您希望每行都显示文本视图,请发布自定义适配器代码,我将帮助您完成。我已经发布了适配器活动。我想每行都需要一个字段,因为每个listview中的所有字段都在数据库中的同一行上。在我发布的数据库外观截图中,这是3次交付。我希望在“DEL”按钮下面有文本视图字段,显示送货编号。因此,第一个列表视图中的传递值为1,第二个列表视图中的传递值为2,依此类推……但如果我从数据库中删除它,数字当然会下降。这就是我需要的!非常感谢你的帮助!愿源头与你同在!