Ich habe an einer App, wo die wichtigsten Activity
führt zu CalendarActivity
, die eine Schaltfläche führt zu einem anderen Activity
, wo der Benutzer ein Ereignis erstellt hat. Sobald das Ereignis erstellt wurde, wird der Benutzer zu CalendarActivity
zurückgebracht, und ein vorher leeres TextView
zeigt das Ereignis an. Der Code, den ich verwendet habe, scheint so zu sein, als ob er funktionieren sollte, und ist fast wortwörtlich von einem Online-Tutorial. Ich recherchierte die Kommentare des Videos und des Videomachers Blog, und viele andere scheinen zu sagen, es funktioniert gut. Ich habe immer und immer wieder überprüft, und ich glaube, dass es grammatikalisch richtig ist, usw., aber ich kann es nicht bekommen, um das Ereignis in die TextView
zu laden. Irgendwelche Hinweise würden sogar helfen. Ich würde Sie bitten, es in der Nähe von grundlegendem Englisch zu halten, ich bin gerade dabei, mich zu programmieren, und verwende diese App als eine Lernerfahrung.SQLite Datenbank Problem in TextView
Danke!
CalendarActivity
:
package com.bm.sn.sbeta;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class CalendarActivity extends AppCompatActivity {
CalendarView calendar;
Button createEvent;
public static String createEventDate;
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);
Cursor result = db.getAllData();
if (result.getCount() == 0) {
noEventToday();
}else{
TextView eventList = (TextView)findViewById(R.id.eventList);
StringBuffer stringBuffer = new StringBuffer();
while (result.moveToNext()) {
stringBuffer.append("eventDat : "+result.getString(0)+"\n");
stringBuffer.append("timeHour : "+result.getString(1)+"\n");
stringBuffer.append("timeMinue : "+result.getString(2)+"\n");
stringBuffer.append("event : "+result.getString(3)+"\n");
stringBuffer.append("location : "+result.getString(4)+"\n");
stringBuffer.append("crew : "+result.getString(5)+"\n\n");
eventList.setText(stringBuffer);
}
}
calendar = (CalendarView)findViewById(R.id.calendar);
calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener(){
@Override
public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth){
createEventDate = (month+"."+dayOfMonth+"."+year);
createEvent.setText("Create Event for "+createEventDate);
}
});
createEvent = (Button)findViewById(R.id.eventCreateButton);
createEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent toEventCreateActivity = new Intent(CalendarActivity.this, EventCreateActivity.class);
startActivity(toEventCreateActivity);
}
});
}
/*public void fillEventList(){
}
public void noEventToday(){
TextView eventList = (TextView)findViewById(R.id.eventList);
eventList.setText("Nothing scheduled for today.");
}*/
}
EventCreateActivity
:
package com.bm.sn.sbeta;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class EventCreateActivity extends AppCompatActivity {
DatabaseHelper db;
String textViewText = CalendarActivity.createEventDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_create);
db = new DatabaseHelper(this);
final TextView titleTextView = (TextView)findViewById(R.id.titleTextView);
titleTextView.setText("Create event for "+textViewText);
final TimePicker timePicker = (TimePicker)findViewById(R.id.timePicker);
final EditText entryEvent = (EditText)findViewById(R.id.entryEvent);
final EditText entryLocation = (EditText)findViewById(R.id.entryLocation);
final EditText entryCrew = (EditText)findViewById(R.id.entryCrew);
Button createEventButton = (Button)findViewById(R.id.saveEvent);
createEventButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.insertData(
titleTextView.toString(),
timePicker.getCurrentHour().toString(),
timePicker.getCurrentMinute().toString(),
entryEvent.getText().toString(),
entryLocation.getText().toString(),
entryCrew.getText().toString()
);
Toast.makeText(EventCreateActivity.this, "I'll keep that in mind.", Toast.LENGTH_LONG).show();
Intent toCalendarActivity = new Intent(EventCreateActivity.this, CalendarActivity.class);
startActivity(toCalendarActivity);
}
});
}
}
DatabaseHelper
:
package com.bm.sn.sbeta;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "SavitaCalendar.db";
public static final String TABLE_NAME = "CalendarEvents";
public static final String col_0 = "ID";
public static final String col_1 = "eventDate" ;
public static final String col_2 = "timeHour";
public static final String col_3 = "timeMinute";
public static final String col_4 = "event";
public static final String col_5 = "location";
public static final String col_6 = "crew";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+TABLE_NAME+ " (ID INTEGER PRIMARY KEY AUTOINCREMENT,EVENTDATE TEXT,TIMEHOUR TEXT,TIMEMINUTE TEXT, EVENT TEXT, LOCATION TEXT, CREW TEXT); ");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public void insertData (String eventDate, String timeHour, String timeMinute, String event, String location, String crew){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(col_1, eventDate);
contentValues.put(col_2, timeHour);
contentValues.put(col_3, timeMinute);
contentValues.put(col_4, event);
contentValues.put(col_5, location);
contentValues.put(col_6, crew);
db.insert(TABLE_NAME, null, contentValues);
db.close();
}
public Cursor getAllData(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("select * from "+TABLE_NAME, null);
return result;
}
}
'tools "EventCreateActivity.": context' nichts tut hier spezielle –
public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, 1); }
– nuccioOk .. Das ist ein Datenbankcode ... Hat nichts mit dem XML-Attribut –