Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#7 #534 第七次实验 #1555

Merged
merged 4 commits into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<dist:module dist:instant="true" />

<application
android:name=".MyApplication"
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.net.PortUnreachableException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
Expand All @@ -17,6 +18,8 @@
import edu.hzuapps.androidlabs.model.Task;

public class TaskDao {
public final static String LAST_TIME_PATTERN = "yyyy-MM-dd HH:mm";
public final static String CREATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
private String tableName;
private TaskDatabaseHelper taskDatabaseHelper;

Expand All @@ -32,33 +35,48 @@ public TaskDao(Context context){
* @param content : 内容
* @return 返回成功插入的数据数
*/
public long save(String title, String content){
public long save(String title, String content, String lastTime){
SQLiteDatabase database = taskDatabaseHelper.getWritableDatabase();
// 使用Android封装的SQL语法
ContentValues values = new ContentValues();
values.put("title", title);
values.put("content", content);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
new Locale("zh", "CN"));
Date d = new Date();
String date = sdf.format(d);
values.put("last_time",date);
if(lastTime == null) {
SimpleDateFormat sdf = new SimpleDateFormat(LAST_TIME_PATTERN,
new Locale("zh", "CN"));
Date d = new Date();
String date = sdf.format(d);
values.put("last_time", date);
}
else {
values.put("last_time", lastTime);
}
long id = database.insert(tableName,null,values);
database.close();
return id;
}

public void update(String title, String content, long id){
public void update(String title, String content, String lastTime, long id){
update(title, content, lastTime, -1, id);
}

public void update(String title, String content, String lastTime, int finish, long id){
SQLiteDatabase database = taskDatabaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("title", title);
values.put("content", content);
if(title != null)
values.put("title", title);
if(content != null)
values.put("content", content);
if(lastTime != null)
values.put("last_time", lastTime);
if(finish != -1){
values.put("finish", finish);
}
database.update(tableName, values, "id=?",
new String[]{Long.toString(id)});
database.close();
}


public long delete(long id){
SQLiteDatabase database = taskDatabaseHelper.getWritableDatabase();
String idStr = Long.toString(id);
Expand All @@ -79,7 +97,8 @@ public Task findById(long id){
task.setId(cursor.getLong(cursor.getColumnIndex("id")));
task.setTitle(cursor.getString(cursor.getColumnIndex("title")));
task.setContent(cursor.getString(cursor.getColumnIndex("content")));
task.setDate(cursor.getString(cursor.getColumnIndex("last_time")));
task.setLastTime(cursor.getString(cursor.getColumnIndex("last_time")));
task.setCreateTime(cursor.getString(cursor.getColumnIndex("create_time")));
task.setFinish(cursor.getInt(cursor.getColumnIndex("finish")));
}
cursor.close();
Expand All @@ -102,7 +121,8 @@ public List<Task> findAll(){
task.setTitle(cursor.getString(cursor.getColumnIndex("title")));
task.setContent(cursor.getString(cursor.getColumnIndex("content")));
task.setFinish(cursor.getInt(cursor.getColumnIndex("finish")));
task.setDate(cursor.getString(cursor.getColumnIndex("last_time")));
task.setLastTime(cursor.getString(cursor.getColumnIndex("last_time")));
task.setCreateTime(cursor.getString(cursor.getColumnIndex("create_time")));
taskList.add(task);
}
cursor.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.hzuapps.androidlabs.listview;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -10,30 +11,27 @@
import java.util.List;

import edu.hzuapps.androidlabs.model.Task;
import edu.hzuapps.androidlabs.presenter.TaskService;
import edu.hzuapps.androidlabs.soft1714080902223.HomeActivity;
import edu.hzuapps.androidlabs.soft1714080902223.R;

public class HomeListAdapter extends BaseAdapter {

private Context mContext;
private LayoutInflater mLayoutInflater;
//考虑到内存占用,不将此层与model分离,而使用其引用
//由于引用传递,又由于TaskService是单例,tasks赋值一次后便作为TaskService中list的的观察者
private List<Task> tasks;

public HomeListAdapter(Context context, List<Task> tasks){
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
this.tasks = tasks;
}
private TaskService taskService;

public HomeListAdapter(Context context){
mContext = context;
mLayoutInflater = LayoutInflater.from(context);
taskService = TaskService.INSTANCE.getTaskService();
taskService.setHomeListAdapter(this);
tasks = taskService.getAllList();
}

public void setTasks(List<Task> tasks){
if(tasks == null) {
return;
}
this.tasks = tasks;
public void notifyChange(){
// 通知ListView修改了
notifyDataSetChanged();
}
Expand All @@ -52,6 +50,7 @@ public int getCount() {
* @param position 参数为第几个对象
* @return 返回子项对应的对象
*/

@Override
public Object getItem(int position) {
//获取列表中的对象
Expand All @@ -61,15 +60,15 @@ public Object getItem(int position) {
/**
*
* @param position
* @return 返回子项的是第几个
* @return 返回对应位置的item的id
*/
@Override
public long getItemId(int position) {
return position;
return 0;
}

static class ViewHolder {
TextView lvTitle, lvTime, lvContent;
TextView lvTitle, lvTime, lvContent, lvStatus;
}

/**
Expand All @@ -80,10 +79,10 @@ static class ViewHolder {
* @return 返回子项视图
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
public View getView(final int position, View convertView, ViewGroup parent) {
Task task = (Task) getItem(position);
View view;
ViewHolder holder;
final ViewHolder holder;
//如果不存在,就去xml中获取,存在就直接用
if(convertView == null){
//关联list_item及其参数
Expand All @@ -92,16 +91,41 @@ public View getView(int position, View convertView, ViewGroup parent) {
holder.lvTitle = view.findViewById(R.id.lv_title);
holder.lvContent = view.findViewById(R.id.lv_content);
holder.lvTime = view.findViewById(R.id.lv_time);
holder.lvStatus = view.findViewById(R.id.lv_status);
view.setTag(holder);
}
else{
view = convertView;
holder = (ViewHolder) convertView.getTag();
}
//使用blog中的数据填上
//使用task中的数据填上
holder.lvTitle.setText(task.getTitle());
holder.lvContent.setText(task.getContent());
holder.lvTime.setText(task.getDate());
holder.lvTime.setText(taskService.getTextTime(task.getCreateTime()));

//根据Finish的值的不同更改样式
if(task.getFinish() == 0){
taskService.setStatus(holder.lvStatus, taskService.UNFINALSTATUS, position);
}else{
taskService.setStatus(holder.lvStatus, taskService.FINALSTATUS, position);
}

//设置点击更换标签样式
holder.lvStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(holder.lvStatus.getText() == taskService.FINALSTATUS){
taskService.setStatus(holder.lvStatus, taskService.UNFINALSTATUS, position);
}else{
taskService.setStatus(holder.lvStatus, taskService.FINALSTATUS, position);
}

}
});
return view;
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ public class Task {
private Long id;
private String title;
private String content;
private String date;
private String createTime;
private String lastTime;
private int finish;

public Long getId() {
Expand All @@ -32,12 +33,20 @@ public void setContent(String content) {
this.content = content;
}

public String getDate() {
return date;
public String getCreateTime() {
return createTime;
}

public void setDate(String date) {
this.date = date;
public void setCreateTime(String createTime) {
this.createTime = createTime;
}

public String getLastTime() {
return lastTime;
}

public void setLastTime(String lastTime) {
this.lastTime = lastTime;
}

public int getFinish() {
Expand All @@ -50,6 +59,7 @@ public void setFinish(int finish) {

@Override
public String toString() {
return "['title': " + getTitle() + ", 'content':" + getContent() + ", 'date':" + getDate() + "]";
return String.format("[title: %s, content:%s, last_time:%s, create_time:%s, finish: %d]",
getTitle(), getContent(), getLastTime(), getCreateTime(), getFinish());
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
package edu.hzuapps.androidlabs.presenter;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import edu.hzuapps.androidlabs.soft1714080902223.R;

public class JsonService {


public JSONObject getHomeJson(){
JSONObject jsonObject = null;
try {
//建立http连接
String url_s = "https://raw.githubusercontent.com/TRLVMMR/android-" +
String url = "https://raw.githubusercontent.com/TRLVMMR/android-" +
"labs-2019/bd5d63db1f4aae84b62860eb66f8733710936206/students/s" +
"oft1714080902223/app/src/main/info.json";
URL url = new URL(url_s);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setUseCaches(false);
conn.connect();
//获取文件放入输出流中
InputStream inputStream = conn.getInputStream();
// 通过url得到文件字节流
InputStream inputStream = NetworkService.getInternetData(url);
//如果没有获取到数据,返回null
if(inputStream == null){
return null;
}
InputStreamReader input = new InputStreamReader(inputStream);
BufferedReader buffer = new BufferedReader(input);
//当返回成功时,读取json数据
if(conn.getResponseCode() == 200){
String inputLine;
StringBuffer resultData = new StringBuffer();
while((inputLine = buffer.readLine())!= null){
resultData.append(inputLine);
}
jsonObject = new JSONObject(resultData.toString());
String inputLine;
StringBuffer resultData = new StringBuffer();
while ((inputLine = buffer.readLine()) != null) {
resultData.append(inputLine);
}
jsonObject = new JSONObject(resultData.toString());

} catch(Exception e){
//如果超时,或者其他异常,输出异常信息
e.printStackTrace();
Expand Down
Loading