使用方法将Integer转换为String并在TextView中打印

时间:2016-04-25 00:54:05

标签: java android

我正在尝试计算搜索进度条中的页数,而我在将字符串打印到某个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>

1 个答案:

答案 0 :(得分:0)

字符串变量应该只包含字符串。将整数转换为String,以使String变量接受它。

str += "First Page Number: " + String.valueOf(first_page_number_update);
str += "Last Page Number: " + String.valueOf(last_page_number_update);