2013-06-12 53 views
2

有沒有辦法解決我的問題?通過活動從數據庫中獲取變量?

下面是我的情況的一個簡單的例子(那不是真正的,它是類似的)。 我有一個活動:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/lib/com.google.ads" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#000000" 
tools:context=".MainActivity" > 

<com.loopj.android.image.SmartImageView 
    android:id="@+id/image_switcher" 
    android:layout_width="500dp" 
    android:layout_height="match_parent" 
    android:layout_alignParentTop="true" /> 

在這種活動我有一個Android:layout_width = 「500dp」。 這裏的值500需要從數據庫中獲取並在此處使用。

如何從活動中調用數據庫? 任何解決方案是可接受的,因爲它的工作:)

我有一些想法: 在onCreate活動調用數據庫,他們以某種方式設置我需要的屬性值。

我很新的android和它很有趣,但我沒有人問。

感謝

回答

1

你需要的第一件事就是創建一個databaseAdapter類,這將是與你做你所有的數據庫連接工作一個輔助類。在這種情況下檢索使用SQL SELECT從價值500 ....(我要去假設你知道SQL)

這裏有一些好的裁判

http://android-er.blogspot.ie/2011/06/simple-example-using-androids-sqlite.html http://www.youtube.com/watch?v=j-IV87qQ00M

接下來,你要設定SmartImageView的屬性,首先在代碼中獲取對它的引用。

SmartImageView myImage = (SmartImageView) this.findViewById(R.id.image_switcher); 

然後,可以通過引用它(如果循環J smartImageView具有適當屬性)設定的屬性,如果不把它放在一個線性佈局,得到該基準,並設置其佈局寬度(尊重像素設備的密度)使用

float displayMetricsDensity = basecontext.getResources().getDisplayMetrics().density; 
     int height = (int)(pixelsValue * displayMetricsDensity);//use your own pixelsValus 

LinearLayout myLL = this.findViewById(R.id.image_switcher_LinearLayout); 
LayoutParams LP = new LayoutParams(height, LayoutParams.MATCH_PARENT); 

myLL.setLayoutParams(LP); 

玩得開心學習android ...記得它的真正的Java,其上有一個Android框架和API。

相關問題