Mình bị bí không biết cách gọi hàm mong các anh em giúp đỡ

thienly

New Member
1/11/21
1
0
1
SÀI GÒN
Đoạn code này mình gọi database.exportToSD(); không được

CongViecAdapter.java

package com.example.sqlitedatabasekpt;
Java:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class CongViecAdapter extends BaseAdapter {
    private Context context;
    private int layout;
    private Database database;

    public CongViecAdapter(Context context, int layout, List<CongViec> congViecList) {
        this.context = context;
        this.layout = layout;
        this.congViecList = congViecList;
    }

    private List<CongViec> congViecList;

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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
private class ViewHolder{
        TextView txtTen ;
        ImageView imgDelete, imgEdit;
}
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if(view == null)
        {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(layout,null);
            holder.txtTen       = (TextView) view.findViewById(R.id.textviewTen);
            holder.imgDelete    = (ImageView) view.findViewById(R.id.imageviewDelete);
            holder.imgEdit      = (ImageView) view.findViewById(R.id.imageviewEdit);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }

        CongViec congViec = congViecList.get(i);
        holder.txtTen.setText(congViec.getTenCV());
        //Bắt sự kiện xóa và sửa
        holder.imgEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "Sửa" + congViec.getTenCV(), Toast.LENGTH_SHORT).show();
                // xuất dữ liệu
               // database = new Database();
               // database = new Database(this, "ghichu.sqlite",null,1);
                database.exportToSD();
            }
        });

        return view;
    }
}
+++  Còn đây là class Database.java của mình
package com.example.sqlitedatabasekpt;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.os.Environment;
import android.view.View;

import androidx.annotation.Nullable;

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



    // CREATE, INSERT, UPDATE, DELETE
    public void QueryData(String sql)
    {
        SQLiteDatabase database = getWritableDatabase();
        database.execSQL(sql);
    }
    //SELECT
    public Cursor getDaTa(String sql){
        SQLiteDatabase database = getReadableDatabase();
        return database.rawQuery(sql,null);

    }
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    // create folder if it not exist
    // folder on sd to backup data
    private final String folderSD = Environment.getExternalStorageDirectory() + "/MyNote";
    // name of main data

    private final String dataName = "ghichu.sqlite";
    private final String dataPath = "//data//com.example.sqlitedatabasekpt//databases//";




    // data main
    private final String data = dataPath + dataName;

    // name of temp data
    private final String dataTempName = dataName + "_temp";

    // temp data for copy data from sd then copy data temp into main data
    private final String dataTemp = dataPath + dataTempName;

    private OnBackupListener onBackupListener;

    public void setOnBackupListener(OnBackupListener onBackupListener) {
        this.onBackupListener = onBackupListener;
    }
    public interface OnBackupListener {
        public void onFinishExport(String error);

        public void onFinishImport(String error);
    }

    private Context context;
    private void createFolder() {
        File sd = new File(folderSD);
        if (!sd.exists()) {
            sd.mkdir();
            System.out.println("create folder");
        } else {
            System.out.println("exits");
        }
    }

    public void exportToSD() {

        String error = null;
        try {

            createFolder();

            File sd = new File(folderSD);

            if (sd.canWrite()) {

                SimpleDateFormat formatTime = new SimpleDateFormat("yyyy_MM_dd__HH_mm_ss");
                String backupDBPath = dataName + "_" + formatTime.format(new Date());

                File currentDB = new File(Environment.getDataDirectory(), data);
                File backupDB = new File(sd, backupDBPath);

                if (currentDB.exists()) {
                    FileChannel src = new FileInputStream(currentDB).getChannel();
                    FileChannel dst = new FileOutputStream(backupDB).getChannel();
                    dst.transferFrom(src, 0, src.size());
                    src.close();
                    dst.close();
                } else {
                    System.out.println("db not exist");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            error = "Error backup";
        }
        onBackupListener.onFinishExport(error);
    }
}
 
Last edited by a moderator:

Joe

Thành viên VIP
21/1/13
3,022
1,335
113
what is "mình gọi database.exportToSD(); không được " ?
What do the errors say?
Have you checked the SD card whether it is writable or protected?
Remember 2 things:
  1. Computer is stupid because it always does what it is programmed and
  2. AI (Artificial Intelligence) is as "intelligent" as its creators. If its creators made a wrong move AI always follows this WRONG MOVE.
 
Sửa lần cuối: