开发者

Android use NumberFormat for an item of a ListView

开发者 https://www.devze.com 2023-04-05 07:40 出处:网络
I have a String in an Editext that I convert to a float like this : float montantFac = Float.valueOf(etMontantFac.getText().toString());

I have a String in an Editext that I convert to a float like this :

float montantFac = Float.valueOf(etMontantFac.getText().toString());

I save it in a database and later, I get it back from the DB and I assign it again to this EditText.

etMontantFac.setText(facture.getString(facture.getColumnIndexOrThrow("montant_fa"))); 

My problem is that it displays some numbers with exponent. For example, 1000000 is displayed like this : 1e+06

How can I do to ever display the values with numers only (no letter or + or anything else) ?

EDIT :

@Pratik : Thanks, that works fine!

I also need to do the same for an item of a ListView but I don't find how to use your solution in this case. Can you help me ?

Here is my ListView :

    private void fillData(){
    Cursor cuFactures = db.recupListeFacture(triFactures, argumentWhereVhc, choixVhc, argumentWhereGar, choixGar);
    startManagingCursor(cuFactures);

    ListAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.facture_ligne,
            cuFactures,
   开发者_StackOverflow社区         new String[] {"libelle_fa", "nom_vhc", "montant_fa", "nom_garage", "date_fa"},
            new int[] {R.id.listeNomFac, R.id.listeNomVhcFac, R.id.listeMontantFac, R.id.listeNomGarFac, R.id.listeDateFac});

    setListAdapter(adapter);

The concerned field is always "montant_fa"

Thank you in advance!


check this link for format

http://download.oracle.com/javase/tutorial/i18n/format/decimalFormat.html

try this way

float val = facture.getFloat(facture.getColumnIndexOrThrow("montant_fa"));

NumberFormat nf = NumberFormat.getNumberInstance(loc);
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern(pattern);
String output = df.format(value);
etMontantFac.setText(output);


I know this is old, but here is for the format data between sqlite and listview in case someone trying to do the same thing. I had similar problem and solved it with that.

here is my example

adapter = new SimpleCursorAdapter(....);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
               if (columnIndex == cursor.getColumnIndex("columnname")) {
                   TextView tv = (TextView) view;
                   NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
                   tv.setText(nf.format(cursor.getInt(columnIndex)));
                   // must return true to be applied
                   return true;
               }
               return false;
        }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消