Chamiko / CNPM

0 stars 0 forks source link

sql #7

Open Chamiko opened 6 years ago

Chamiko commented 6 years ago

package com.samsung.androidday6sqlite;

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

/**

public class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); }

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(DBInfor.CREAT_USER_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String sql = "DROP TABLE IF EXISTS " + DBInfor.TABLE_USER;
    db.execSQL(sql);
    onCreate(db);
}

}

Chamiko commented 6 years ago

package com.samsung.androidday6sqlite;

/**

public class DBInfor { public static final String DB_NAME = "student.db"; public static final String TABLE_USER = "user"; public static final String CL_ID = "id"; public static final String CL_NAME = "name"; public static final String CL_AGE = "age"; public static final String CL_SCHOOL = "school"; public static final String CL_IMAGE = "Image"; public static final int DB_VERSION = 1;

public static final String CREAT_USER_TABLE =
        "CREATE TABLE " + TABLE_USER + "(" + CL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + CL_NAME + " TEXT, " + CL_AGE + " INTEGER, " + CL_SCHOOL + " TEXT, " + CL_IMAGE + " TEXT)";

}

Chamiko commented 6 years ago

package com.samsung.androidday6sqlite;

import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast;

import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List;

public class MainActivity extends AppCompatActivity implements MyClick, View.OnClickListener { private static final int PICK_IMAGE = 1; private StudentManager studentDatabase; private RecyclerView rvStudent; private ArrayList studentArrayList; private StudentAdapter adapter; private EditText edtName, edtAge, edtSchool; private Button btnAdd, btnDelete, btnEdit; private ImageView img; public Uri image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initViews();
    studentDatabase = new StudentManager(this);
    ArrayList<Student> arrStudents = studentDatabase.getAllStudent();

    //querry

// for(int i = 0; i < arrStudents.size(); i++){ // Student student = arrStudents.get(i); // Toast.makeText(this, student.toString(), Toast.LENGTH_LONG).show(); // }

    //them

// long res = studentDatabase.addStudent("QooBee", 18, "RoyalBee"); // Toast.makeText(this, "So dong: " + res,Toast.LENGTH_SHORT).show();

    //xoa

// int res = studentDatabase.deleteStudent(2); // if(res > 0){ // Toast.makeText(this, "xoa ok " + res,Toast.LENGTH_SHORT).show(); // }else { // Toast.makeText(this, "ko xoa dc " + res,Toast.LENGTH_SHORT).show(); // }

// int res = studentDatabase.updateStudent(1, "QooBee cute", 15, "RoyalBee"); // if(res > 0){ // Toast.makeText(this, "update ok " + res,Toast.LENGTH_SHORT).show(); // }else { // Toast.makeText(this, "ko update dc " + res,Toast.LENGTH_SHORT).show(); // } }

private void initViews() {
    img = (ImageView) findViewById(R.id.img);
    edtName = (EditText) findViewById(R.id.edt_name);
    edtAge = (EditText) findViewById(R.id.edt_age);
    edtSchool = (EditText) findViewById(R.id.edt_school);
    btnAdd = (Button) findViewById(R.id.btn_add);
    btnDelete = (Button) findViewById(R.id.btn_delete);
    btnEdit = (Button) findViewById(R.id.btn_edit);
    rvStudent = (RecyclerView) findViewById(R.id.rv_student);
    studentArrayList=new ArrayList<>();
    adapter = new StudentAdapter(studentArrayList, MainActivity.this, this);
    LinearLayoutManager layoutManager= new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
    rvStudent.setLayoutManager(layoutManager);
    rvStudent.setAdapter(adapter);

    img.setOnClickListener(this);
    btnAdd.setOnClickListener(this);
    btnDelete.setOnClickListener(this);
    btnEdit.setOnClickListener(this);

}

@Override
public void onItemClick(int position, View view) {

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.img:
            readSrcImage();
            break;
        case R.id.btn_add:
            if(fullInfo()){
                Student s = new Student();
                String sName = edtName.getText().toString();
                int sAge = Integer.parseInt(edtAge.getText().toString());
                String sSchool = edtSchool.getText().toString();
                String sImage = image.toString();
                long res = studentDatabase.addStudent(sName, sAge, sSchool, sImage);
                Toast.makeText(this, "So dong: " + res,Toast.LENGTH_SHORT).show();
                studentArrayList.clear();
                studentArrayList=studentDatabase.getAllStudent();
                adapter.setStudentArrayList(studentArrayList);

            } else {
                Toast.makeText(MainActivity.this, "Please! Fill out full information", Toast.LENGTH_SHORT).show();
            }
            break;
        case R.id.btn_delete:
            break;
        case R.id.btn_edit:
            break;
    }
}

private void readSrcImage() {
    Intent imgIntent = new Intent();
    imgIntent.setType("image/*");
    imgIntent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(imgIntent, "Select Picture"), PICK_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE) {
        if ((resultCode == RESULT_OK) && (data != null) && (data.getData() != null)) {
            image = data.getData();
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), image);
                img.setImageBitmap(bitmap);
                img.setScaleType(ImageView.ScaleType.CENTER_CROP);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

private boolean fullInfo() {
    if (image == null) {
        return false;
    }
    if (edtName.getText().toString().compareTo("") == 0) {
        return false;
    }
    if (edtAge.getText().toString().compareTo("") == 0) {
        return false;
    }
    if (edtSchool.getText().toString().compareTo("") == 0) {
        return false;
    }

    return true;
}

}

Chamiko commented 6 years ago

package com.samsung.androidday6sqlite;

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

import java.util.ArrayList;

/**

public class StudentManager { private DBHelper dbHelper; private SQLiteDatabase database; private Context context;

public StudentManager(Context context) {
    this.context = context;
    dbHelper = new DBHelper(context, DBInfor.DB_NAME, null, DBInfor.DB_VERSION);
    database = dbHelper.getWritableDatabase();
}

public long addStudent(String name, int age, String school, String image) {
    ContentValues cv = new ContentValues();
    cv.put(DBInfor.CL_NAME, name);
    cv.put(DBInfor.CL_AGE, age);
    cv.put(DBInfor.CL_SCHOOL, school);
    cv.put(DBInfor.CL_IMAGE, image);

    long rows = database.insert(DBInfor.TABLE_USER, null, cv); //nullColumHack cot ko co gia tri trung
    return rows;
}

public int deleteStudent(int id) {
    int rows = database.delete(DBInfor.TABLE_USER, DBInfor.CL_ID + "=?", new String[]{String.valueOf(id)});
    return rows;
}

public int updateStudent(int id, String name, int age, String school, String image) {
    ContentValues cv = new ContentValues();
    cv.put(DBInfor.CL_NAME, name);
    cv.put(DBInfor.CL_AGE, age);
    cv.put(DBInfor.CL_SCHOOL, school);
    cv.put(DBInfor.CL_IMAGE, image);
    int rows = database.update(DBInfor.TABLE_USER, cv, DBInfor.CL_ID + "=?", new String[]{String.valueOf(id)});
    return rows;
}

public ArrayList<Student> getAllStudent() {
    ArrayList<Student> arrStudent = new ArrayList<>();
    Cursor cursor = database.query(DBInfor.TABLE_USER, null, null, null, null, null, null, null);

// cursor.moveToFirst(); while (cursor.moveToNext()) { int idIndex = cursor.getColumnIndex(DBInfor.CL_ID); int nameIndex = cursor.getColumnIndex(DBInfor.CL_NAME); int ageIndex = cursor.getColumnIndex(DBInfor.CL_AGE); int schoolIndex = cursor.getColumnIndex(DBInfor.CL_SCHOOL); int imageIndex = cursor.getColumnIndex(DBInfor.CL_IMAGE);

        int id = cursor.getInt(idIndex);
        String name = cursor.getString(nameIndex);
        int age = cursor.getInt(ageIndex);
        String school = cursor.getString(schoolIndex);
        String image = cursor.getString(imageIndex);

        Student student = new Student(id, name, age, school, image);
        arrStudent.add(student);
    }
    return arrStudent;
}

}

Chamiko commented 6 years ago

package com.samsung.androidday6sqlite;

import android.content.ContentValues; import android.content.Context; import android.graphics.Bitmap; import android.net.Uri; import android.provider.MediaStore; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView;

import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList;

/**

public class StudentAdapter extends RecyclerView.Adapter{ private ArrayList studentArrayList; private Context context; private MyClick myClick;

public StudentAdapter(ArrayList<Student> studentArrayList, Context context, MyClick myClick) {
    this.studentArrayList = studentArrayList;
    this.context = context;
    this.myClick = myClick;
}

public StudentAdapter(ArrayList<Student> studentArrayList, Context context) {
    this.studentArrayList = studentArrayList;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(context).inflate(R.layout.layout_item, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Student student = studentArrayList.get(position);
    holder.tvName.setText(student.getName());
    holder.tvAge.setText(student.getAge()+"");
    holder.tvSchool.setText(student.getSchool());

    try {
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), Uri.parse(student.getImage()));
        holder.img.setImageBitmap(bitmap);
        holder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public void setStudentArrayList(ArrayList<Student> studentArrayList) {
    this.studentArrayList = studentArrayList;
    notifyDataSetChanged();
}

@Override
public int getItemCount() {
    return studentArrayList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView tvName, tvAge, tvSchool;
    ImageView img;
    public ViewHolder(final View itemView) {
        super(itemView);
        img = itemView.findViewById(R.id.img);
        tvName = itemView.findViewById(R.id.tv_name);
        tvAge = itemView.findViewById(R.id.tv_age);
        tvSchool = itemView.findViewById(R.id.tv_school);

        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myClick.onItemClick(getAdapterPosition(), itemView);
            }
        });

        tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myClick.onItemClick(getAdapterPosition(), itemView);
            }
        });

        tvAge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myClick.onItemClick(getAdapterPosition(), itemView);
            }
        });

        tvSchool.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myClick.onItemClick(getAdapterPosition(), itemView);
            }
        });

    }
}

}