点查找没有反应,点下一个直接就崩溃退出了,调动SeekBar也直接退出,目前就知道这些错误
package com.example.work_book;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity {
private TextView teturntime, tv_age, tv_bookname, tv_class, tv_applyage,
tv_tip;
private EditText name, et_time;
private RadioGroup sex;
private CheckBox ls, xy, wy;
private SeekBar sb_age;
private ImageView iv_pic;
private Button btn_find, btn_next;
private List<Book> lists_book;
private List<Book> lists_get;
private Person person;
private boolean isLs;
private boolean isXy;
private boolean isWy;
private int age = 18;
private int count = 0;
private int i=0;
private RadioGroupListener radioGroupListener;
private CheckBoxListener checkBoxListener;
private SeekBarListener seekBarListener;
private ButtonListener buttonListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_book);
initView();
initData();
setListener();
}
private void setListener() {
radioGroupListener = new RadioGroupListener();
checkBoxListener = new CheckBoxListener();
seekBarListener = new SeekBarListener();
buttonListener = new ButtonListener();
ls.setOnCheckedChangeListener(checkBoxListener);
xy.setOnCheckedChangeListener(checkBoxListener);
wy.setOnCheckedChangeListener(checkBoxListener);
sb_age.setOnSeekBarChangeListener(seekBarListener);
btn_find.setOnClickListener(buttonListener);
btn_next.setOnClickListener(buttonListener);
sex.setOnCheckedChangeListener(radioGroupListener);
}
private void initData() {
person = new Person();
lists_get = new ArrayList<Book>();
lists_book = new ArrayList<Book>();
lists_book.add(new Book("人生感悟", "心灵鸡汤", 15, false, false, true,
R.drawable.aa));
lists_book.add(new Book("边城", "长篇小说", 18, true, false, false,
R.drawable.bb));
lists_book.add(new Book("saplr", "专业书", 25, false, false, false,
R.drawable.cc));
lists_book.add(new Book("光辉岁月", "历史小说", 16, true, false, false,
R.drawable.dd));
lists_book.add(new Book("宋词三百首", "诗词集", 10, true, false, false,
R.drawable.ee));
lists_book.add(new Book("中国古代文学教学纲要", "教学纲要", 20, true, false, false,
R.drawable.ff));
lists_book.add(new Book("无花果", "诗文集", 15, false, false, true,
R.drawable.gg));
lists_book.add(new Book("古镇记忆", "回忆录", 20, true, false, true,
R.drawable.hh));
}
private void initView() {
teturntime = (TextView) findViewById(R.id.tv_returntime);
tv_age = (TextView) findViewById(R.id.tv_age);
tv_bookname = (TextView) findViewById(R.id.tv_bookname);
tv_class = (TextView) findViewById(R.id.tv_class);
tv_applyage = (TextView) findViewById(R.id.tv_applyage);
tv_tip = (TextView) findViewById(R.id.tv_tip);
name = (EditText) findViewById(R.id.et_name);
et_time = (EditText) findViewById(R.id.et_time);
sex = (RadioGroup) findViewById(R.id.rg_sex);
ls = (CheckBox) findViewById(R.id.cb_ls);
xy = (CheckBox) findViewById(R.id.cb_xy);
wy = (CheckBox) findViewById(R.id.cb_wy);
sb_age = (SeekBar) findViewById(R.id.sb_age);
iv_pic = (ImageView) findViewById(R.id.iv_pic);
btn_find = (Button) findViewById(R.id.btn_find);
btn_next = (Button) findViewById(R.id.btn_next);
}
class RadioGroupListener implements OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.rb_male:
person.setSex("男");
break;
case R.id.rb_female:
person.setSex("女");
break;
}
}
}
class CheckBoxListener implements
android.widget.CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
CheckBox cbBox = (CheckBox) buttonView;
switch (cbBox.getId()) {
case R.id.cb_ls:
isLs = isChecked;
break;
case R.id.cb_xy:
isXy = isChecked;
break;
case R.id.cb_wy:
isWy = isChecked;
break;
}
System.out.println("当前喜好:" + "历史:" + isLs + " 悬疑:" + isXy + "文艺:"
+ isWy);
}
}
class SeekBarListener implements OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
age = seekBar.getProgress();
person.setAge(age);
tv_age.setText(age);
tv_age.getText().toString();
}
}
class ButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_find:
String rate1=et_time.getText().toString();
String rate2=teturntime.getText().toString();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date1=formatter.parse(rate1);
Date date2=formatter.parse(rate2);
if(date1.getTime()>date2.getTime()){
Toast.makeText(MainActivity.this, "借书的时间晚于还书时间,退出", Toast.LENGTH_LONG).show();
finish();
}else{
String dString = formatter.format(date1);
person.setTime(dString);
lists_get.clear();
count = 0;
checkData();
tv_tip.setText("符合条件的书有" + (count + 1) + "本");
tv_tip.getText().toString();
Toast.makeText(MainActivity.this, person.toString(),
Toast.LENGTH_LONG).show();
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.btn_next:
if (i == count) {
Toast.makeText(MainActivity.this, "已经显示完全了",
Toast.LENGTH_LONG).show();
finish();
}
tv_tip.setText("符合条件的书有" + count + "本");
tv_bookname.setText(lists_get.get(i).getBookname());
tv_bookname.getText().toString();
tv_class.setText(lists_get.get(i).getBookclass());
tv_class.getText().toString();
tv_applyage.setText(lists_get.get(i).getApplyage());
tv_applyage.getText().toString();
iv_pic.setImageResource(lists_get.get(i).getPic());
i++;
person.setName(name.getText().toString());
Toast.makeText(MainActivity.this, person.toString(), Toast.LENGTH_LONG).show();
break;
}
}
}
private void checkData() {
for (int i = 0; i < lists_book.size(); i++) {
Book book = lists_book.get(i);
if (book.getApplyage() < age && book.isLs() == isLs
&& book.isXy() == isXy && book.isWy() == isWy) {
lists_get.add(book);
count++;
}
}
}
}public class Book {
private String bookname;
private String bookclass;
private int applyage;
private boolean ls;
private boolean xy;
private boolean wy;
private int pic;
public Book(String bookname, String bookclass, int applyage, boolean ls,
boolean xy, boolean wy, int pic) {
super();
this.bookname = bookname;
this.bookclass = bookclass;
this.applyage = applyage;
this.ls = ls;
this.xy = xy;
this.wy = wy;
this.pic = pic;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getBookclass() {
return bookclass;
}
public void setBookclass(String bookclass) {
this.bookclass = bookclass;
}
public int getApplyage() {
return applyage;
}
public void setApplyage(int applyage) {
this.applyage = applyage;
}
public boolean isLs() {
return ls;
}
public void setLs(boolean ls) {
this.ls = ls;
}
public boolean isXy() {
return xy;
}
public void setXy(boolean xy) {
this.xy = xy;
}
public boolean isWy() {
return wy;
}
public void setWy(boolean wy) {
this.wy = wy;
}
public int getPic() {
return pic;
}
public void setPic(int pic) {
this.pic = pic;
}
@Override
public String toString() {
return "Book [bookname=" + bookname + ", bookclass=" + bookclass
+ ", applyage=" + applyage + ", ls=" + ls + ", xy=" + xy
+ ", wy=" + wy + ", pic=" + pic + "]";
}
}public class Person {
private String name;
private String sex;
private int age;
private String time;
Book book;
public Person(){
}
public Person(String name, String sex, int age, String time, Book book) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.time=time;
this.book = book;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
@Override
public String toString() {
return "Person [name=" + name + ", sex=" + sex + ", age=" + age
+ ", time=" + time + "]";
}
}<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/lightyellow" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="借书" android:textSize="20dp" android:paddingLeft="20dp" android:textColor="@color/mediumseagreen"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_marginTop="10dp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="10sp" android:text="读者:"/> <EditText android:id="@+id/et_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:textSize="10sp" android:hint="请输入姓名"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="10sp" android:text="性别:"/> <RadioGroup android:id="@+id/rg_sex" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:orientation="horizontal"> <RadioButton android:id="@+id/rb_male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10sp" android:text="男"/> <RadioButton android:id="@+id/rb_female" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10sp" android:text="女"/> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical" android:layout_marginTop="5dp"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="借出时间:" android:textSize="10sp"/> <EditText android:id="@+id/et_time" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2.3" android:hint="请输入借出时间" android:textSize="10sp"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="归还时间:" android:textSize="10sp"/> <TextView android:id="@+id/tv_returntime" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="10sp" android:text="2016.06.30"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="爱好:" android:textSize="10sp"/> <CheckBox android:id="@+id/cb_ls" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="历史" android:textSize="10sp"/> <CheckBox android:id="@+id/cb_xy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="悬疑" android:textSize="10sp"/> <CheckBox android:id="@+id/cb_wy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文艺" android:textSize="10sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="年龄:" android:textSize="10sp"/> <TextView android:id="@+id/tv_age" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:gravity="center" android:textSize="10sp"/> <SeekBar android:id="@+id/sb_age" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" android:max="100" android:progress="18" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_marginLeft="10dp" android:orientation="horizontal"> <ImageView android:id="@+id/iv_pic" android:layout_width="0dp" android:layout_height="110dp" android:layout_weight="1" android:src="@drawable/aa"/> <LinearLayout android:layout_width="0dp" android:layout_height="110dp" android:layout_weight="1" android:layout_marginRight="10dp" android:background="@color/navajowhite" android:gravity="center" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="书名"/> <TextView android:id="@+id/tv_bookname" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="类型"/> <TextView android:id="@+id/tv_class" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="适用年龄"/> <TextView android:id="@+id/tv_applyage" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <Button android:id="@+id/btn_find" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查找"/> <TextView android:id="@+id/tv_tip" android:layout_width="50dp" android:layout_height="wrap_content" android:lines="2"/> <Button android:id="@+id/btn_next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一个"/> </LinearLayout> </LinearLayout>
0
收起
正在回答
2回答
程序还有如下错误:
1)时间格式使用的new SimpleDateFormat("yyyy-MM-dd"),所以布局文件中TextView显示要设置成android:text="2016-06-30",格式一致
2)当调用finish()时,只是将活动推向后台,并没有立即释放内存,活动的资源并没有被清理,还会继续执行,所以建议把finish()后面的语句单独放到else中
3)tv_applyage.setText(lists_get.get(i).getApplyage()+"");这里传入的int也要改成String
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7235 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程


恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星