1. 程式人生 > >sqlite 增刪改查完整例子。

sqlite 增刪改查完整例子。

package mm.shandong.com.testsqlsqllite;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;



import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mm.shandong.com.testsqlsqllite.dao.PersonDao1;
import mm.shandong.com.testsqlsqllite.entity.Person;

public class TestSQLSqlLiteActivity extends AppCompatActivity {
    ListView listView;
    List<Person> persons;
    PersonDao1 personDao;
    BaseAdapter personAdapter;
    Map<String,Boolean> radioStates;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_sqlsql_lite);
        radioStates=new HashMap<String,Boolean>();
        personDao=new PersonDao1(this);
        listView= (ListView) findViewById(R.id.listView);
        persons=personDao.getAllPerson();
        initListView();
    }
    public void initListView(){
        personAdapter=new BaseAdapter() {
            @Override
            public int getCount() {
                return persons.size();
            }
            @Override
            public Object getItem(int position) {
                return persons.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }
            @Override
            public View getView(final  int position, View view, ViewGroup viewGroup) {
                view=getLayoutInflater().inflate(R.layout.item_sqlite_listview,null);
                TextView textViewPerson_name= (TextView) view.findViewById(R.id.textViewPerson_name);
                TextView textViewPerson_sex= (TextView) view.findViewById(R.id.textViewPerson_sex);
                TextView textViewPerson_code=(TextView)view.findViewById(R.id.textViewPerson_code);
                TextView textViewPerson_age= (TextView) view.findViewById(R.id.textViewPerson_age);
                final Person person= (Person) getItem(position);
                textViewPerson_name.setText(person.getName());
                textViewPerson_age.setText(person.getAge()+"");
                textViewPerson_sex.setText(person.getSex());
                textViewPerson_code.setText(person.getCode());
                RadioButton radioButton= (RadioButton) view.findViewById(R.id.radioButton);
                radioButton.setOnClickListener(new View.OnClickListener() {
                       @Override
                       public void onClick(View view) {
                           if(view instanceof RadioButton) {
                               RadioButton radioButton = (RadioButton) view;
                               if (radioButton.isChecked()) {
                                   radioStates.put(String.valueOf(position), true);
                                   for (String key : radioStates.keySet()) {
                                       if (!key.equals(String.valueOf(position))) {
                                           radioStates.put(key, false);
                                       }
                                   }
                                   notifyDataSetChanged();
                               }
                           }

                       }
                   }
                );
                Boolean tempState=radioStates.get(String.valueOf(position));
                if(tempState!=null&&tempState){
                    radioButton.setChecked(true);
                    view.setBackgroundColor(Color.BLUE);
                }else{
                    radioButton.setChecked(false);
                    view.setBackgroundColor(Color.WHITE);
                }
                return view;
            }
        };
        listView.setAdapter(personAdapter);
    }
    public void addPerson(View view){
        AlertDialog.Builder builder= new AlertDialog.Builder(this);
        builder.setTitle("新增使用者").create();
        View childView=getLayoutInflater().inflate(R.layout.layout_sqlite_alert,null);
        builder.setView(childView);
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Field field = null;
                try {
                    field = dialogInterface.getClass().getSuperclass().getDeclaredField("mShowing");
                    field.setAccessible(true);
                    field.set(dialogInterface, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                try {
                    Field field = dialogInterface.getClass().getSuperclass().getDeclaredField("mShowing");
                    field.setAccessible(true);
                    field.set(dialogInterface,false);
                    AlertDialog ad;
                    if(dialogInterface instanceof AlertDialog){
                        ad= (AlertDialog) dialogInterface;
                        EditText editViewPerson_name=
                                (EditText) ad.findViewById(R.id.editViewPerson_name);
                        EditText editViewPerson_code=
                                (EditText) ad.findViewById(R.id.editViewPerson_code);
                        EditText editViewPerson_age=
                                (EditText) ad.findViewById(R.id.editViewPerson_age);
                        RadioGroup radioGroupSex=
                                (RadioGroup) ad.findViewById(R.id.radioGroupSex);
                        if(TextUtils.isEmpty(editViewPerson_name.getText().toString())){
                            Toast.makeText(TestSQLSqlLiteActivity.this,
                                    "姓名不能為空",Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if(TextUtils.isEmpty(editViewPerson_code.getText().toString())){
                            Toast.makeText(TestSQLSqlLiteActivity.this,
                                    "編號不能為空",Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if(TextUtils.isEmpty(editViewPerson_age.getText().toString())){
                            Toast.makeText(TestSQLSqlLiteActivity.this,
                                    "年齡不能為空",Toast.LENGTH_SHORT).show();
                            return;
                        }
                        Person person=new Person();
                        person.setAge(Integer.parseInt(editViewPerson_age.getText().toString()));
                        person.setName(editViewPerson_name.getText().toString());
                        person.setCode(editViewPerson_code.getText().toString());
                        RadioButton radioButton= (RadioButton) radioGroupSex.
                                findViewById(radioGroupSex.getCheckedRadioButtonId());
                        person.setSex(radioButton.getText().toString());
                        personDao.addPerson(person);
                        persons=personDao.getAllPerson();
                        radioStates=new HashMap<String,Boolean>();
                        personAdapter.notifyDataSetChanged();
                        field.set(dialogInterface,true);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        builder.show();

    }
    public void deletePerson(View view){
        int position=-1;
        for(int i=0;i<listView.getChildCount();i++){
            View childView=listView.getChildAt(i);
            RadioButton radioButton= (RadioButton) childView.findViewById(R.id.radioButton);
            if(radioButton.isChecked()){
                position=i;
                break;
            }
        }
        if(position!=-1){
            Person person= persons.get(position);
            personDao.deletePerson(person);
            persons=personDao.getAllPerson();
            radioStates=new HashMap<String,Boolean>();
            personAdapter.notifyDataSetChanged();

        }else{
            Toast.makeText(this,"請選擇要刪除的行",Toast.LENGTH_SHORT);
        }
    }
    public void updatePerson(View view){
        int position=-1;
        for(int i=0;i<listView.getChildCount();i++){
            View childView=listView.getChildAt(i);
            RadioButton radioButton= (RadioButton) childView.findViewById(R.id.radioButton);
            if(radioButton.isChecked()){
                position=i;
                break;
            }
        }
        if(position!=-1){
            final Person person= persons.get(position);
            AlertDialog.Builder builder= new AlertDialog.Builder(this);
            builder.setTitle("修改使用者").create();
            View childView=getLayoutInflater().inflate(R.layout.layout_sqlite_alert,null);
            EditText editViewPerson_name=
                    (EditText) childView.findViewById(R.id.editViewPerson_name);
            EditText editViewPerson_code=
                    (EditText) childView.findViewById(R.id.editViewPerson_code);
            EditText editViewPerson_age=
                    (EditText) childView.findViewById(R.id.editViewPerson_age);
            RadioGroup radioGroupSex=
                    (RadioGroup) childView.findViewById(R.id.radioGroupSex);
            editViewPerson_name.setText(person.getName());
            editViewPerson_age.setText(String.valueOf(person.getAge()));
            editViewPerson_code.setText(person.getCode());
            if(person.getSex().equals("男")){
                ((RadioButton)radioGroupSex.getChildAt(0)).setChecked(true);
            }else{
                ((RadioButton)radioGroupSex.getChildAt(1)).setChecked(true);
            }
            builder.setView(childView);
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                }
            });
            builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                    try {
                        Field field = dialogInterface.getClass().getSuperclass().getDeclaredField("mShowing");
                        field.setAccessible(true);
                        field.set(dialogInterface,false);
                        AlertDialog ad;
                        if(dialogInterface instanceof AlertDialog){
                            ad= (AlertDialog) dialogInterface;
                            EditText editViewPerson_name=
                                    (EditText) ad.findViewById(R.id.editViewPerson_name);
                            EditText editViewPerson_age=
                                    (EditText) ad.findViewById(R.id.editViewPerson_age);
                            EditText editViewPerson_code=
                                    (EditText) ad.findViewById(R.id.editViewPerson_code);
                            RadioGroup radioGroupSex= (RadioGroup) ad.findViewById(R.id.radioGroupSex);

                            if(TextUtils.isEmpty(editViewPerson_name.getText().toString())){
                                Toast.makeText(TestSQLSqlLiteActivity.this,
                                        "姓名不能為空",Toast.LENGTH_SHORT).show();
                                return;
                            }
                            if(TextUtils.isEmpty(editViewPerson_age.getText().toString())){
                                Toast.makeText(TestSQLSqlLiteActivity.this,
                                        "年齡不能為空",Toast.LENGTH_SHORT).show();
                                return;
                            }

                            person.setAge(Integer.parseInt(editViewPerson_age.getText().toString()));
                            person.setName(editViewPerson_name.getText().toString());
                            RadioButton radioButton= (RadioButton) radioGroupSex.
                                    findViewById(radioGroupSex.getCheckedRadioButtonId());
                            person.setSex(radioButton.getText().toString());
                            person.setCode(editViewPerson_code.getText().toString());
                            personDao.updatePerson(person);
                            persons=personDao.getAllPerson();
                            radioStates=new HashMap<String,Boolean>();
                            personAdapter.notifyDataSetChanged();
                            field.set(dialogInterface,true);
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            builder.show();

        }else{
            Toast.makeText(this,"請選擇要刪除的行",Toast.LENGTH_SHORT);
        }
    }

}