ventsi.slav
ventsi.slav

Reputation: 332

Getting a variable from the db through the activity?

Is there a way or something else to solve my issue?

Here's a simple example of my situation (thats not the real one, it's something similar). I have an activity :

<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" />

In that activity I have an android:layout_width="500dp". The value 500 here needs to be taken from the database and to be used here in the activity.

How can I call to the database from the activity ? Any solution is acceptable since its a working one :)

I have something in mind: In the onCreate activity to call the database and them somehow to set the value on the attribute I need.

I'm pretty new in android and it's a lot of fun but I don't have anyone to ask.

Thanks

Upvotes: 2

Views: 64

Answers (1)

Kickaha
Kickaha

Reputation: 3857

First thing you need is to create a databaseAdapter class, this will be a single helper class with which you do all your database connection work. In this case retrieving the value 500 using a SQL SELECT FROM .... (Im going to assume you know SQL)

here are some good refs

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

Next you want to set the attribute of your SmartImageView, first get a reference to it in code.

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

Then you can set the attribute by referencing it (if the loopJ smartImageView has an appropriate attribute), if not put it in a linear layout, get a reference to that and set its layout width (respecting the pixel density of the device) using

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

Have fun learning android... remember its really Java, with an Android framework and API's on top.

Upvotes: 1

Related Questions