日历对话框

时间:2017-02-23 01:16:45

标签: java android

有人可以告诉我我做错了什么并且它给了我这个错误错误的第一个参数类型。发现:' android.widget.EditText',必需:' java.lang.String'

in boolean for etHours

private void addData() {
        String hours = etHours.getText().toString();
        //spinnerSelectedItem should have a value

        if (!etHours.equals("") && !spinnerSelectedItem.equals("")) {
            boolean insertData = mylogsDB.addData(etHours, spinnerSelectedItem);
            if (insertData) {
                Toast.makeText(AddActivity.this, "Η αποθήκευση ολοκληρώθηκε επιτυχώς.", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(AddActivity.this, "Ώχ,Κάτι πήγε στραβά.", Toast.LENGTH_LONG).show();
            }
        } else {
            //do something
        }   

package com.example.soultaker.multicolumn;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.Calendar;
import java.util.*;
import static com.example.soultaker.multicolumn.Constants.COL0;
import static com.example.soultaker.multicolumn.Constants.COL2;
import static com.example.soultaker.multicolumn.Constants.COL3;


public class AddActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private DatabaseHelper mylogsDB;
    private Button btn_save, btn, btn_edit;
    private String spinnerSelectedItem;
    int year_x, month_x, day_x;
    static final int DIALOG_ID = 0;
    private EditText etHours;
    private EditText etID;
    private Spinner etShift;
    private ArrayList<HashMap<String, String>> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        etHours = (EditText) findViewById(R.id.editText);
        etShift = (Spinner) findViewById(R.id.spinner);
        etID = (EditText) findViewById(R.id.etID);
        btn = (Button) findViewById(R.id.btn_choose_date);
        btn_save = (Button) findViewById(R.id.btn_save);
        btn_edit = (Button) findViewById(R.id.btn_edit);
        mylogsDB = new DatabaseHelper(this);

        ListView listView = (ListView) findViewById(R.id.listView1);

        final Calendar cal = Calendar.getInstance();
        year_x = cal.get(Calendar.YEAR);
        month_x = cal.get(Calendar.MONTH);
        day_x = cal.get(Calendar.DAY_OF_MONTH);
        addData();
        UpdateData();
        saveData();
        showDialogOnButtonClick();

        //populate an ArrayList<String> from the database and then view it
        ArrayList<String> theList = new ArrayList<>();
        Cursor data = mylogsDB.showData();
        if(data.getCount() == 0){
            Toast.makeText(this, "Δεν υπάρχουν καταχωρίσεις",Toast.LENGTH_LONG).show();
        }else{
            while(data.moveToNext()){
                theList.add(data.getString(1));
                ListAdapter listAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,theList);
                listView.setAdapter(listAdapter);
            }
        }

    }



    public void showDialogOnButtonClick() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    protected Dialog onCreateDialog(int id) {

        if (id == DIALOG_ID)
            return new DatePickerDialog(this, dpickerListener, year_x, month_x, day_x);
        return null;
    }

    private DatePickerDialog.OnDateSetListener dpickerListener
            = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            year_x = year;
            month_x = monthOfYear + 1;
            day_x = dayOfMonth;
            Toast.makeText(AddActivity.this, day_x + "." + month_x + "." + year_x, Toast.LENGTH_LONG).show();

        }
    };

    public void saveData() {
        btn_save.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {
                addData();
            }
        }
        );

        // Spinner click listener
        etShift.setOnItemSelectedListener(this);

        // Spinner Drop down elements
        List<String> categories = new ArrayList<>();
        categories.add("Πρωί");
        categories.add("Μεσημέρι");
        categories.add("Νύχτα");

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, categories);

        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        etShift.setAdapter(dataAdapter);
    }

    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // On selecting a spinner item
        spinnerSelectedItem = parent.getItemAtPosition(position).toString();

        // Showing selected spinner item
        Toast.makeText(parent.getContext(), "Έχετε επιλέξει: " + spinnerSelectedItem, Toast.LENGTH_SHORT).show();
    }


    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }

    private void addData() {
        String hours = etHours.getText().toString();
        //spinnerSelectedItem should have a value

        if (!etHours.equals("") && !spinnerSelectedItem.equals("")) {
            boolean insertData = mylogsDB.addData(etHours, spinnerSelectedItem);
            if (insertData) {
                Toast.makeText(AddActivity.this, "Η αποθήκευση ολοκληρώθηκε επιτυχώς.", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(AddActivity.this, "Ώχ,Κάτι πήγε στραβά.", Toast.LENGTH_LONG).show();
            }
        } else {
            //do something
        }
        ListView listView = (ListView) findViewById(R.id.listView1);

        list = new ArrayList<HashMap<String, String>>();

        HashMap<String, String> temp = new HashMap<String, String>();
        temp.put(COL0, "");

        temp.put(COL2, "");
        temp.put(COL3, "");
        list.add(temp);

        HashMap<String, String> temp2 = new HashMap<String, String>();
        temp2.put(COL0, "");

        temp2.put(COL2, "");
        temp2.put(COL3, "");
        list.add(temp2);

        HashMap<String, String> temp3 = new HashMap<String, String>();
        temp3.put(COL0, "");

        temp3.put(COL2, "");
        temp3.put(COL3, "");
        list.add(temp3);

        ListViewAdapters adapter = new ListViewAdapters(this, list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
                int pos = position + 1;
                Toast.makeText(AddActivity.this, Integer.toString(pos) + " Clicked", Toast.LENGTH_SHORT).show();
            }

        });
    }

    private void UpdateData(){
        btn_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int temp = etID.getText().toString().length();
                if (temp > 0) {
                    boolean update = mylogsDB.updateData(etID.getText().toString(),
                            etHours.getText().toString(), etShift.getSelectedItem().toString());
                    if (update) {
                        Toast.makeText(AddActivity.this, "Successfully Updated Data!", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(AddActivity.this, "Something Went Wrong :(.", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(AddActivity.this, "You Must Enter An ID to Update :(.", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

boolean insertData = mylogsDB.addData(etHours, spinnerSelectedItem);

这条线是否出现问题?
尝试将其更改为:

boolean insertData = false;
if(etHours != null && !etHours.isEmpty())
    insertData = mylogsDB.addData(etHours.getText().toString(), spinnerSelectedItem);


如果NullPointerException在访问etHours时不断出现,则最好检查一下您的XML,或者至少记录etHours

答案 1 :(得分:0)

试试这个,

     private void addData() {
        String hours = etHours.getText().toString();
        //spinnerSelectedItem should have a value

        if (!hours.equals("") && !spinnerSelectedItem.equals("")) {
            boolean insertData = mylogsDB.addData(hours, spinnerSelectedItem);
            if (insertData) {
                Toast.makeText(AddActivity.this, "Η αποθήκευση ολοκληρώθηκε επιτυχώς.", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(AddActivity.this, "Ώχ,Κάτι πήγε στραβά.", Toast.LENGTH_LONG).show();
            }
        } else {
            //do something
        }