如何将项目与列标题对齐

时间:2012-09-15 14:29:23

标签: android

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

名称-----------------编号-----------------年龄

Yan 1232312 25

这是我的xml代码,用于查看sqlite数据库

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

1 个答案:

答案 0 :(得分:0)

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

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

这是您看到高分的活动:

    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编程很新,所以我的解决方案可能并不完美但至少对我有用。

相关问题