2016-04-25 48 views
-3

シークの進行状況バーからページ数を計算しようとしていますが、文字列を特定のTextViewに印刷できません。どんな助けもありがとう。ここにコードがあります整数を文字列に変換してTextViewで印刷する方法を使用します。

更新:Update()を実行すると値がゼロで割ることはできません。 calculate_pagesにおける方法

new_book.java

package com.example.barbersp.dailyreader; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.SeekBar; 
import android.widget.TextView; 
import android.widget.Toast; 

public class new_book extends Activity { 

public int first_page_number_update, number_of_days_to_read_update, last_page_number_update; 

private TextView book_title; 
private EditText edit_book_title; 
private TextView author_name; 
private EditText edit_Author_Name; 
private TextView first_page_number; 
private SeekBar edit_first_page_number; 
private TextView last_page_number; 
private SeekBar edit_last_page_number; 
private TextView number_of_days_to_read; 
private SeekBar edit_number_of_days_to_read_per_week; 
private TextView number_of_pages_per_day; 
private TextView number_of_pages_overall; 
private TextView expected_finish_date; 
private TextView expected_finish_date_final; 
private Button go_to_saved_books; 
private Button calculate_pages; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.new_book); 

    book_title = (TextView) findViewById(R.id.book_title); 
    edit_book_title = (EditText) findViewById(R.id.edit_book_title); 
    author_name = (TextView) findViewById(R.id.author_name); 
    edit_Author_Name = (EditText) findViewById(R.id.edit_Author_Name); 
    first_page_number = (TextView) findViewById(R.id.first_page_number); 
    edit_first_page_number = (SeekBar) findViewById(R.id.edit_first_page_number); 
    last_page_number = (TextView) findViewById(R.id.last_page_number); 
    edit_last_page_number = (SeekBar) findViewById(R.id.edit_last_page_number); 
    number_of_days_to_read = (TextView) findViewById(R.id.number_of_days_to_read); 
    edit_number_of_days_to_read_per_week = (SeekBar) findViewById(R.id.edit_number_of_days_to_read_per_week); 
    TextView number_of_pages_overall = (TextView) findViewById(R.id.number_of_pages_overall); 
    expected_finish_date = (TextView) findViewById(R.id.expected_finish_date); 
    expected_finish_date_final = (EditText) findViewById(R.id.expected_finish_date_final); 
    go_to_saved_books = (Button) findViewById(R.id.go_to_saved_books); 
    final Button calculate_pages = (Button) findViewById(R.id.calculate_pages); 


    Log.i(getClass().getSimpleName(), "onCreate"); 

    edit_first_page_number.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
     @Override 
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
      first_page_number_update = progress; 

      String str = new String(); 
      str += "First Page Number: " + first_page_number_update; 
      first_page_number.setText(str.toString()); 
     } 

     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) { 

     } 

     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) { 

     } 
    }); 


    edit_last_page_number.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
     @Override 
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
      last_page_number_update = progress; 

      String str = new String(); 
      str += "Last Page Number: " + last_page_number_update; 
      last_page_number.setText(str.toString()); 
     } 

     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) { 

     } 

     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) { 

     } 
    }); 

    edit_number_of_days_to_read_per_week.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
     @Override 
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
      number_of_days_to_read_update = progress; 

      edit_number_of_days_to_read_per_week.getProgress(); 

      String str = new String(); 
      str += "Number of days to read per week: " + number_of_days_to_read_update; 
      number_of_days_to_read.setText(str.toString()); 
     } 

     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) { 

     } 

     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) { 

     } 
    }); 


    calculate_pages.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      calculatePages(); 
     } 
    }); 

} 

public String calculatePages() 
{ 
    int number_of_pages_overall_update = last_page_number_update-first_page_number_update/number_of_days_to_read_update; 
    String number_of_pages_overall = Integer.toString(number_of_pages_overall_update); 
    number_of_pages_overall.setText(""); 
    return 
} 

public void sendMessage(View view) 
{ 
    Intent intent = new Intent(new_book.this, book_collection.class); 
    startActivity(intent); 
} 

protected void onDestroy() { 
    super.onDestroy(); 
    Log.i(getClass().getSimpleName(), "onDestroy"); 
} 

protected void onPause() { 
    super.onPause(); 
    Log.i(getClass().getSimpleName(), "onPause"); 
} 

protected void onRestart() { 
    super.onRestart(); 
    Log.i(getClass().getSimpleName(), "onRestart"); 
} 

protected void onResume() { 
    super.onResume(); 
    Log.i(getClass().getSimpleName(), "onResume"); 
} 

protected void onStart(){ 
    super.onStart(); 
    Log.i(getClass().getSimpleName(), "onStart"); 
} 

protected void onStop(){ 
    super.onStop(); 
    Log.i(getClass().getSimpleName(), "onStop"); 
    } 
} 

new_book.xml

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".new_book" 
android:weightSum="1"> 

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:text="@string/book_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="1" 
     android:id="@+id/book_title"/> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/edit_book_title" 
     android:layout_column="3" /> 

</TableRow> 

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:text="@string/author_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="1" 
     android:id="@+id/author_name"/> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/edit_Author_Name" 
     android:layout_column="3" /> 
</TableRow> 

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:text="@string/first_page_number" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="1" 
     android:id="@+id/first_page_number"/> 

    <SeekBar 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/edit_first_page_number" 
     android:layout_column="3" 
     android:max="800"/> 

</TableRow> 

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:text="@string/last_page_number" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="1" 
     android:id="@+id/last_page_number"/> 

    <SeekBar 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/edit_last_page_number" 
     android:layout_column="3" 
     android:max="800"/> 

</TableRow> 

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:text="@string/number_of_days_to_read_per_week" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="1" 
     android:id="@+id/number_of_days_to_read"/> 

    <SeekBar 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/edit_number_of_days_to_read_per_week" 
     android:layout_column="3" 
     android:max="7"/> 

</TableRow> 

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/Calculate_pages" 
     android:id="@+id/calculate_pages" 
     android:layout_column="1" 
     android:onClick="calcMessage"/> 

</TableRow> 

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:text="@string/number_of_pages_to_read_per_day" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="1" 
     android:id="@+id/number_of_pages_per_day"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/number_of_pages_overall" 
     android:layout_column="3" /> 

</TableRow> 

<TableRow 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TextView 
     android:text="@string/expected_finish_date" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_column="1" 
     android:id="@+id/expected_finish_date"/> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="date" 
     android:ems="10" 
     android:id="@+id/expected_finish_date_final" 
     android:layout_column="3" /> 

</TableRow> 

<TableRow 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Book Collection" 
     android:id="@+id/go_to_saved_books" 
     android:layout_column="1" 
     android:onClick="sendMessage"/> 
</TableRow> 

</TableLayout> 
+0

文字列yourString = String.valueOf(yourInteger); :) – Razgriz

+0

こんなに大したコードですか? –

+0

'String yourString =" "+ yourInteger;' –

答えて

0

文字列変数は文字列だけを含める必要があります。文字列変数を受け入れるために整数をStringに変換します。

str += "First Page Number: " + String.valueOf(first_page_number_update); 
str += "Last Page Number: " + String.valueOf(last_page_number_update); 
関連する問題