Person类
public class Person {
private int _id;
private String name;
private int age;
Person(int _id, String name, int age) {
this._id = _id;
this.name = name;
this.age = age;
}
public void set_id(int _id) {
this._id = _id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public int get_id(){
return _id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
main avtivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
MyDAO myDAO;
ListView listView;
List<Person>listData;
BaseAdapter adapter;
int selId;
EditText et_name;
EditText et_age;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_add=findViewById(R.id.bt_add);bt_add.setOnClickListener(this);
Button bt_modify=findViewById(R.id.bt_modify);bt_modify.setOnClickListener(this);
Button bt_del=findViewById(R.id.bt_del);bt_del.setOnClickListener(this);
et_name=findViewById(R.id.et_name);
et_age= findViewById(R.id.et_age);
myDAO=new MyDAO(this);
if(myDAO.getRecordsNumber()==0){
myDAO.insertInfo("tian",20);
myDAO.insertInfo("wang",40);
}
displayRecords();
}
public void displayRecords(){
listView=findViewById(R.id.listView);
listData=new ArrayList<Person>();
Cursor cursor=myDAO.allQuery();
while (cursor.moveToNext()){
int id=cursor.getInt(0);
String name=cursor.getString(1);
int age=cursor.getInt(cursor.getColumnIndex("age"));
Person person=new Person(id,name,age);
listData.add(person);
}
adapter=new BaseAdapter() {
@Override
public int getCount() {
return listData.size();
}
@Override
public Object getItem(int position) {
return listData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@RequiresApi(api = 28)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item=View.inflate(getApplicationContext(),R.layout.list_item,null);
TextView idTV=item.findViewById(R.id.tv_id);
TextView nameTV=item.findViewById(R.id.tvname);
TextView ageTV=item.findViewById(R.id.tvage);
Person p=listData.get(position);
idTV.setText(p.get_id()+"");
nameTV.setText(p.getName());
ageTV.setText(p.getAge()+"");
return item;
}
};
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@RequiresApi(api = 28)
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Person rec=(Person)adapter.getItem(position);
et_name.setText(rec.getName());
et_age.setText(rec.getAge()+"");
selId=rec.get_id();
}
});
}
@Override
public void onClick(View v){
if(selId>0){
String p1 =et_name.getText().toString().trim();
int p2=Integer.parseInt(et_age.getText().toString());
switch (v.getId()){
case R.id.bt_add:
myDAO.insertInfo(p1,p2);
break;
case R.id.bt_modify:
myDAO.updateInfo(p1,p2,selId);
Toast.makeText(getApplicationContext(),"更新成功!",Toast.LENGTH_SHORT).show();
break;
case R.id.bt_del:
myDAO.deleteInfo(selId);
Toast.makeText(getApplicationContext(),"删除成功!",Toast.LENGTH_SHORT).show();
et_name.setText(null);et_age.setText(null);selId=0;
}
}else {
if(v.getId()==R.id.bt_add){
String p1=et_name.getText().toString();
String p2=et_age.getText().toString();
if(p1.equals("")||p2.equals("")){
Toast.makeText(getApplicationContext(),"姓名和年龄都不能空!",Toast.LENGTH_SHORT).show();
}else {
myDAO.insertInfo(p1,Integer.parseInt(p2));
}
}else {
Toast.makeText(getApplicationContext(),"请先选择记录!",Toast.LENGTH_SHORT).show();
}
}
displayRecords();
}
}