Android需要帮助显示来自sqlite数据库的东西

时间:2015-01-28 16:16:48

标签: android database sqlite android-layout adapter

您好我需要您在活动中显示来自数据库的数据的帮助,我一直在尝试很多东西,但不知道该怎么做。我正在创建某种天气应用程序。它在设备上有数据库中的数据。 有DataBaseHandler类,它返回包含所有Spots的List。

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

我有listView xml的活动我想在上面的代码中根据一个元素的另一个relativelayout显示一些东西。 SpotActivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        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>

这里是上面列表视图中一个元素的相对布局。 我想从数据库中使用TextView id = SpotName连接SpotName,使用Windspeed Text View连接数据库中的Windspeed,以及从数据库中基于SpotWeatherIcon在ImageView中显示图像。 spotlista_layout.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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>

这是活动代码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);
}

感谢您的帮助!我试图这样做,仍然有很大的问题所以现在我求助。它是一个我正在工作的学校项目,这是我必须做的最后一件事,以使应用程序工作 - 显示东西:D

1 个答案:

答案 0 :(得分:0)

您需要为ListView创建适配器。我建议你看The World Of ListView

所有适配器都为数据集中的每个项目提供了一个视图。对于游标数据,您应该将CursorAdapter子类化,并至少实现newView()bindView()方法。