mercredi 28 janvier 2015

Android need help displaying things from sqlite database

Hello i need Your help in displaying data from database in activity, ive been trying many things but have no idea how to do that. Im creating some kind of weather app. It has Data in Database on the device. There is DataBaseHandler Class which returns List with all Spots.



public List<Spot> getUserSpotList(int id){
List<Spot> spotList = new ArrayList<Spot>();
String selectQuery = "SELECT " +SPOT_ID+","+ SPOT_NAME + ","+ SPOT_LATITUDE + ","+SPOT_LONGITUDE+","+SPOT_WIND_SPEED +","+SPOT_WEATHER_ICON+
" FROM " + TABLE_SPOT + " WHERE "+SPOT_USER_ID + "="+id;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

if (cursor.moveToFirst()) {
do {
Spot spot = new Spot();
spot.setSpot_id(Integer.parseInt(cursor.getString(0)));
spot.setSpotName(cursor.getString(1));
spot.setSpotLatitude(cursor.getString(2));
spot.setSpotLongitude(cursor.getString(3));
spot.setWindSpeed(Double.parseDouble(cursor.getString(4)));
spot.setSpotWeatherIcon(cursor.getString(5));
// Adding User to list
spotList.add(spot);
} while (cursor.moveToNext());
}

return spotList;
}


And i Have Activity with listView xml in witch i want to display some stuff from the code above based on another relativelayout for one element. SpotActivity.xml



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.mk.rcwindy.SpotActivity">

<ListView
android:id="@+id/listLajout"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>


And here is Relative layout for one element in the list view above. I would like to connect SpotName from database with TextView id=SpotName, Windspeed from database with Windspeed Text View, and Display image in ImageView based on SpotWeatherIcon from database. spotlista_layout.xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip"
android:id="@android:id/list">



<ImageView
android:id="@+id/SpotAvilabilityIcon"
android:layout_width="20dp"
android:layout_height="match_parent"
android:background="@color/green_apple"/>

<ImageView
android:id="@+id/WeatherIcon"
android:layout_width="70dp"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/WindSpeed"
android:scaleType="centerInside"
/>
<TextView
android:id="@+id/WindSpeed"
android:text=""
android:textSize="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/WindSpeedUnit"
android:layout_gravity="center"
android:layout_centerVertical="true"/>
<TextView
android:text="m/s"
android:textSize="30dp"
android:id="@+id/WindSpeedUnit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/SpotAvilabilityIcon"
android:layout_toStartOf="@+id/WeatherIcon"
android:layout_toLeftOf="@+id/WeatherIcon"
android:layout_centerVertical="true">

<TextView
android:id="@+id/SpotName"
android:text=""
android:textSize="40dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_centerVertical="true"/>
</FrameLayout>


</RelativeLayout>


And here is the code of activity SpotActivity



public class SpotActivity extends ActionBarActivity {


int loggedUserId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spot);

Intent intent = getIntent();

if (null != intent) {
loggedUserId = intent.getIntExtra("logged", 0);
}

String text = "logged user id = "+loggedUserId;
Toast t1 = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
t1.show();




}

public int userIdSender(){
return loggedUserId;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_spot, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id== R.id.action_new){
Intent intent = new Intent(this, SpotAddActivity.class);
intent.putExtra("logged",loggedUserId);
startActivity(intent);
}

return super.onOptionsItemSelected(item);
}


Thanks for Help! I tried to do that and still have a big problems so now im asking for help. Its a school project im working at and that is the last thing i have to do to make the app work-Display stuff :D


Aucun commentaire:

Enregistrer un commentaire