vendredi 29 janvier 2016

How to get data from database on click of an event?

I am generating an events dynamically. These events data I am storing in sqlite database. Now I want to retrieve data of the clicked event. I tried to retrieve data but always getting 0th id data.

Generating events function :

  private void createEvent(LayoutInflater inflater, ViewGroup dayplanView, int fromMinutes, int toMinutes, String title) {
    final View eventView = inflater.inflate(R.layout.event_view, dayplanView, false);
    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) eventView.getLayoutParams();

    RelativeLayout container = (RelativeLayout) eventView.findViewById(R.id.container);
    TextView tvTitle = (TextView) eventView.findViewById(R.id.textViewTitle);


    if (tvTitle.getParent() != null)
        ((ViewGroup) tvTitle.getParent()).removeView(tvTitle);
    tvTitle.setText(title);

    int distance = (toMinutes - fromMinutes);

    layoutParams.topMargin = dpToPixels(fromMinutes + 9);
    layoutParams.height = dpToPixels(distance);

    eventView.setLayoutParams(layoutParams);
    dayplanView.addView(eventView);
    container.addView(tvTitle);

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

            i = new Intent(getActivity(),AddEventActivity.class);
            startActivity(i);

        }
    });

}

This is my attempt to get data:

db = new EventTableHelper(getApplication());
    eventData = new EventData();
 if(editMode)//true
    {
        eventData = events.get(id);
      Toast.makeText(getApplicationContext(),String.valueOf(id),Toast.LENGTH_LONG).show();

            title.setText(eventData.getTitle());
            eventTitle = title.getText().toString();

        db.updateEvent(eventData);

        Toast.makeText(getApplicationContext(),"Edit mode",Toast.LENGTH_LONG).show();
        Log.i("Log","save mode");
    }

I have EventTableHelper in that i have created functions to get event,update and delete events.

public class EventTableHelper extends SQLiteOpenHelper {


    private static final String TABLE = "event";
    private static final String KEY_ID = "id";
    private static final String KEY_TITLE = "title";
    private static final String KEY_FROM_DATE = "datefrom";
    private static final String KEY_TO_DATE = "dateto";
    private static final String KEY_LOCATION = "location";
    private static final String KEY_DAY_OF_WEEK = "dayofweek";


    public EventTableHelper(Context context) {
        super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }

    public void createTable(SQLiteDatabase db){
        String CREATE_EVENTS_TABLE = "CREATE TABLE " + TABLE+ "("
                + KEY_ID + " INTEGER PRIMARY KEY,"
                + KEY_TITLE + " TEXT,"
                + KEY_FROM_DATE + " DATE,"
                + KEY_TO_DATE + " DATE,"
                + KEY_DAY_OF_WEEK + " TEXT "
                + KEY_LOCATION + " TEXT" +  ")";
        db.execSQL(CREATE_EVENTS_TABLE);
    }
    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS " + TABLE);

       // createTable(db);
       // onCreate(db);
    }

    public void addEvent(EventData event) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_TITLE,event.getTitle());
        values.put(KEY_FROM_DATE, event.getFromDate());
        values.put(KEY_TO_DATE,event.getToDate());
        values.put(KEY_DAY_OF_WEEK,event.getDayOfWeek());
        values.put(KEY_LOCATION,event.getLocation());

        db.insert(TABLE, null, values);

        db.close();
    }

   EventData getEvent(int id) {

        SQLiteDatabase db = this.getReadableDatabase();
        EventData eventData = new EventData();
        Cursor cursor = db.query(TABLE, new String[]{KEY_ID,
                        KEY_TITLE, KEY_FROM_DATE, KEY_TO_DATE,KEY_DAY_OF_WEEK, KEY_LOCATION}, KEY_ID + "=?",
                new String[]{String.valueOf(id)}, null, null, null, null);

       if( cursor != null && cursor.moveToFirst() ) {
          eventData = new EventData(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),
                   cursor.getString(3), cursor.getString(4), cursor.getString(5));

       }
       return eventData;
    }


    public List<EventData> getAllEvents() {
        List<EventData> conList = new ArrayList<EventData>();

        String selectQuery = "SELECT  * FROM " + TABLE;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {

                EventData event = new EventData();

                event.setId(Integer.parseInt(cursor.getString(0)));
                event.setTitle(cursor.getString(1));
                event.setFromDate(cursor.getString(2));
                event.setToDate(cursor.getString(3));
                event.setLocation(cursor.getString(4));

                conList.add(event);
            } while (cursor.moveToNext());
        }

        return conList;
    }

}

I want to show data and update data if changes made.

Whats going wrong?

Aucun commentaire:

Enregistrer un commentaire