1

I have fetching data from sqlite database. What i want is to display with bold the "Name" of each of my products.

Here is the code:

    fullProductDetails = "<b>Name: </b>"+data.getString(1) + "| Price: " + data.getString(2) + "| Quantity: " + data.getString(3);

The problem is that it's displaying the "b" character instead of bold in my listview.

Update

Here is a bit more of code:

Cursor data = mDatabaseHelper.getData();
        ArrayList<String> listData = new ArrayList<>();
        while (data.moveToNext()) {
            //get the value from the database in column 1
            //then add it to the ArrayList
            totalPrice=Float.parseFloat(data.getString(2))*Float.parseFloat(data.getString(3));
            fullProductDetails = "<b>Name: </b>"+data.getString(1) + "| Price: " + data.getString(2) + "| Quantity: " + data.getString(3);
            listData.add(fullProductDetails+" <b>Total Price: </b>"+totalPrice);
        }

//create the list adapter and set the adapter
        ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData);
        mListView.setAdapter(adapter);

Here is the screenshot. You can see two times "B" because i was testing something else. Ignore the second. enter image description here

Alex
  • 1,816
  • 5
  • 23
  • 39

2 Answers2

1

HTML formatted text won't work for native non-web TextView in Android. or any other not-web-based framework/system...

you should format this text with Spans, this is recommended way with plenty of possibilities

BUT for your simple case (handling only bold <b> tag) you may use Html.fromHtml method

fullProductDetails = ...
Spanned fullProductDetailsSpanned = Html.fromHtml(fullProductDetails);
textView.setText(fullProductDetailsSpanned);

read carefully THIS (use compat method posted in answer) and THIS (only few tags are supported by this method, rest would be stripped out, but bold is on the list)

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Thank you for the answer! I don't have a textView to display these! I only have the listView! – Alex Jun 02 '21 at 13:11
  • `ListView` contains some list items and in your case these are `TextView`s, supplied by adapter you have set. if you have simple instance, e.g. `ArrayAdapter` then you may migrate to `ArrayAdapter` (and `ArrayList listData` also), but the best option would be to write own adapter or at least override `getView()` method – snachmsm Jun 02 '21 at 13:12
1

Try this :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    textView.setText(Html.fromHtml("<b>Name: </b>", Html.FROM_HTML_MODE_COMPACT));
} else { 
    textView.setText(Html.fromHtml("<b>Name: </b>"));
}

or directly:

textView.setText(Html.fromHtml("<b>Name: </b>"));
Renis1235
  • 4,116
  • 3
  • 15
  • 27