【问题标题】:Android need help displaying things from sqlite databaseAndroid 需要帮助来显示来自 sqlite 数据库的内容
【发布时间】:2015-01-28 16:16:47
【问题描述】:

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

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 的活动,我想根据一个元素的另一个相对布局从上面的代码中显示一些东西。 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>

这是上面列表视图中一个元素的相对布局。 我想将数据库中的 SpotName 与 TextView id=SpotName 连接,将数据库中的 Windspeed 与 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

【问题讨论】:

  • 您的列表适配器在哪里?
  • 我没有,可能据我所知,这是我需要你帮助的部分。我试图完全理解适配器,但仍然无法让它们与我的应用程序一起使用。 @Karakuri
  • 是否有人可以帮助我使用该适配器?我可以说它是某种 dedline 情况,对你来说,最多可能是 10 分钟。这不像我没有尝试我知道我需要适配器只需要了解它们:) 否则谢谢你我现在正在看那个视频。

标签: android database sqlite android-layout adapter


【解决方案1】:

您需要为您的 ListView 创建一个适配器。我推荐你看The World Of ListView

适配器所做的只是为数据集中的每个项目提供一个视图。对于游标数据,您应该继承 CursorAdapter 并至少实现 newView()bindView() 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多