在TextView

时间:2018-04-04 10:04:46

标签: android textview android-database

我创建了一个存储所有正确值的database。我需要将row中存储的每个database显示在一个TextView的新行中。

当前输出

Current Output

添加到database后,它会添加和updates当前值,而不是转到新行。

必需输出

Required Output

database

中新行上显示的TextView中的每一行

将数据插入数据库

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);
    }
}

执行

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();
            }
        }
    });
}

显示数据

@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)));

}

1 个答案:

答案 0 :(得分:0)

我假设你想在单独的行中显示ArrayList的每个项目。 试试这个,希望得到这个帮助。

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");
        }