首页 \ 问答 \ Android从数据库中填充ListView,格式为textview,带有两位小数(Android Populate ListView from database, format textview with two decimals)

Android从数据库中填充ListView,格式为textview,带有两位小数(Android Populate ListView from database, format textview with two decimals)

我有这个方法从数据库填充ListView。 一切都很好。 但是我希望其中一个文本字段显示带有两位小数的值。

之前已经问过这个问题并且许多标记为重复,但问题是我如何实现两位小数以及我在下面的代码中的位置

正如你所看到的,我尝试了许多不同的事情而没有成功。 也许我需要在我设置TextViews的地方构建自定义适配器?

请不要将此标记为副本,因为此处未给出答案等:

将Double值格式化为2个小数位的最佳方法

或者在这里:

将小数加倍到2位小数

或者在这里:

如何在Java中将数字舍入到n个小数位

这是我的代码和我的问题是我在哪里可以实现我的代码舍入到两位小数?

private void populateListViewFromDB(int month, int year) {


    Cursor cursor = dbHandler.getMonth(month,year);

    // TextView setEx = (TextView)findViewById(R.id.oneRowExkl);
    // set = Double.parseDouble(setEx.getText().toString());
    // setEx.setText(String.format("%.2f", set));
    //TextView ex = (TextView)findViewById(R.id.oneRowExkl);
    //ex.setText(String.format("%.2f", Double.parseDouble(MyDBHandler.INKL)));


    String[] fromFieldNames = new String[] {
            MyDBHandler.COLUMN_PRODUCTNAME, MyDBHandler.DATE,
            MyDBHandler.INKL };
    int[] toViewIDs = new int[] { R.id.oneRowName, R.id.oneRowDate,
            R.id.oneRowExkl };

    // Create adapter to may columns of the DB onto element in the UI.
    @SuppressWarnings("deprecation")
    SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this, //
    Context  
            R.layout.one_row, // Row layout template
            cursor, // cursor (set of DB records to map)
            fromFieldNames, // DB Column names
            toViewIDs // View IDs to put information in
    );

    ListView myList = (ListView) findViewById(R.id.list1);
    // myList.setBackgroundColor(Color.GRAY);
    myList.setAdapter(myCursorAdapter);
}

I have this method that populates ListView from Database. Everything works good. But I want to have one of the textfields showing the value with two decimals.

This has been asked before and many marked is as duplicate but the question is how can I implement the two decimals and where do I do it in the code below

As you can see, I tried many different things without success. Maybe I need to build a custom adapter where I set my TextViews?

And please don't mark this as a duplicate because the answer is not given for etc here:

Best way to Format a Double value to 2 Decimal places

or here:

Round a double to 2 decimal places

or here:

How to round a number to n decimal places in Java

Here comes my code and my question is where can I Implement my code for rounding up to two decimals?

private void populateListViewFromDB(int month, int year) {


    Cursor cursor = dbHandler.getMonth(month,year);

    // TextView setEx = (TextView)findViewById(R.id.oneRowExkl);
    // set = Double.parseDouble(setEx.getText().toString());
    // setEx.setText(String.format("%.2f", set));
    //TextView ex = (TextView)findViewById(R.id.oneRowExkl);
    //ex.setText(String.format("%.2f", Double.parseDouble(MyDBHandler.INKL)));


    String[] fromFieldNames = new String[] {
            MyDBHandler.COLUMN_PRODUCTNAME, MyDBHandler.DATE,
            MyDBHandler.INKL };
    int[] toViewIDs = new int[] { R.id.oneRowName, R.id.oneRowDate,
            R.id.oneRowExkl };

    // Create adapter to may columns of the DB onto element in the UI.
    @SuppressWarnings("deprecation")
    SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this, //
    Context  
            R.layout.one_row, // Row layout template
            cursor, // cursor (set of DB records to map)
            fromFieldNames, // DB Column names
            toViewIDs // View IDs to put information in
    );

    ListView myList = (ListView) findViewById(R.id.list1);
    // myList.setBackgroundColor(Color.GRAY);
    myList.setAdapter(myCursorAdapter);
}

原文:https://stackoverflow.com/questions/34197229
更新时间:2023-01-21 14:01

最满意答案

在客户端,您不需要证书私钥来信任服务器。 由于您在问题中写道,您在keystore.ImportKey导入了证书和密钥,我认为它们已作为PrivateKeyEntry导入(您可以使用keytool验证密钥库中条目的类型)。

但是,如果要将证书用作信任锚,则应将证书作为TrustedCertificateEntry导入。 它可以通过keytool实现:

keytool -importcert -trustcacerts -alias myTrustAnchor -file /path/to/cert.crt -keystore /path/to/keystore

然后,您可以在应用程序中配置信任库:

System.setProperty("javax.net.ssl.trustStore","/path/to/keystore");

Thanks @Jcs

This is how I solved the problem. When I tried opening the webservice URL in a browser, it asked for client certificate. This means, because I had already imported server certificate in jssecacert in jvm, my client was missing the client certificate. So, instead of setting javax.net.ssl.trustStore and javax.net.ssl.trustStorePassword properties I set javax.net.ssl.keyStore and javax.net.ssl.keyStorePassword properties and it is working fine. I missed before the fact that the private key and certificate are imported into the keystore. ImportKey are basically client identity which I received long back from someone saying those are server certificates. That was misleading me. So, let me summarize the solution if someone is looking for it.

  1. Download server certificate and import into JVM cacerts or jssecacerts on system path. I used this post.

  2. Open webservice URL in a browser and if it asks for client certificate it means server is set to expect certificate from client. In case of self signed certificate you must already have self signed certificate from server. Import these in a keystore and set the system properties for key store and not the trust store before actually making call to web service as shown below. This is because you already have imported server certificate into client trust store (cacerts).

Code:

MySoap12Stub stub = (MySoap12Stub) new MyLocator().getMySoap12(new java.net.URL(WSUrl));

System.setProperty("javax.net.ssl.keyStore", "certs/keystoreQA.Importkey");
System.setProperty("javax.net.ssl.keyStorePassword", "importkey");

In addition in my case, server is expecting user token and password set into SOAP headers. This is how I set this into SOAP headers:

((Stub) stub).setHeader(HeaderHandler.getSecurityHeader(User, password));

public class HeaderHandler {

    public static SOAPHeaderElement getSecurityHeader(String user,String password) throws Exception {
        SOAPHeaderElement wsseSecurity = new SOAPHeaderElement(new PrefixedQName(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
            "Security", "wsse"));
        wsseSecurity.setActor(null);
        wsseSecurity.setMustUnderstand(true);

        SOAPElement usernameToken = wsseSecurity.addChildElement("UsernameToken", "wsse");
        usernameToken.setAttribute("xmlns:wsu","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
        SOAPElement username = usernameToken.addChildElement("Username", "wsse");
        username.addTextNode(user);

        SOAPElement password = usernameToken.addChildElement("Password", "wsse");
        password.setAttribute("Type","http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
        password.addTextNode(password);
        return wsseSecurity;
    }
} 

I hope this explains in details how to use self signed certificates and WSSE user token and password in axis2 client calling web services over https using usertoken and password.

Cheers! good to go now.

相关问答

更多
  • 我找到了一个解决方案,并将与任何其他与axis2合作的不幸人士分享。 ConfigurationContext ctx = ConfigurationContextFactory.createConfigurationContextFromFileSystem("repository", null); NetWS_0Stub stub = new NetWS_0Stub(ctx, aEndPoint); ServiceClient client = stub._getServiceClient(); O ...
  • Axis 2是该框架的重大改写。 升级没有真正的捷径,你需要处理它或保留在Axis 1上(正如有些人真正决定做的那样)。 官方迁移指南提供了一些有用的提示,但它没有详细说明为了成功完成迁移您需要了解的所有内容。 由于你将经历升级的痛苦,如果我不建议你看看Apache CXF ,我会失职。 它是一个更现代的Web服务框架,支持JAX-WS 和 JAX-RS。 如果你签订了第一个开发合同,那么你的客户就不必升级了(只是服务器实现方)。 Axis 2 was a significant rewrite of th ...
  • 该错误是由WSDL文件的位置路径中的空格引起的:file:/ C:/ Pluton Server / installers / Webservice _engine / axis2-1.5.1 / bin / EncryptService.wsdl 尝试将WSDL文件移动到没有空格的路径(例如c:/test/EncryptService.wsdl)。 The error is caused by spaces in the path of the WSDL file's location: file:/C: ...
  • 我没有在用户名令牌中使用rampart,但对于我的服务(签名和加密),IBM文档确实非常有用。 请查看: http : //www.ibm.com/developerworks/java/library/j-jws4/ Java Web服务:Axis2 WS-Security基础知识; 了解如何将Rampart安装到Axis2中并实现UsernameToken处理 对于我的项目,当我使用WS-Security(工作环境)和没有WS-Security(测试环境)时,Java源代码是相同的。 我需要在客户端执行 ...
  • 我在http://www.ibm.com/developerworks/java/library/j-jws4/上找到了一个有用的例子。 我摆脱了axis2.xml中的OutflowSecurity参数。 我也摆脱了密码回调类。 我添加了一个policy.xml文件和一些代码来加载它。 我使用此代码提供用户名和密码: // Prepare the client PartnerAPIStub stub = stubProvider.getStub(); ServiceClient client = stub. ...
  • 您正在尝试实例化抽象类或接口。 这就是抛出InstantiationError的原因。 根据文档,它陈述如下 公共类InstantiationError扩展了IncompatibleClassChangeError 当应用程序尝试使用Java新构造来实例化抽象类或接口时抛出。通常,编译器会捕获此错误; 如果类的定义不兼容地更改,则此错误只能在运行时发生。 you are trying to instantiate an abstract class or interface. that's why Inst ...
  • 首先,URL端点应该是httpS而不是http ,并使您的信任库/密钥库可供JVM使用。 请参阅: http : //blog.technogemsinc.com/2008/02/https-connection-in-java.html 大约一年前我也使用过https://code.google.com/p/misc-utils/wiki/JavaHttpsUrl .. 从以下两个选项中使用对您更方便的内容 使信任库可用于jvm -Djavax.net.ssl.trustStore = “<%PATH_T ...
  • 参考: http : //axis.apache.org/axis2/java/core/docs/http-transport.html 对于每个协议(HTTP和/或HTTPS),必须在axis2.xml中声明AxisServletListener实例。 如果仅使用单个协议,则不需要进一步配置。 例如,如果仅使用HTTP,则axis2.xml中必须存在以下声明:
    在客户端,您不需要证书私钥来信任服务器。 由于您在问题中写道,您在keystore.ImportKey导入了证书和密钥,我认为它们已作为PrivateKeyEntry导入(您可以使用keytool验证密钥库中条目的类型)。 但是,如果要将证书用作信任锚,则应将证书作为TrustedCertificateEntry导入。 它可以通过keytool实现: keytool -importcert -trustcacerts -alias myTrustAnchor -file /path/to/cert.crt ...
  • 当您在应用程序层中构建身份验证(用户名/密码)时,您的问题的答案是肯定的,肥皂异常是一种很好的做法。 当用户(客户端)尝试访问您的服务时,它可能使用库/应用程序与您的服务进行通信,并且捕获soap异常应该不是问题。 我不确定你想要通过额外的尝试完成什么,但它们应该是新的请求,而不是在同一个请求中。 When you build authentication (username/password) within the application layer, the answer to your questio ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)