0

我已经检查了有关预填充数据库的所有答案,但是没有一个显示如何在数据库中插入 REFERENCED TEXT VIEWS 或在 SQLiteOpenHelper 的 onCreate() 中手动输入练习名称,以便文本视图出现在数据库的开头。我遇到了一个在 onCreate() 中使用 ContentValues 的地方,但这对我不起作用。此外,我还尝试在 onCreate 中手动显示练习名称(参见下面 DatabaseManager 中的代码),但没有成功。

基本上,我的数据库中有 3 列,KEY_ROWID、KEY_EXERCISENAME 和 KEY_DURATION,其中 ROWID 是自动增量,KEY_DURATION 是用于不同练习的 editText,KEY_EXERCISENAME 是我想要显示包含在我的 XML 布局中的所有练习名称的位置(weekone.xml )。因此,当用户查看数据库时,所有练习名称都已显示在 KEY_EXERCISENAME 下,并且他必须在各自的编辑框中输入这些练习的特定值。

weekone.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <!--
    Begin Relative Layout
    -->
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/originallayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:scrollHorizontally="true"
        android:background="@drawable/appbackground">

        <TextView
        android:id="@+id/about_title"
        android:layout_width="fill_parent"
        android:layout_height="38dp"
        android:background="#00141c"
        android:fontFamily="Hobo Std"
        android:gravity="center"
        android:text="@string/start"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF"
        android:textSize="25sp"
        android:textStyle="bold" />

        <ImageView
            android:id="@+id/backarrow"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_alignBottom="@+id/about_title"
            android:layout_alignParentTop="true"
            android:clickable="true"
            android:src="@drawable/arrow" />

        <ImageView
            android:id="@+id/week4"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="24dp"
            android:clickable="true"
            android:paddingTop="400dp"
            android:src="@drawable/weekfour" />



        <ImageView
            android:id="@+id/week3"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_toLeftOf="@+id/week4"
            android:clickable="true"
            android:paddingTop="400dp"
            android:src="@drawable/weekthree" />

        <ImageView
            android:id="@+id/week2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/week3"
            android:clickable="true"
            android:paddingTop="400dp"
            android:src="@drawable/weektwo" />

        <ImageView
            android:id="@+id/week1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/week2"
            android:clickable="true"
            android:paddingTop="400dp"
            android:src="@drawable/weekone" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:orientation="vertical"
        android:background="#00000000"

        android:paddingTop="40dp"
        android:id="@+id/weekone">

        <!--     Line seperator     -->

        <View
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:id="@+id/separator"
                android:visibility="visible"
                android:background="#00141c"/>

        <TextView
            android:id="@+id/week1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/week1"
            android:textColor="#FFFFFF"
            android:textSize="18sp"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:background="#00141c"/>


                <!-- Begin Scrollable Linear Layout -->

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="325dp" >

       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:scrollHorizontally="true">     

        <ImageView
            android:id="@+id/treadmillimage"
            android:layout_width="80dp"
            android:layout_height="75dp"
            android:paddingTop="10dp"
            android:src="@drawable/treadmill" />

                <!--     Line seperator     -->

        <View
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:id="@+id/separator"
                android:visibility="visible"
                android:background="#00141c"/>


                <!--    End Relative Layout   -->

        <EditText
            android:id="@+id/durOnTreadmill"
            android:layout_width="129dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="32dp"
            android:hint="hh:mm:ss" />

        </RelativeLayout>

        <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:scrollHorizontally="true">


        <TextView                               
            android:id="@+id/treadmilltext"     <----- name of exercise machine in textview
            android:layout_width="91dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="@string/treadmill"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/duration"
            android:layout_width="91dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="44dp"
            android:text="@string/dur"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        </RelativeLayout>

    <!--        Begin Relative Layou        -->

        <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:scrollHorizontally="true">

        <ImageView
            android:id="@+id/stepperimage"
            android:layout_width="80dp"
            android:layout_height="75dp"
            android:paddingTop="10dp"
            android:src="@drawable/stepper" />

            <!--     Line seperator     -->

        <View
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:id="@+id/separator"
                android:visibility="visible"
                android:background="#00141c"/>


             <!--   End Relative Layout  -->

        <EditText
            android:id="@+id/durOnStepper"
            android:layout_width="129dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="32dp"
            android:ems="10"
            android:inputType="number"
            android:hint="hh:mm:ss" />

            <requestFocus />


        </RelativeLayout>

        <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:scrollHorizontally="true">

        <TextView
            android:id="@+id/steppertext"       <------ name of exercise machine in textView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:text="@string/stepper"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/duration"
            android:layout_width="91dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="44dp"
            android:text="@string/dur"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        </RelativeLayout>

    <!--    Begin Relative Layout    -->

        <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:scrollHorizontally="true">

        <ImageView
            android:id="@+id/stationaryrowing"
            android:layout_width="80dp"
            android:layout_height="75dp"
            android:paddingTop="10dp"
            android:src="@drawable/stationaryrowing" />

        <EditText
            android:id="@+id/durOnStationaryRowing"
            android:layout_width="129dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="32dp"
            android:ems="10"
            android:inputType="number" 
            android:hint="hh:mm:ss"/>

        <!--     Line seperator     -->

        <View
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:id="@+id/separator"
                android:visibility="visible"
                android:background="#00141c"/>


    <!--
    End Relative Layout
    -->
        </RelativeLayout>

        <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:scrollHorizontally="true">

        <TextView
            android:id="@+id/stationaryrowingtext"      <------ name of exercise machine in textView
            android:layout_width="108dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:text="@string/stationaryrowing"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/duration"
            android:layout_width="91dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="44dp"
            android:text="@string/dur"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

        </RelativeLayout>

         <!--   Begin Relative Layout   -->
         <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:scrollHorizontally="true">

        <ImageView
            android:id="@+id/exercisebikeimage"
            android:layout_width="80dp"
            android:layout_height="75dp"
            android:paddingTop="10dp"
            android:src="@drawable/excerisebike"/>

        <EditText
            android:id="@+id/durOnExerciseBike"
            android:layout_width="129dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="32dp"
            android:ems="10"
            android:inputType="number" 
            android:hint="hh:mm:ss"/>

            <!--     Line seperator     -->

        <View
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:id="@+id/separator"
                android:visibility="visible"
                android:background="#00141c"/>

        <!--    End Relative Layout  -->

        </RelativeLayout>

         <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:scrollHorizontally="true">

         <TextView
            android:id="@+id/excerisebiketext"      <------ name of exercise machine in textView
            android:layout_width="91dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:text="@string/exercisebike"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

         <TextView
            android:id="@+id/duration"
            android:layout_width="91dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="44dp"
            android:text="@string/dur"
            android:textColor="#FFFFFF"
            android:textSize="15sp" />

         </RelativeLayout>

        <!--    Begin Relative Layout   -->

           <RelativeLayout 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:scrollHorizontally="true">

        <ImageView
            android:id="@+id/ellipticaltrainerimage"
            android:layout_width="80dp"
            android:layout_height="75dp"
            android:paddingTop="10dp"
            android:src="@drawable/ellipticaltrainer"/>

        <EditText
            android:id="@+id/durOnellipticaltrainer"
            android:layout_width="129dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="32dp"
            android:ems="10"
            android:inputType="number" 
            android:hint="hh:mm:ss"/>

            <!--     Line seperator     -->

        <View
                android:layout_width="fill_parent"
                android:layout_height="2dp"
                android:id="@+id/separator"
                android:visibility="visible"
                android:background="#00141c"/>

        <!--        End Relative Layout      -->

    </RelativeLayout>

           <RelativeLayout 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:scrollHorizontally="true">

            <TextView
                android:id="@+id/ellipticaltrainertext"     <------ name of exercise machine in textView
                android:layout_width="91dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="@string/ellipticaltrainer"
                android:textColor="#FFFFFF"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/duration"
                android:layout_width="91dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="44dp"
                android:text="@string/dur"
                android:textColor="#FFFFFF"
                android:textSize="15sp" />

            </RelativeLayout>

           <Button 
               android:id = "@+id/saveButton"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:text = "@string/save"/>

           <Button 
               android:id = "@+id/viewButton"
               android:layout_width = "wrap_content"
               android:layout_height = "wrap_content"
               android:text = "@string/view"/>

        </LinearLayout>

       <!--     End ScrollView   -->

      </ScrollView>

        <!--        End Linear Layout        -->

    </LinearLayout>     

        <!--        End Linear Layout       -->

</RelativeLayout>

数据库管理器.java

package com.example.draft;

import java.sql.SQLException;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseManager 
{

public static final String KEY_ROWID = "_id";
public static final String KEY_EXERCISENAME = "exercisename";
public static final String KEY_DURATION = "duration";

private static final String DATABASE_NAME = "MyDatabase";
private static final String DATABASE_TABLE = "weekOne";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper
{

    public DbHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        // TODO Auto-generated method stub
        db.execSQL
        ("CREATE TABLE " + DATABASE_TABLE + " (" +
            KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            KEY_EXERCISENAME + " EXERCISEBIKE, " +     <----------Exercise name entered manually
            KEY_DURATION + " TEXT NOT NULL);"
        );



    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

}

public DatabaseManager(Context c)
{
    ourContext = c; 
}

public DatabaseManager open() 
{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;    
}

public void close()
{
    ourHelper.close();
}

public long createEntry(String treadmillTimings) {
    // TODO Auto-generated method stub

    ContentValues cv = new ContentValues();
    cv.put(KEY_DURATION, treadmillTimings);
    return ourDatabase.insert(DATABASE_TABLE, null,cv);

}

public String getData() 
{
    // TODO Auto-generated method stub
    String[] columns = new String[]{ KEY_ROWID, KEY_EXERCISENAME, KEY_DURATION};
    Cursor cur = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iRow = cur.getColumnIndex(KEY_ROWID);
    int iExerciseName = cur.getColumnIndex(KEY_EXERCISENAME);
    int iDuration = cur.getColumnIndex(KEY_DURATION);

    for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext())
    {
        result = result + cur.getString(iRow) + "        " + cur.getString(iExerciseName) + "    " + cur.getString(iDuration) + "\n";
    }

    return result;
}

}

WeekOne.java(引用所有 textView 的类

//referencing textViews 

    treadmillText = (TextView) findViewById (R.id.treadmilltext);
    stepperText = (TextView) findViewById (R.id.steppertext);
    ellipticalTrainerText = (TextView) findViewById (R.id.ellipticaltrainertext);
    stationaryRowingText = (TextView) findViewById (R.id.stationaryrowingtext);
    exerciseBikeText = (TextView) findViewById (R.id.excerisebiketext);

有没有可能我正在尝试做的事情

谢谢 :)

4

1 回答 1

0

设法解决它!:D,我没有KEY_EXERCISENAME为这个练习命名。所以,经过所有的研究,并没有放弃,插入'cv.put(KEY_EXERCISENAME, "Treadmill")createEntry()像专业人士一样工作

谢谢大家的回复和支持:)

于 2013-03-23T20:19:17.997 回答