I'm working o a calendar app with events and I save them in MyDatabaseHelpe, the events are shown in a listview by day, my problem is that in onitemclicked, I want the data of the event to be shown in an AlertDialog, I use a CursorAdapter class and when i am trying to show the data, the data that are position 0, arent shown and show the data that are in position 1.
I've tried many approches of the way the position could fit in function, and i search many StackOverFlows(and more sites) subjects to find a solution.
**The delete row function works perfectly without problem.
Any ideas or approches would be helpuful!!
`
`private void DialogClickedItemAndDelete() {
monthListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View view1 = getLayoutInflater().inflate(R.layout.show_event_from_listview, null);
Object listItem = monthListView.getItemAtPosition(position).toString();
EventCursorAdapter EC = new EventCursorAdapter(MainActivity.this, myDB.readAllData());
Cursor cursor = EC.getCursor();
cursor.moveToPosition(position);
View view2 = EC.getView(position,view1,parent);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(view2).
setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String id_row = hourAdapter.getItem(position).getEvents().get(0).getId();
myDB.deleteOneRow(id_row);
AllEventsList.reloadActivity(MainActivity.this);
}
}).setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AllEventsList.reloadActivity(MainActivity.this);
}
});
builder.show();
}
});
} `
``
``
#CursorAdapter Class
package com.example.calendarcapital;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
public class EventCursorAdapter extends CursorAdapter {
public EventCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
c.moveToFirst();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.show_event_from_listview, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView id_lv_tv = (TextView) view.findViewById(R.id.id_lv_tv);
TextView title_lv_tv = (TextView) view.findViewById(R.id.title_lv_tv);
TextView comment_lv_tv = (TextView) view.findViewById(R.id.comment_lv_tv);
TextView date_lv_tv = (TextView) view.findViewById(R.id.date_lv_tv);
TextView time_lv_tv = (TextView) view.findViewById(R.id.time_lv_tv);
String id_row = cursor.getString(cursor.getColumnIndexOrThrow("_id"));
String title = cursor.getString(cursor.getColumnIndexOrThrow("event_title"));
String comment = cursor.getString(cursor.getColumnIndexOrThrow("event_comment"));
String date = cursor.getString(cursor.getColumnIndexOrThrow("event_date"));
String time = cursor.getString(cursor.getColumnIndexOrThrow("event_time"));
id_lv_tv.setText(id_row);
title_lv_tv.setText(title);
comment_lv_tv.setText(comment);
date_lv_tv.setText(date);
time_lv_tv.setText(time);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
}
#MyDatabaseHelper Class
public class MyDatabaseHelper extends SQLiteOpenHelper {
private Context context;
private static final String DATABASE_NAME = "CalendarCapital.db";
private static final int DATABASE_VERSION =1;
private static final String TABLE_NAME ="my_events_db";
public static final String COLUMN_ID ="_id";
private static final String COLUMN_TITLE = "event_title";
private static final String COLUMN_COMMENT = "event_comment";
private static final String COLUMN_DATE = "event_date";
private static final String COLUMN_TIME = "event_time";
MyDatabaseHelper(@Nullable Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db)
{
String query = "CREATE TABLE " + TABLE_NAME +
" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_TITLE + " TEXT, " +
COLUMN_COMMENT + " TEXT, " +
COLUMN_DATE + " TEXT, " +
COLUMN_TIME + " TEXT);";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
void addEvent(String title, String comment, LocalDate date, LocalTime time)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE, title);
cv.put(COLUMN_COMMENT, comment);
cv.put(COLUMN_DATE, String.valueOf(date));
cv.put(COLUMN_TIME, String.valueOf(time));
long result = db.insert(TABLE_NAME, null, cv);
if (result== -1)
{
Toast.makeText(context, "Data Failed", Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(context, "Data Added Successfully", Toast.LENGTH_SHORT).show();
}
}
Cursor readAllData(){
String query = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
if (db != null)
{
cursor = db.rawQuery(query, null);
}
return cursor;
}
void updateData(String row_id, String title, String comments, String date, String time)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_TITLE,title);
cv.put(COLUMN_COMMENT,comments);
cv.put(COLUMN_DATE, String.valueOf(date));
cv.put(COLUMN_TIME, String.valueOf(time));
long result = db.update(TABLE_NAME,cv,"_id=?",new String[]{row_id});
if (result == -1)
{
Toast.makeText(context, "Failed to Update", Toast.LENGTH_SHORT).show();
}else
{
Toast.makeText(context, "Successfully Update", Toast.LENGTH_SHORT).show();
}
}
void deleteOneRow(String row_id)
{
SQLiteDatabase db = this.getWritableDatabase();
long result = db.delete(TABLE_NAME, "_id=?", new String[]{row_id});
if (result == -1)
{
Toast.makeText(context, "Failed to Delete.", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(context, "Deleted Successfully.", Toast.LENGTH_SHORT).show();
}
}
void deleteAllData()
{
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME);
}
}
Another approch I tried is this
View view2 = EC.getView(position,view1,parent);
View view3 = monthListView.getChildAt(position - monthListView.getFirstVisiblePosition());
View view4 = EC.newView(EC.mContext,EC.mCursor,parent);
View view5=hourAdapter.getView(position,view1,parent);
UPDATE: 27/12/2022
I noticed that my problem is maybe because i sort my list.
private void setMonthAdapter() { hourAdapter = new HourAdapter(getApplicationContext(), AllEventsList.hourEventListFromDatabase(getApplicationContext(), myDB));
hourAdapter.sort((o1, o2) -> o1.events.get(0).getDate().compareTo(o2.events.get(0).getDate()));
hourAdapter.sort((o1, o2) -> o1.events.get(0).getTime().compareTo(o2.events.get(0).getTime()));
monthListView.setAdapter(hourAdapter);
}
I can retrieve the values but with the wrong order, in my listview my events are sorted by time and date, when i click item in position 0 it show me the item in position 1 and versa.
**SOLUTION: **
The problem was that after sorting list i didn't called notifyDataChanged();
Furthermore I made in my CursorAdapter Class a method to retrieve fields via cursor.
So in cursor adapter i made this:
public View setAllFields(View view,String id, String title, String comment, String date, String time)
{
TextView id_lv_tv = (TextView) view.findViewById(R.id.id_lv_tv);
TextView title_lv_tv = (TextView) view.findViewById(R.id.title_lv_tv);
TextView comment_lv_tv = (TextView) view.findViewById(R.id.comment_lv_tv);
TextView date_lv_tv = (TextView) view.findViewById(R.id.date_lv_tv);
TextView time_lv_tv = (TextView) view.findViewById(R.id.time_lv_tv);
id_lv_tv.setText(id);
title_lv_tv.setText(title);
comment_lv_tv.setText(comment);
date_lv_tv.setText(date);
time_lv_tv.setText(time);
return view;
}
And my main onitemclick now is like this:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { View view1 = getLayoutInflater().inflate(R.layout.show_event_from_listview, null); // private ArrayList MlaDats = new ArrayList(); HourEvent myEvent = (HourEvent) monthListView.getAdapter().getItem(position);
String myEventId= myEvent.getEvents().get(0).getId();
String myTitle = myEvent.getEvents().get(0).getName();
String myComment = myEvent.getEvents().get(0).getComment();
String myDate = String.valueOf(myEvent.getEvents().get(0).getDate());
String myTime = String.valueOf(myEvent.getEvents().get(0).getTime());
View viewFinal;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
viewFinal = CA.setAllFields(view1,myEventId,myTitle,myComment,myDate,myTime);
// viewFinal = SD.getView(position, view1, parent);
builder.setView(viewFinal).
CLOSED
I'm working o a calendar app with events and I save them in MyDatabaseHelpe, the events are shown in a listview by day, my problem is that in onitemclicked, I want the data of the event to be shown in an AlertDialog, I use a CursorAdapter class and when i am trying to show the data, the data that are in position 0, arent shown and show the data that are in position 1. cursor adapter getview problem at position 0
I've tried many approches of the way the position could fit in function, and i search many StackOverFlows(and more sites) subjects to find a solution.
**The delete row function works perfectly without problem.
Any ideas or approches would be helpuful!!
**SOLUTION: **
The problem was that after sorting list i didn't called notifyDataChanged();
Furthermore I made in my CursorAdapter Class a method to retrieve fields via cursor.
So in cursor adapter i made this:
public View setAllFields(View view,String id, String title, String comment, String date, String time)
{
TextView id_lv_tv = (TextView) view.findViewById(R.id.id_lv_tv);
TextView title_lv_tv = (TextView) view.findViewById(R.id.title_lv_tv);
TextView comment_lv_tv = (TextView) view.findViewById(R.id.comment_lv_tv);
TextView date_lv_tv = (TextView) view.findViewById(R.id.date_lv_tv);
TextView time_lv_tv = (TextView) view.findViewById(R.id.time_lv_tv);
id_lv_tv.setText(id);
title_lv_tv.setText(title);
comment_lv_tv.setText(comment);
date_lv_tv.setText(date);
time_lv_tv.setText(time);
return view;
}
And my main onitemclick now is like this:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { View view1 = getLayoutInflater().inflate(R.layout.show_event_from_listview, null); // private ArrayList MlaDats = new ArrayList(); HourEvent myEvent = (HourEvent) monthListView.getAdapter().getItem(position);
String myEventId= myEvent.getEvents().get(0).getId();
String myTitle = myEvent.getEvents().get(0).getName();
String myComment = myEvent.getEvents().get(0).getComment();
String myDate = String.valueOf(myEvent.getEvents().get(0).getDate());
String myTime = String.valueOf(myEvent.getEvents().get(0).getTime());
View viewFinal;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
viewFinal = CA.setAllFields(view1,myEventId,myTitle,myComment,myDate,myTime);
// viewFinal = SD.getView(position, view1, parent);
builder.setView(viewFinal).