在表格布局中显示来自 sqlite 的数据,
Posted
技术标签:
【中文标题】在表格布局中显示来自 sqlite 的数据,【英文标题】:Display data from sqlite in Table layout, 【发布时间】:2013-02-22 06:56:03 【问题描述】:我想将我的数据从 sqlite 显示到我使用 xml 使用 tablelayout 创建的表中,我想怎么做?有什么例子吗?我不知道如何将值传递到表中。它和我们做 listview 或 textview 一样吗?
下面是我的xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:textColor="#fcfafa" >
<TextView
android:id="@+id/textView1"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="@string/glucose"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#030303" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="17dp"
android:background="#C4BFBF"
android:orientation="vertical" >
</LinearLayout>
<ImageButton
android:id="@+id/imageButton1"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/textView1"
android:layout_marginLeft="16dp"
android:contentDescription="@string/btn_home"
android:onClick="onClickHome"
android:src="@drawable/homebtn" />
<Button
android:id="@+id/imageButton2"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout1"
android:layout_marginRight="18dp"
android:layout_marginTop="10dp"
android:onClick="onClickAdd"
android:textSize="14sp"
android:text="@string/btn_add"/>
<TableLayout
android:id="@+id/myTableLayout"
android:layout_
android:layout_
android:padding="1dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/imageButton2"
android:layout_marginTop="5dp" >
<TableRow
android:id="@+id/tableRow1"
android:layout_
android:layout_
android:background="@drawable/cell_normalshape">
<TextView
android:id="@+id/textView2"
android:background="@drawable/cell_normalshape"
android:layout_
android:text="@string/date"
android:gravity="center_horizontal"
android:textColor="#2A0A0A"
android:textSize="16sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView3"
android:background="@drawable/cell_normalshape"
android:layout_weight="0.2"
android:text="@string/Breakfast"
android:layout_span="2"
android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#2A0A0A"
android:textSize="16sp"/>
<TextView
android:id="@+id/textView4"
android:background="@drawable/cell_normalshape"
android:layout_
android:textColor="#2A0A0A"
android:textSize="16sp"
android:gravity="center_horizontal"
android:layout_span="2"
android:text="@string/Lunch"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView5"
android:background="@drawable/cell_normalshape"
android:layout_
android:textColor="#2A0A0A"
android:textSize="16sp"
android:layout_span="2"
android:gravity="center_horizontal"
android:text="@string/Dinner"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_
android:layout_
android:background="@drawable/cell_normalshape">
<TextView
android:id="@+id/textView7"
android:background="@drawable/cell_normalshape"
android:layout_
android:textColor="#2A0A0A"
android:text=""
android:textSize="18sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView8"
android:background="@drawable/cell_normalshape"
android:layout_weight="0.3"
android:text="@string/Pre"
android:gravity="center_horizontal"
android:layout_
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#2A0A0A"
android:textSize="16sp"/>
<TextView
android:id="@+id/textView9"
android:background="@drawable/cell_normalshape"
android:layout_weight="0.3"
android:layout_
android:textColor="#2A0A0A"
android:gravity="center_horizontal"
android:textSize="16sp"
android:text="@string/Post"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView10"
android:background="@drawable/cell_normalshape"
android:layout_
android:textColor="#2A0A0A"
android:textSize="16sp"
android:text="@string/Pre"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView11"
android:layout_
android:textColor="#2A0A0A"
android:textSize="16sp"
android:text="@string/Post"
android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView12"
android:background="@drawable/cell_normalshape"
android:layout_
android:text="@string/Pre"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#2A0A0A"
android:textSize="16sp"/>
<TextView
android:id="@+id/textView13"
android:background="@drawable/cell_normalshape"
android:text="@string/Post"
android:layout_
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#2A0A0A"
android:textSize="16sp"/>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_
android:layout_
android:background="@drawable/cell_normalshape">
<TextView
android:id="@+id/textView15"
android:background="@drawable/cell_normalshape"
android:layout_
android:textColor="#2A0A0A"
android:text=""
android:textSize="18sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView16"
android:background="@drawable/cell_normalshape"
android:layout_weight="0.3"
android:text=""
android:layout_
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#2A0A0A"
android:textSize="18sp" />
<TextView
android:id="@+id/textView17"
android:background="@drawable/cell_normalshape"
android:layout_weight="0.3"
android:layout_
android:textColor="#2A0A0A"
android:textSize="18sp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView18"
android:background="@drawable/cell_normalshape"
android:layout_
android:textColor="#2A0A0A"
android:textSize="18sp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView19"
android:layout_
android:textColor="#2A0A0A"
android:textSize="18sp"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView20"
android:background="@drawable/cell_normalshape"
android:layout_
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#2A0A0A"
android:textSize="18sp" />
<TextView
android:id="@+id/textView21"
android:background="@drawable/cell_normalshape"
android:text=""
android:layout_
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#2A0A0A"
android:textSize="18sp" />
</TableRow>
</TableLayout>
</RelativeLayout>
这是我的代码..
private SQLiteDatabase database;
private ArrayList<String> result = new ArrayList<String>();
private ArrayList<Long> idList = new ArrayList<Long>();
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.glucose_view);
TableLayout myTable = (TableLayout)findViewById(R.id.myTableLayout);
DBHelper helper = new DBHelper(this);
database = helper.getWritableDatabase();
// view data
try
String query = "select * from " + DBHelper.TABLE_GLUCOSE;
Cursor c = database.rawQuery(query, null);
c.moveToFirst();
c.moveToPosition(3); // may be two, not sure if the cursor starts at 0 or 1
while (c.isAfterLast() == false)
String tblName = c.getString(1);
Cursor table = database.rawQuery("SELECT * FROM " + DBHelper.TABLE_GLUCOSE,null);
table.moveToFirst();
if (table.getString(table.getColumnIndex("Title")) != null)
// Your code to create table
c.moveToNext();
谁能帮帮我..
【问题讨论】:
您是否尝试使用此代码...它提供了什么... 和我们做listview或者textview一样吗? => 是的,这很简单,因为我们在 TextView 中设置了值。 这是什么意思?在 textview 中设置值? 我可以这样使用吗? try if( cursor != null ) if (cursor.moveToFirst()) do idList.add(cursor.getInt(0)); list.add(cursor.getString(1)); while (cursor.moveToNext()); catch(SQLException e) cursor.moveToFirst(); TextView TV_Name = (TextView) findViewById(R.id.TVName);TV_Name.setText(cursor.getString(1));cursor.close(); 你的问题是什么,你想达到什么目的,为什么要把光标移到第4条记录 【参考方案1】:试试这个
if((cursor.moveToFirst())
do
LinearLayout layout=new LinearLayout(context);
layout.setOrienation(LinearLayout.HORIZONTAL);
for(int i=0;i<cursor.getColumnCount();i++)
TextView text=new TextView(context);
switch(cursor.getType(i))
case Cursor.FIELD_TYPE_INTEGER:
text.setText(String.valueOf(cursor.getInt(i)))
break;
case Cursor.FIELD_TYPE_STRING:
text.setText(cursor.getString(i))
break;
...
..
layout.addView(text);
tablelayout.addView(layout);
whilie(cursor.moveToNext())
【讨论】:
在你的活动内容上下文中声明一个全局变量上下文;并在 onCreate 中设置 context=this,现在使用这个上下文变量。上下文意味着范围 真的很抱歉,我只是这方面的初学者,这与我创建的 xml 代码一起使用对吗?但是我们想如何用我们想要显示的数据来声明 tablerows? 按下控制键,然后光标后的空格会显示可用的方法,developer.android.com/reference/android/database/… 它显示,但不在我创建的表中.. 我想如何将 db 中的第 1 列放在 android 表布局中的 column1 中 @user2089567 我会在 20 分钟后回来以上是关于在表格布局中显示来自 sqlite 的数据,的主要内容,如果未能解决你的问题,请参考以下文章