如何在listview中保存在sqlite数据库中的所有项目前面添加货币符号?

时间:2017-09-16 14:53:50

标签: android sqlite listview android-sqlite

我有一个sqlite数据库,其中包含值,我想将磅符号放在所有listview项的前面。我不想将磅符号与值放在一起,因为它存储为整数,因此可以将其添加到应用程序的其他位置。我该怎么做?

例如我目前有:

  • 10
  • 20
  • 30

我希望:

  • £10
  • £20
  • £30

以下是我在listview中的活动:

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.database.Cursor;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.ListView;
import android.support.v4.app.Fragment;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;

public class tab2income extends Fragment {

    private static final String TAG = "tab2income";
    DatabaseHelper mDatabaseHelper;
    private ListView mListView;
    View rootView;
    Cursor incomedata;
    SimpleCursorAdapter sca;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.tab2income, container, false);
        return rootView;

    }

    @Override
    public void onActivityCreated( Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mListView = (ListView) rootView.findViewById(R.id.listViewincome);
        mListView.setEmptyView(rootView.findViewById(R.id.empty));
        mDatabaseHelper = new DatabaseHelper(getActivity());


        populateListView();
    }

    private void populateListView() {
        Log.d(TAG, "populateListView: Displaying data in the ListView.");
        incomedata = mDatabaseHelper.getincomeData();
        sca = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_1, incomedata, new String[]{DatabaseHelper.INCOME_AMOUNT}, new int[]{android.R.id.text1}, 2);
        mListView.setAdapter(sca);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int csrpos = incomedata.getPosition();
                incomedata.moveToPosition(i);
                displayNoteDate(
                        incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_NOTES)),
                        incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_DATE)),
                        l);
                incomedata.moveToPosition(csrpos);
            }
        });
    }




    @Override
    public void onDestroy() {
        super.onDestroy();
        incomedata.close();
    }

    public void displayNoteDate(String noteContent, String dateValue, final long noteID) {
        MaterialDialog.Builder builder= new MaterialDialog.Builder(getActivity())
                .title("Income Information")
                .content("Note: "+noteContent+"\nDate: "+ dateValue)
                .positiveText("edit")
                .negativeText("delete")
                .neutralText("close")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        Toast.makeText(getActivity(),"EDIT",Toast.LENGTH_LONG).show();
                    }
                })
                .onNeutral(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        mDatabaseHelper.deleteincomeData(Long.toString(noteID));
                        incomedata = mDatabaseHelper.getincomeData();
                        sca.swapCursor(incomedata);
                    }
                });
        builder.show();
    }

}

2 个答案:

答案 0 :(得分:2)

你必须按照这种方式..它会帮助你..

将它放在strings.xml中

public String[] getValuesList(Cursor c) {
       String[] valuesList = new String[c.getCount()];
        int i=0;
        if (c.getCount() > 0) {
            if (c.moveToFirst()) {
                do {
                    //Here let us assume 2  is Your INCOME_AMOUNT column index
                    String value = R.string.currency_symbol+c.getString(2);
                    valuesList[i] = value;
                    i++;
                } while (c.moveToNext());
            }
            c.close();
        }
        return valuesList;
    }

然后在您的值上添加此字符串us前缀.. 例如

private void populateListView() {
        Log.d(TAG, "populateListView: Displaying data in the ListView.");
        incomedata = mDatabaseHelper.getincomeData();
        String amountListValues[] = getValuesList(incomedata);
        sca = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,  amountListValues);
        mListView.setAdapter(sca);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {               
                incomedata.moveToPosition(i);
                displayNoteDate(
                        incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_NOTES)),
                        incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_DATE)),
                        l);
                incomedata.moveToPosition(i);
            }
        });
    }

你必须替换populateListView()方法 像

glDrawArrays

它会以英镑符号显示......

如果您需要其他数学符号/ unicode字符,请参阅此链接http://xahlee.info/comp/unicode_common_symbols.html

答案 1 :(得分:0)

您可以更改方法getincomeData,以便查询使用以下内容: -

SELECT '$' || incomedata AS incomedata FROM incometable;

incomedata 是表格中的一列但是结果光标中的 incomedata 列是前缀为 $ 的相应值。

这是一个真实的例子(显示前后值): -

enter image description here

这是基于 productusageccost 列。使用的SQL是: -

SELECT productusagecost AS originaldata, '$' || productusagecost AS productusagecost FROM productusage

这需要 productusagecost 列,将其输出为 originaldata 列以及 productusagecost ,但 productusagecost 的位置以 $ 为前缀的列。您实际上不需要productusagecost AS originaldata,,因为这仅用于说明。

然后,您将为希望以符号作为前缀的每列执行此操作。注意到如果您使用数据进行计算,那么您必须删除前缀或使用第二列(因此该示例包含原始数据列的原因)。

您可以在设置相应视图时使用CustomAdapter为值添加前缀。例如在CustomAdpater的bindView方法(getView也可以使用)中,我有: -

productcost.setText(NumberFormat.getCurrencyInstance().format(csr.getDouble(productusage_cost_offest)));

这使用设备的货币格式。 (如果使用此功能,您应该研究使用 getCurrentcyInstance 的专业和缺点。)