java - Using a method to convert Integer to String and print in TextView -
i trying calculate number of pages seek progress bar , having trouble printing string textview. appreciated. here code
update: gives me value cannot divide 0 when runs through update(); method in 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 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>
a string variable should contain strings. convert integer string in order string variable accept it.
str += "first page number: " + string.valueof(first_page_number_update); str += "last page number: " + string.valueof(last_page_number_update);
Comments
Post a Comment