无法将DatabaseHandler中的DatabaseHandler()应用于

时间:2018-09-20 11:44:47

标签: java android sqlite

我想创建sqlitedatabase,并希望将数据保存在其中。

SavetoDataAdapter.class

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.andisofttechnology.myapplication.DatabaseHandler;
import com.andisofttechnology.myapplication.R;

import java.util.List;

/**
 * Created by Alfraganus on 9/20/2018.
 */

public class SavedDataAdapter extends BaseAdapter {
LayoutInflater layoutInflater;
List<Student>studentList;
Context context;
DatabaseHandler databaseHandler;

public SavedDataAdapter(Context context,List<Student>list){
    this.studentList = list;
    this.context = context;
    layoutInflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    databaseHandler = new DatabaseHandler(this.context);
}

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

    @Override
    public Object getItem(int position) {
        return studentList.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.inflate(R.layout.activity_attendance, null);
       }


    return convertView;
    }

DatabaseHandler.class

import android.database.sqlite.SQLiteOpenHelper;

import com.andisofttechnology.myapplication.adapter.Student;

import java.util.ArrayList;
import java.util.List;


public class  DatabaseHandler extends SQLiteOpenHelper{

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "attendace";
    public static final String TABLE_STUDENT = "student";
    public static final  String STUDENT_ID = "_id";
    public static final  String STUDENT_NAME = "name";
    public static final  String STUDENT_SURNAME = "surname";
    public static final  String STUDENT_CONTACT = "contact";
    public static final String  STUDENT_ROLLNUM = "rollNum";


    private final Context mContext;
    SQLiteDatabase database;


    public DatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, Context mContext) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.mContext = mContext;
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
database.execSQL("CREATE TABLE " + TABLE_STUDENT + "(" + STUDENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + STUDENT_NAME + " TEXT, " + STUDENT_SURNAME + " TEXT, " + STUDENT_CONTACT + " TEXT, " + STUDENT_ROLLNUM + " TEXT" + ")");
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        database.execSQL("DROP TABLE IF EXISTS " + TABLE_STUDENT);
        onCreate(database);

    }

    public long StudentKiritish(Student studentList)
    {
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues cv = new ContentValues();

        cv.put(STUDENT_NAME,studentList.getName());
        cv.put(STUDENT_SURNAME,studentList.getSurname());
        cv.put(STUDENT_CONTACT,studentList.getContact());
        cv.put(STUDENT_ROLLNUM,studentList.getRollNum());

        Long insert = database.insert(TABLE_STUDENT, null, cv);
        database.close();
        return insert;

    }
public List<Student>studentLists() {
        List<Student> list = new ArrayList<>();
        SQLiteDatabase database = this.getReadableDatabase();
        String s = "select * from " + TABLE_STUDENT;
    Cursor cursor = database.rawQuery(s,null);

    if(cursor.moveToFirst()){
        do {
            list.add(new Student(cursor.getInt(cursor.getColumnIndex(STUDENT_ID)),
                    cursor.getString(cursor.getColumnIndex(STUDENT_NAME)),
                    cursor.getString(cursor.getColumnIndex(STUDENT_SURNAME)),
                    cursor.getString(cursor.getColumnIndex(STUDENT_ROLLNUM)),
                    cursor.getString(cursor.getColumnIndex(STUDENT_CONTACT))));

                           }while (cursor.moveToNext());

    }database.close();
    return list;
}
}

在SaveDataAdapter.class中,红线位于:databaseHandler = new databaseHandler(this.context)  说上下文不能应用于

请任何人告诉我问题是什么 ................................................... .................................

2 个答案:

答案 0 :(得分:1)

解决方案:

代替此:

public DatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, Context mContext) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.mContext = mContext;
}

写下:

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.mContext = context;
}

在您的DatabaseHandler班上。

就是这样。

希望有帮助。

答案 1 :(得分:0)

  

构造函数是用于初始化新创建的代码块   宾语。构造函数类似于Java中的实例方法,但是   不是一种方法,因为它没有返回类型。

不要

    public ICollection<BlogPostTagForCreationDto> BlogPostTags { get; set; }
        = new List<BlogPostTagForCreationDto>();

要做

 public DatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, Context mContext) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.mContext = mContext;
    }

然后下面的部分将起作用。

public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
         this.mContext = mContext;
    }