在android recyclerview textview中将textview更改为2位小数

时间:2017-12-10 12:44:48

标签: android sqlite android-recyclerview

我在recyclerview中有一个textview,显示保存在sqlite数据库中的十进制货币值。但当我在数据库中输入10.00美元时,recyclerview textview显示为10美元而不是10.00美元。我该如何解决这个问题?

这是我对recyclerview的布局:

    

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/pound"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:paddingStart="8dp"
            android:text="£"
            android:textSize="20sp"
            android:textColor="@color/fab2_color"/>

        <TextView
            android:id="@+id/favourite_textView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_weight="1"
            android:maxLength="9"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:textSize="20sp"
            android:textColor="@color/fab2_color"
            />
    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_marginEnd="5dp"
        android:layout_marginRight="5dp"
        android:color="#ffffff" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingTop="30dp">

                <TextView
                    android:id="@+id/category_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:maxLines="1"
                    android:paddingBottom="5dp"
                    android:paddingTop="5dp"
                    android:paddingStart="8dp"/>

                <TextView
                    android:id="@+id/verse_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_weight="1"
                    android:gravity="end"
                    android:maxLines="1"
                    android:paddingBottom="5dp"
                    android:paddingTop="5dp" />
            </LinearLayout>

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:layout_marginEnd="5dp"
                android:layout_marginRight="5dp"
                android:color="#ffffff" />
</android.support.v7.widget.CardView>

这是我将值插入数据库的地方:

    public void AddData(){
        FloatingActionButton fab2 = (FloatingActionButton) findViewById(R.id.fab_tick_income);
        fab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                formatter = new DecimalFormat("0.00");
                RawincomeValue = editAmountincome.getRawValue();
                FormatedincomeVal = (double) RawincomeValue/100;

                if (FormatedincomeVal > 0){
                    isAmountInserted = true;
                }
                else{
                    isAmountInserted = false;
                }

                noteCheck = editNotesincome.getText().toString().trim();

                if(noteCheck.isEmpty())
                {
                    noteCheck = noteCheck.replace("", "No Note Inserted");
                }
                else
                {
                    //EditText is not empty
                }

                if(isCategoryInserted && isDateInserted && isAmountInserted && isAccountInserted) {
                    myDbincome.insertincomeData(
                            FormatedincomeVal,
                            editDateincome.getText().toString(),
                            noteCheck,
                            category,
                            account);

                    Toast.makeText(add_income.this,"Income Registered",Toast.LENGTH_LONG).show();
                    Intent intent=new Intent(add_income.this, MainActivity.class);
                    startActivity(intent);
                }
根据请求

适配器视图:

    public class IncomeAdapter extends RecyclerView.Adapter<IncomeAdapter.ViewHolder> {
        private final ArrayList<String> dataSet;
        private final ArrayList<String> dataSet2;
        private final ArrayList<String> dataSet3;
        private ItemClickListener clickListener;
        DecimalFormat formatter = new DecimalFormat("0.00");
        String myListPreference;
        Context ctx;

        class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            final TextView pound;
            final TextView mTextView;
            final TextView textView;
            final TextView categoryText;
            final CardView cardView;

            ViewHolder(View v) {
                super(v);
                SharedPreferences sharedPreferences = android.support.v7.preference.PreferenceManager.getDefaultSharedPreferences(ctx);
                myListPreference = sharedPreferences.getString("CurrencyType", "£");

                pound = (TextView) v.findViewById(R.id.pound);
                mTextView = (TextView) v.findViewById(R.id.favourite_textView);
                categoryText = (TextView) v.findViewById(R.id.category_text);
                textView = (TextView) v.findViewById(R.id.verse_text);
                cardView = (CardView) v.findViewById(R.id.cardview);

                pound.setText(""+ myListPreference);
                v.setOnClickListener(this);
            }

            @Override
            public void onClick(View view) {
                if (clickListener != null) clickListener.onClick(view, getAdapterPosition());
            }

        }

        public IncomeAdapter(ArrayList<String> myDataset, ArrayList<String> myDataSet2, ArrayList<String> myDataset3, Context ctx) {
            this.dataSet = myDataset;
            this.dataSet2 = myDataSet2;
            this.dataSet3 = myDataset3;
            this.ctx = ctx;
        }

我如何将信息放入recyclerview:

    private void populateListView() {
        Log.d(TAG, "populateListView: Displaying data in the ListView.");
        ArrayList<String> arrayList = new ArrayList<>();
        ArrayList<String> arrayList2 = new ArrayList<>();
        ArrayList<String> arrayList3 = new ArrayList<>();

        IncomeAdapter incomeAdapter = new IncomeAdapter(arrayList,arrayList2,arrayList3, getContext());
        mListView.setAdapter(incomeAdapter);

        incomeAdapter.setClickListener(this);

        incomedata = mDatabaseHelper.getincomeData();
        if(incomedata.moveToFirst()){
            do {
                arrayList.add(incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_AMOUNT)));
                arrayList2.add(incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_DATE)));
                arrayList3.add(incomedata.getString(incomedata.getColumnIndex(DatabaseHelper.INCOME_CATEGORY)));
            }
            while (incomedata.moveToNext());
        }
        incomeAdapter.notifyDataSetChanged();
    }

1 个答案:

答案 0 :(得分:0)

textview.setText(String.format(&#34; Value  a:%。2f&#34;,a));