Skip to content

Commit

Permalink
hzuapps#2 hzuapps#158 第二次作业
Browse files Browse the repository at this point in the history
  • Loading branch information
anzhoulin committed Jun 2, 2017
1 parent ebde8fd commit 98513f6
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package edu.hzuapps.androidlabs.homeworks.net1414080903119;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.administrator.linjingzhen.R;

public class Net1414080903119AddActivity extends AppCompatActivity {

EditText etName;
EditText etSchool;
EditText etAge;
EditText etHours;

Button bt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net1414080903119_add);
etName= (EditText) findViewById(R.id.et_add_name);
etAge= (EditText) findViewById(R.id.et_add_age);
etHours= (EditText) findViewById(R.id.et_add_hours);
etSchool= (EditText) findViewById(R.id.et_add_school);
bt= (Button) findViewById(R.id.bt_add);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Teacher teacher=new Teacher();
teacher.setSchool(etSchool.getText().toString());
teacher.setHours(etHours.getText().toString());
teacher.setAge(etAge.getText().toString());
teacher.setName(etName.getText().toString());
Net1414080903119Presenter.getInstance(Net1414080903119AddActivity.this).addTeacher(teacher);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package edu.hzuapps.androidlabs.homeworks.net1414080903119;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.linjingzhen.R;

import java.util.List;

public class Net1414080903119ListActivity extends AppCompatActivity {

ListView lv;
List<Teacher> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net1414080903119_list);
list= Net1414080903119Presenter.getInstance(this).listTeacher();
final MyAdapter adapter=new MyAdapter();
lv.setAdapter(adapter);
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Net1414080903119Presenter.getInstance(Net1414080903119ListActivity.this).deleteTeacher(list.get(position).getName());
adapter.notifyDataSetChanged();
return false;
}
});
}

class MyAdapter extends BaseAdapter{

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int position) {
return list.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null){
convertView= LayoutInflater.from(Net1414080903119ListActivity.this).inflate(R.layout.item_net1414080903119,null);
}
Teacher teacher=list.get(position);
TextView tvName= (TextView) findViewById(R.id.tv_item_name);
TextView tvAge= (TextView) findViewById(R.id.tv_item_age);;
TextView tvSchool= (TextView) findViewById(R.id.tv_item_school);;
TextView tvHours= (TextView) findViewById(R.id.tv_item_hours);;
tvAge.setText(teacher.getAge());
tvName.setText(teacher.getName());
tvSchool.setText(teacher.getSchool());
tvHours.setText(teacher.getHours());
return convertView;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package edu.hzuapps.androidlabs.homeworks.net1414080903119;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.administrator.linjingzhen.R;

public class Net1414080903119MainActivity extends AppCompatActivity {

Button btAdd;
Button btList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net1414080903119_main);
btAdd= (Button) findViewById(R.id.bt_to_add);
btList= (Button) findViewById(R.id.bt_to_list);
btAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Net1414080903119MainActivity.this,Net1414080903119AddActivity.class));
}
});
btList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Net1414080903119MainActivity.this,Net1414080903119ListActivity.class));
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package edu.hzuapps.androidlabs.homeworks.net1414080903119;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Administrator on 2017/5/31.
*/

public class Net1414080903119Presenter {

private SQLiteOpenHelper helper;
private static Net1414080903119Presenter presenter;

private Net1414080903119Presenter(Context context){
if (helper==null){
helper=new Net1414080903119SQLiteHelper(context);
}
}

public static Net1414080903119Presenter getInstance(Context context){
if (presenter==null){
presenter=new Net1414080903119Presenter(context);
}
return presenter;
}

public void addTeacher(Teacher teacher){
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",teacher.getName());
values.put("age",teacher.getAge());
values.put("school",teacher.getSchool());
values.put("hours",teacher.getHours());
db.insert("teacher",null,values);
db.close();
}

public List<Teacher> listTeacher(){
List<Teacher> list=new ArrayList<>();
SQLiteDatabase db=helper.getReadableDatabase();
Cursor cursor=db.query("teacher",null,null,null,null,null,null);
while (cursor.moveToNext()){
Teacher teacher=new Teacher();
teacher.setName(cursor.getColumnName(cursor.getColumnIndex("name")));
teacher.setAge(cursor.getColumnName(cursor.getColumnIndex("age")));
teacher.setHours(cursor.getColumnName(cursor.getColumnIndex("hours")));
teacher.setSchool(cursor.getColumnName(cursor.getColumnIndex("school")));
list.add(teacher);
}
cursor.close();
db.close();
return list;
}

public void update(String data,String name,String where){

}

public void deleteTeacher(String name){
SQLiteDatabase db=helper.getWritableDatabase();
db.delete("teacher","name=?",new String[]{name});
db.close();
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package edu.hzuapps.androidlabs.homeworks.net1414080903119;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.administrator.linjingzhen.R;

public class Net1414080903119UpdateActivity extends AppCompatActivity {

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

0 comments on commit 98513f6

Please sign in to comment.