Jump to content

dbOpenHelper variable is not found

Guest
package com.example.calendarevent;

import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.provider.CalendarContract;
import android.text.Layout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import androidx.annotation.Nullable;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TimeZone;

public class customcalendar extends LinearLayout {
    ImageButton NextButton,PreviousButton;
    TextView CurrentDate;
    GridView gridView;
    private static final int MAX_CALENDAR_DAYS = 42;
    Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
    Context context;
    SimpleDateFormat dateFormat = new SimpleDateFormat( "MMMM yyyy", Locale.ENGLISH);
    SimpleDateFormat monthFormat = new SimpleDateFormat( "MMMM",Locale.ENGLISH);
    SimpleDateFormat yearFormat = new SimpleDateFormat( "yyyy",Locale.ENGLISH);


    AlertDialog alertDialog
    List<Date> dates = new ArrayList<>();
    List<Events> eventsList = new ArrayList<>();


    public customcalendar(Context context) {
        super(context);
    }

    public customcalendar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        InitializeLayout();
        SetUpCalendar();

        PreviousButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                calendar.add(Calendar.MONTH, -1);
                SetUpCalendar();
            }
        });

        NextButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                calendar.add(Calendar.MONTH, 1);
                SetUpCalendar();
            }
        });

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setCancelable(true);
                View addView = LayoutInflater.from(parent.getContext()).inflate(R.layout.add_newevent_layout,null);
                EditText EventName = addView.findViewById(R.id.events_id);
                TextView EventTime = addView.findViewById(R.id.eventtime);
                ImageButton SetTime = addView.findViewById(R.id.seteventtime);
                Button AddEvent = addView.findViewById(R.id.addevent);
                SetTime.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Calendar calendar = Calendar.getInstance();
                        int hours = calendar.get(Calendar.HOUR_OF_DAY);
                        int minute = calendar.get(Calendar.MINUTE);
                        TimePickerDialog timePickerDialog = new TimePickerDialog(addView.getContext(), R.style.Theme_AppCompat_Dialog
                                , new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                Calendar c = Calendar.getInstance();
                                c.set(Calendar.HOUR_OF_DAY,hourOfDay);
                                c.set(Calendar.MINUTE,minute);
                                c.setTimeZone(TimeZone.getDefault());
                                SimpleDateFormat hformate = new SimpleDateFormat("K:mm a",Locale.ENGLISH);
                                String event_Time = hformate.format(c.getTime());
                                EventTime.setText(event_Time);

                            }
                        },hours,minute, false);
                        timePickerDialog.show();
                    }
                });
                AddEvent.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
            }
        });


    }

    public customcalendar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    private void SaveEvent(String event,String time,String date, String month,String year) {
        dbOpenHelper = new DBOpenHelper(context);

    }

    private void InitializeLayout() {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.calendar_layout,this);
        NextButton = view.findViewById(R.id.nextbutton);
        PreviousButton = view.findViewById(R.id.previousbutton);
        CurrentDate = view.findViewById(R.id.current_date);
        gridView = view.findViewById(R.id.gridview);
    }

    private void SetUpCalendar(){
        String currwntDate = dateFormat.format(calendar.getTime());
        CurrentDate.setText(currwntDate);
        dates.clear();
        Calendar monthCalendar= (Calendar) calendar.clone();
        monthCalendar.set(Calendar.DAY_OF_MONTH,1);
        int FirstDayOfMonth = monthCalendar.get(Calendar.DAY_OF_WEEK)-1;
        monthCalendar.add(Calendar.DAY_OF_MONTH, -FirstDayOfMonth);

        while (dates.size() < MAX_CALENDAR_DAYS) {
            dates.add(monthCalendar.getTime());
            monthCalendar.add(Calendar.DAY_OF_MONTH, 1);

        }
    }



}

In the code i typed in dbOpenHelper but it doesn't find the variable so its red or error right now. I wonder what should do to get the variable in or available. is this a problem from import? if so what import should i put in?

Link to comment
Share on other sites

Link to post
Share on other sites

dbOpenHelper = new DBOpenHelper(context);

this is the only instance of "dbOpenHelper" in your script, but you never actually declare the variable, you never tell the code what "dbOpenHelper" is.

What I think you want to do is this:

DBOpenHelper dbOpenHelper = new DBOpenHelper(context);

 

to explain it a different way, see it as:

myVariable = 4
// vs.
int myVariable = 4

when making a variable you have to declare the type of variable too, otherwise the code doesn't know what to do with it.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, minibois said:

dbOpenHelper = new DBOpenHelper(context);

this is the only instance of "dbOpenHelper" in your script, but you never actually declare the variable, you never tell the code what "dbOpenHelper" is.

What I think you want to do is this:


DBOpenHelper dbOpenHelper = new DBOpenHelper(context);

 

to explain it a different way, see it as:


myVariable = 4
// vs.
int myVariable = 4

when making a variable you have to declare the type of variable too, otherwise the code doesn't know what to do with it.

where should i insert the declare. do i put it in java class for the main code or at the java class i made for  DBOpenHelper. I'm very new to this and don't know where to put in the code.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Chris2002 said:

where should i insert the declare. do i put it in java class for the main code or at the java class i made for  DBOpenHelper. I'm very new to this and don't know where to put in the code.

It entirely depends on where the variable should be visible/where it has to be used.

If you will only use this variable in that single line, declare it in the line. Do you need it in another function? Declare it above those functions.

 

This is quite a broad answer and not very precise, because it really depends on what you're going to do with the variable.

As for now, it's probably easiest to just declare it in the same line as you are using it now, so basically just replace the line with the other line:

 

Original line:

9 minutes ago, minibois said:

dbOpenHelper = new DBOpenHelper(context);

 

replaced line:

9 minutes ago, minibois said:

DBOpenHelper dbOpenHelper = new DBOpenHelper(context);

 

 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, minibois said:

It entirely depends on where the variable should be visible/where it has to be used.

If you will only use this variable in that single line, declare it in the line. Do you need it in another function? Declare it above those functions.

 

This is quite a broad answer and not very precise, because it really depends on what you're going to do with the variable.

As for now, it's probably easiest to just declare it in the same line as you are using it now, so basically just replace the line with the other line:

 

Original line:

replaced line:

 

thank you ill try this out first and see the result

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×