diff --git a/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903119/Net1414080903119AddActivity.java b/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903119/Net1414080903119AddActivity.java new file mode 100644 index 00000000..93ffb5cd --- /dev/null +++ b/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903119/Net1414080903119AddActivity.java @@ -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); + } + }); + } +} diff --git a/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903119/Net1414080903119ListActivity.java b/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903119/Net1414080903119ListActivity.java new file mode 100644 index 00000000..58101fa9 --- /dev/null +++ b/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903119/Net1414080903119ListActivity.java @@ -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 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; + } + } +} diff --git a/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903119/Net1414080903119MainActivity.java b/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903119/Net1414080903119MainActivity.java new file mode 100644 index 00000000..62123ff9 --- /dev/null +++ b/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903119/Net1414080903119MainActivity.java @@ -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)); + } + }); + } +} diff --git a/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903119/Net1414080903119Presenter.java b/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903119/Net1414080903119Presenter.java new file mode 100644 index 00000000..ed954c58 --- /dev/null +++ b/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903119/Net1414080903119Presenter.java @@ -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 listTeacher(){ + List 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(); + } + + +} diff --git a/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903119/Net1414080903119UpdateActivity.java b/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903119/Net1414080903119UpdateActivity.java new file mode 100644 index 00000000..ed97b796 --- /dev/null +++ b/AndroidLabs/app/src/main/java/edu/hzuapps/androidlabs/homeworks/net1414080903119/Net1414080903119UpdateActivity.java @@ -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); + } +}