我通过将AlertDialog与cardView一起使用来收集数据,因此用户可以输入其数据。然后,我将数据传递到数据库。之后,我使用RecyclerView Adapter并在RecyclerView上显示它们
我想使用RecyclerView Adapter显示我的数据,然后在RecyclerView上显示它们,但是当我运行该应用程序时,它崩溃了。
它表明我的问题是我没有EventListAdapter,但是我有并且检查了几次。请帮助我,非常感谢。
主要活动
public class eventListPage_3 extends AppCompatActivity {
private RecyclerView recyclerView;
private EventListAdapter adapter;
private List<Event> eventList;
private List<Event> eventItems;
private DatabaseHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_list_3);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
db = new DatabaseHandler(this);
recyclerView = findViewById(R.id.eventListView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
eventList = new ArrayList<>();
eventItems = new ArrayList<>();
// get all events
eventList = db.GetAllEvents();
for (Event e : eventList){
Event event = new Event();
event.setId(e.getId());
event.setTime(e.getTime());
event.setDescription(e.getDescription());
eventItems.add(event);
}
adapter = new EventListAdapter(this, eventItems);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}}
适配器
public class EventListAdapter extends RecyclerView.Adapter<EventListAdapter.ViewHolder> {
private Context context;
private List<Event> eventList;
public EventListAdapter(Context context, List<Event> eventList) {
this.context = context;
this.eventList = eventList;
}
@NonNull
@Override
public EventListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, viewGroup, false);
return new ViewHolder(view, context);
}
@Override
public void onBindViewHolder(@NonNull EventListAdapter.ViewHolder viewHolder, int i) {
Event event = eventList.get(i);
viewHolder.time.setText(event.getTime());
viewHolder.description.setText(event.getDescription());
}
@Override
public int getItemCount() {
return eventList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView time;
public TextView description;
public Button edit;
public Button delete;
public int id;
public ViewHolder(@NonNull View itemView, Context ctx) {
super(itemView);
context = ctx;
time = itemView.findViewById(R.id.time);
description = itemView.findViewById(R.id.description);
edit = itemView.findViewById(R.id.editedButton);
delete = itemView.findViewById(R.id.deleteButton);
edit.setOnClickListener(this);
delete.setOnClickListener(this);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.editedButton:
break;
case R.id.deleteButton:
break;
}
}
}}
我的数据库类
public class DatabaseHandler extends SQLiteOpenHelper {
private Context context;
public DatabaseHandler(Context context) {
super(context, Database_Constants.DB_NAME, null, Database_Constants.DB_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_EVENT_TABLE = "CREATE TABLE " + Database_Constants.TABLE_NAME + "("
+ Database_Constants.KEY_ID + " INTEGER PRIMARY KEY," + Database_Constants.KEY_EVENT_TIME
+ " TEXT," + Database_Constants.KET_EVENT_DESCRIPTION + " TEXT);";
db.execSQL(CREATE_EVENT_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + Database_Constants.TABLE_NAME);
onCreate(db);
}
//Add
public void AddEvent(Event event){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Database_Constants.KEY_EVENT_TIME, event.getTime());
values.put(Database_Constants.KET_EVENT_DESCRIPTION, event.getDescription());
db.insert(Database_Constants.TABLE_NAME, null, values);
}
//Get All Events
public List<Event> GetAllEvents(){
SQLiteDatabase db = this.getReadableDatabase();
List<Event> events = new ArrayList<>();
Cursor cursor = db.query(Database_Constants.TABLE_NAME, new String[]{Database_Constants.KEY_ID,
Database_Constants.KEY_EVENT_TIME, Database_Constants.KET_EVENT_DESCRIPTION}, null,
null,null, null, Database_Constants.KEY_ID + " DESC");
if (cursor.moveToFirst()){
do {
Event event = new Event();
event.setId(Integer.parseInt(cursor.getString(cursor.getColumnIndex(Database_Constants.KEY_ID))));
event.setTime(cursor.getString(cursor.getColumnIndex(Database_Constants.KEY_EVENT_TIME)));
event.setDescription(cursor.getString(cursor.getColumnIndex(Database_Constants.KET_EVENT_DESCRIPTION)));
events.add(event);
}while (cursor.moveToNext());
}
return events;
}
//Update
public int UpdateEvents(Event event){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Database_Constants.KEY_EVENT_TIME, event.getTime());
values.put(Database_Constants.KET_EVENT_DESCRIPTION, event.getDescription());
return db.update(Database_Constants.TABLE_NAME, values, Database_Constants.KEY_ID + "=?",
new String[]{String.valueOf(event.getId())});
}
//Delete
public void DeleteEvents(int id){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(Database_Constants.TABLE_NAME, Database_Constants.KEY_ID + "=?",
new String[]{String.valueOf(id)});
db.close();}}
具有AlertDialog的我的活动可以收集我的数据
public class MainPage_2 extends AppCompatActivity {
private AlertDialog.Builder dialogBuilder;
private AlertDialog alertDialog;
private EditText time;
private EditText description;
private Button saveButton;
private DatabaseHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page_2);
db = new DatabaseHandler(this);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
create_eventList();
}
});
}
private void create_eventList(){
dialogBuilder = new AlertDialog.Builder(this);
View view = getLayoutInflater().inflate(R.layout.event_create_3, null);
time = view.findViewById(R.id.time);
description = view.findViewById(R.id.description);
saveButton = view.findViewById(R.id.save_button);
dialogBuilder.setView(view);
alertDialog = dialogBuilder.create();
alertDialog.show();
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveEventToDatabase(view);
}
});
}
private void saveEventToDatabase(View v){
Event event = new Event();
String newTime = time.getText().toString();
String newDescription = description.getText().toString();
event.setTime(newTime);
event.setDescription(newDescription);
db.AddEvent(event);
Snackbar.make(v, "event Saved!", Snackbar.LENGTH_LONG).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
alertDialog.dismiss();
//start a new activity
startActivity(new Intent(MainPage_2.this, eventListPage_3.class));
}
}, 1000); //1 sec
}}
logcat屏幕截图: