1

如何将内容与其列标题对齐,我这里有下面的示例

姓名-----------------号码-----------------年龄

严 1232312 25

这是我查看 sqlite 数据库的 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: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>

这个是我的Activityclass来查看xml,你能教我把tablelayout参数放在哪里吗谢谢

    `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

我有同样的问题,当我用谷歌搜索时找不到简单的答案。

这是我的代码,它来自我正在开发的游戏,它只有两列,但可以轻松添加更多列。行按最高分排序。

这是您看到高分的活动:

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

基本上,您正在获取行,但您需要一种方法从每一行中的每一列返回数据。我对android编程很陌生,所以我的解决方案可能并不完美,但这至少对我有用。

于 2014-06-05T20:54:50.820 回答