在新活动中向数据库添加新数据

时间:2019-12-04 12:08:10

标签: android android-activity android-sqlite android-database

我正在创建一个应用程序,该应用程序将允许主键活动将数据添加到数据库,然后在另一个活动中将外键允许将其他数据添加到同一数据库。例如主要活动允许将橄榄球队添加到数据库,然后单击另一个活动以在单击特定橄榄球队时添加每个队成员。 下面是我使用的代码,这是用于将数据主要添加到数据库中的:

package com.example.coursework1;

import android.app.Activity;
import android.content.Intent;

import androidx.appcompat.app.AppCompatActivity;


public class AddNewCDActivity extends Activity {

    private int id;
    private String CDName;
    private String CDArtist;
    private String CDDuration;
    private byte[] image;

    public void CD(String CDName, String CDArtist, String CDDuration, byte[] image, int id) {
        this.CDName = CDName;
        this.CDArtist = CDArtist;
        this.CDDuration = CDDuration;
        this.image = image;
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCDName() {
        return CDName;
    }

    public void setCDName(String CDName) {
        this.CDName = CDName;
    }

    public String getCDArtist() {
        return CDArtist;
    }

    public void setCDArtist(String CDArtist) {
        this.CDArtist = CDArtist;
    }

    public String getCDDuration() {
        return CDDuration;
    }

    public void setCDDuration(String CDDuration) {
        this.CDDuration = CDDuration;
    }

    public void setImage(byte[] image) {
        this.image = image;

        databaseHelper = new DatabaseHelper(this);
    }

@Override
public boolean onCreateOptionsMenu(Menu menu){
        //Inflate the menu
        getMenuInflater().inflate(R.menu.add_new_cd_menu,menu);
        return true;
        }

@Override
public boolean onOptionsItemSelected(MenuItem item){
        //handle action bar clicks here. the action bar will automatically handle clicks on the home/up button
        int id=item.getItemId();

        if(id==R.id.action_Return){
        Intent resultIntent=new Intent();
        setResult(RESULT_OK,resultIntent);
        finish();
        }

        return super.onOptionsItemSelected(item);
        }

public void chooseCoverImage(View view){
        Intent intent=new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        String[]mimeTypes={"image/jpeg","image/png"};
        intent.putExtra(Intent.EXTRA_MIME_TYPES,mimeTypes);
//launching the intent
        startActivityForResult(intent,GALLERY_REQUEST_CODE);
        }//choose cover image

protected void onActivityResult(int requestCode,int resultCode,Intent data){
        if(requestCode==GALLERY_REQUEST_CODE){
        if(resultCode==RESULT_OK){}
        //an image was picked
        Uri selectedImage=data.getData();
        imagePath=getPath(getApplicationContext(),selectedImage);
        Toast.makeText(this,imagePath,Toast.LENGTH_LONG).show();

        ivNewCover=findViewById(R.id.ivNewCDCover);
        ivNewCover.setImageURI(selectedImage);
        bitmap=((BitmapDrawable)ivNewCover.getDrawable()).getBitmap();
        }

        private void startActivityForResult(); {
        Intent intent = new
                Intent(MainActivity.this, AddNewCDActivity.class);
        StartActivityForResult(intent, ADD_CD_REQUEST);
    }

我遇到的第一个错误是databaseHelper = newDatabaseHelper,然后@override也给了我错误。

在此先感谢您的帮助或提示。

1 个答案:

答案 0 :(得分:0)

Java要求为所有变量赋予类型。您需要声明 databaseHelper 是的变量的类型(它应该是DatabaseHelper的一种)。如此更改

databaseHelper = new DatabaseHelper(this);

DatabaseHelper databaseHelper = new DatabaseHelper(this);

或者您可以将DatabaseHelper databaseHelper;添加为成员/实例变量,例如

private int id;
private String CDName;
private String CDArtist;
private String CDDuration;
private byte[] image;
DatabaseHelper databaseHelper; //<<<<<<<<<<

并在创建活动时或至少在使用变量之前实例化它,通常在活动的 onCreate 方法中。这样做的好处是,作用域遍及整个类,并且可以在任何方法中使用,因此您不必在活动中创建多个实例,例如

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    databaseHelper = new DatabaseHelper(this);
}