1

how to align the content to its column title, i have here the sample below

Name-----------------Number-----------------Age

Yan 1232312 25

here is my xml code to view the sqlite database

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TableLayout
       android:id="@+id/table"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableRow>

            <TextView
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="fill_parent"
                android:text="Name" >
            </TextView>

            <TextView
           android:id="@+id/Hot"
                android:layout_width="fill_parent"
                   android:layout_weight="1"
                android:layout_height="fill_parent"
                android:text="Number" >
            </TextView>
        </TableRow>

          </TableLayout>
  <TextView
                android:id="@+id/tvSqlInfo"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="fill_parent"
                android:text="Name" >
            </TextView>

        />

</LinearLayout>

and this one is the my Activityclass to view the xml,can you please teach me where to put the tablelayout parameter thanks

    `public class SQLView extends Activity {
    TextView tv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.sqlview);
    TableLayout table = (TableLayout) findViewById(R.id.table);
    TableRow row= new TableRow(this);
    TextView text = new TextView(this);
    row.addView(text);
    table.addView(row);


            TableLayout.LayoutParams l = 
                new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    l.weight = 1;

         tv= (TextView)findViewById(R.id.tvSqlInfo);
            HotorNot info = new HotorNot(this);
            info.open();
            String data = info.getData();
            info.close();
            tv.setText(data);



here is my database class

    public class HotorNot {

        public static final String KEY_ROWID= "_id";
        public static final String KEY_NAME= "Person_Names";
        public static final String KEY_HOT= "personHOT";

        private static final String DATABASE_name= "HotorNotdb";
        private static final String DATABASE_TABLE= "PeopleTable";
        private static final int DATABASE_VERSION= 1;

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

        public 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_NAME + " TEXT NOT NULL, " +
                        KEY_HOT + " TEXT NOT NULL);"


                );
            }

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


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

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

        public void close(){
            ourHelper.close();
        }
        public long createEntry(String name, String hotness) {
            // TODO Auto-generated method stub
            ContentValues cv = new ContentValues();
            cv.put(KEY_NAME, name);
            cv.put(KEY_HOT, hotness);
        return  ourDatabase.insert(DATABASE_TABLE, null, cv);


        }
        public String getData() {
            // TODO Auto-generated method stub
            String[] columns = new String []{ KEY_ROWID, KEY_NAME, KEY_HOT};
            Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
            String results = "";

            int iRow = c.getColumnIndex(KEY_ROWID);
            int iName = c.getColumnIndex(KEY_NAME);
            int iHotness = c.getColumnIndex(KEY_HOT);

            for (c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
                results = results  + c.getString(iRow) + " " + c.getString(iHotness) + " " + c.getString(iName)+"\n";

            }

            return results;


        }
        public String getName(long l) {
            // TODO Auto-generated method stub
            String[] columns= new String []{KEY_ROWID, KEY_NAME,KEY_HOT};
            Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "_" + l, null, null, null, null);
            if (c != null ){

                c.moveToFirst();
                String name = c.getString(1);
                return name;
            }

            return null;
        }
        public String getHotness(long l) {
            // TODO Auto-generated method stub
            String[] columns= new String []{KEY_ROWID, KEY_NAME,KEY_HOT};
            Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
            if (c != null ){

                c.moveToFirst();
                String hotness = c.getString(2);
                return hotness;
            }
            return null;
        }
    } 
4

1 に答える 1

0

私は同じ問題を抱えていて、グーグルで検索したときに簡単な答えを見つけることができませんでした.

これが私のコードです。これは私が取り組んでいるゲームのものです。列は2つしかありませんが、簡単に追加できます。行は最高スコアでソートされています。

これは、ハイスコアが表示されるアクティビティです。

    public class Highscore extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.highscore);
        TextView tvname = (TextView) findViewById(R.id.tvName);
        TextView tvscore = (TextView) findViewById(R.id.tvScore);
        SQLScore info = new SQLScore(this);
        info.open();
        String name = info.getName();
        String score = info.getScore();
        info.close();
        tvname.setText(name);
        tvscore.setText(score);
        }
}

そして、これはその XML です。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="*" >

        <TableRow>

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Name" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="Score" />
        </TableRow>

        <TableRow>

            <TextView
                android:id="@+id/tvName"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Name" />

            <TextView
                android:id="@+id/tvScore"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Score" />
        </TableRow>
    </TableLayout>

</LinearLayout>

最後に、SQL ハンドラ ファイルのメソッド:

public String getName() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_SCORE};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,     null, KEY_SCORE + " DESC", "10");
    String result = "";

    int iName = c.getColumnIndex(KEY_NAME);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result + c.getString(iName) + "\n";
    }

    return result;
}

public String getScore() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_SCORE};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, KEY_SCORE + " DESC", "10");
    String result = "";

    int iScore = c.getColumnIndex(KEY_SCORE);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result + c.getString(iScore) + "\n";
    }

    return result;
}

基本的に、行をフェッチしていますが、すべての行内の各列からデータを返す 1 つのメソッドが必要です。私はAndroidプログラミングにかなり慣れていないので、私のソリューションは完璧ではないかもしれませんが、これは少なくとも私にとってはうまくいきました.

于 2014-06-05T20:54:50.820 に答える