2017-12-10 5 views
0

Ich habe eine Textansicht in einer Recyclerview, die Dezimalgeldwerte in einer SQLite-Datenbank gespeichert zeigt. Aber wenn ich $ 10.00 in die Datenbank eintrage, zeigt die recyclerview Textansicht es als $ 10 anstelle von $ 10.00 an. Wie behebe ich das?Ändern der Textansicht auf 2 Dezimalstelle in Android Recyclerview Textansicht

Das ist mein Layout für die 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> 

Hier ist, wo ich die Werte in die Datenbank einfügen:

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); 
       } 

Adapter viewholder als pro Anfrage:

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; 
     } 

Wie kann ich die Informationen in die recyclerview setzen:

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(); 
    } 
+0

Können Sie den Code anzeigen, wo Sie die Textansicht einstellen? d. h. im Viewholder der recyclerview – riadrifai

+0

habe ich die Frage –

+0

keinen Code im Adapter, der mit db oder dieser Nummer verwandt ist, –

Antwort

0

textview.setText (String.format ("Value eines:% .2f", a));

+0

aktualisiert, was das a im Zeichenkettenformat bedeutet –