-1

I have created a database that stores all the correct values. I need for each row stored in the database to be displayed on a new line in one TextView.

Current Output

Current Output

After adding to database it adds on and updates current values instead of going to new line.

Required Output

Required Output

Each row from the database displayed on a new line in TextView

Insert data to database

public static void InsertOrUpdateRatingPoints(Context context, int point, SelfToSelfActivity.Rating activity) {
    DBHelper dbHelper = new DBHelper(context);
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    String[] projection = {ID, TIME, TYPE,};
    String where = TYPE + " = ?";
    String[] whereArgs = {String.valueOf(activity)};
    String orderBy = TIME + " DESC";

    Cursor cursor = db.query(TABLE_NAME, projection, where, whereArgs, null, null, orderBy);
    boolean sameDay = false;

    Date currentTime = Calendar.getInstance().getTime();
    int StoredPoint = 0;
    long lastStored = 0;

    if (cursor != null) {
        if (cursor.moveToFirst()) {
            lastStored = cursor.getLong(cursor.getColumnIndex(TIME));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            sameDay = (sdf.format(new Date(lastStored))).equals(sdf.format(currentTime));
            if (sameDay) StoredPoint = cursor.getInt(cursor.getColumnIndex(POINT));
        }
        cursor.close();

    }


    ContentValues cv = new ContentValues();
    cv.put(POINT, point + StoredPoint);

    if (sameDay) {
        db.update(TABLE_NAME, cv, TIME + " = ?", new String[]{String.valueOf(lastStored)});
    } else {
        cv.put(TYPE, activity.ordinal());
        cv.put(TIME, currentTime.getTime());
        cv.put(POINT, point);
        db.insert(TABLE_NAME, null, cv);
    }
}

Execute

public void execute() {
    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            Cursor c = TrackerDb.getStoredItems(getApplicationContext());
            if (c != null) {
                if (c.moveToFirst()) {
                    WorkoutDetails details = null;
                    do {
                        WorkoutDetails temp = getWorkoutFromCursor(c);
                        if (details == null) {
                            details = temp;
                            continue;
                        }
                        if (isSameDay(details.getWorkoutDate(), temp.getWorkoutDate())) {
                            if (DBG) Log.d(LOG_TAG, "isSameDay().. true");
                            details.add(temp);
                        } else {
                            mWorkoutDetailsList.add(details);
                            details = temp;
                        }
                    } while (c.moveToNext());
                    if (details != null) mWorkoutDetailsList.add(details);

                    if (DBG)
                        Log.d(LOG_TAG, "AsyncTask: list size " + mWorkoutDetailsList.size());

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mWorkoutsAdapter.updateList(mWorkoutDetailsList);
                            //AVG_THIRTY.setText(String.valueOf(EmotionListAdapter.thirtyday));
                            //Today_Score.setText(String.valueOf(EmotionListAdapter.day));
                        }
                    });
                }


                c.close();
            }
        }
    });
}

Display Data

@Override
public void onBindViewHolder(RatingListViewHolder holder, int position)
{

    WorkoutDetails details = mWorkoutsList.get(position);

    holder.textSTS.setText(String.valueOf(totalSTS));
    holder.textLoss.setText(String.valueOf(details.getPoints(SelfToSelfActivity.Rating.LOSS)));
    holder.textRateLoss.setText(String.valueOf(details.getPoints(SelfToSelfActivity.Rating.RATELOSS)));

}
Community
  • 1
  • 1

1 Answers1

0

I assume you want to display every item of ArrayList in separate lines. Try this, hope this help.

TextView conciergeServicesTv = (TextView) findViewById(R.id.activity_get_quote_final_concierge_services_tv);
        if (arrayListConciergeServices.size() != 0) { //ArrayList you are receiving\\
            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < arrayListConciergeServices.size(); i++) {
                if (i == arrayListConciergeServices.size() - 1) {
                    stringBuilder.append(arrayListConciergeServices.get(i));
                } else {
                    stringBuilder.append(arrayListConciergeServices.get(i)).append("\n");
                }
            }
            conciergeServicesTv.setText(stringBuilder);
        } else {
            conciergeServicesTv.setText("No concierge services selected");
        }
Rahul Singh Chandrabhan
  • 2,531
  • 5
  • 22
  • 33