【问题标题】:inflating the layout doesn't work膨胀布局不起作用
【发布时间】:2014-02-11 11:04:37
【问题描述】:

大家好!

我有一个大问题。我正在尝试使用光标适配器在列表视图中显示来自 sqlite 数据库的数据。为此,我有一个自己创建的布局和一个光标。我使用了方法 bindview 和 newview。

我的错误是带有 id 的。

这是我的适配器类:

public class RouteAdapterActivity extends BaseAdapter{

    Context context;
    ArrayList<DefineRouteActivity> arraylist;

    public RouteAdapterActivity(Context context, ArrayList<DefineRouteActivity> list) {
        this.context = context;
        arraylist = list;
    }
    @Override
    public int getCount() {

        return arraylist.size();
    }

    @Override
    public Object getItem(int position) {
        return arraylist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        DefineRouteActivity routeItems = arraylist.get(position);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.outputlayout,null);

        TextView tvDeparture = (TextView)view.findViewById(R.id.txOutputDeparture);
        tvDeparture.setText(routeItems.getAbfahrtszeit());
        TextView tvDuration = (TextView)view.findViewById(R.id.txOutputDuration);
        tvDuration.setText(routeItems.getDauer());
        TextView tvTransition = (TextView)view.findViewById(R.id.txOutputTransition);
        tvTransition.setText(routeItems.getUmstieg());

        return view;
    }
}

这是我的光标适配器:

public void getRoute() {
    lvList = (ListView)findViewById(R.id.lvView);
    mdbH = new DatabaseHelperActivity(this);
    cursor = mdbH.fetchallRoutes(mdbH);
    ArrayList<DefineRouteActivity> route = new ArrayList<DefineRouteActivity>();
    RouteAdapterActivity adapter = new RouteAdapterActivity(PlanOutputActivity.this, route);

    lvList.setAdapter(adapter);



}

最后这是我的布局,应该插入到列表视图中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/outputlayout"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_marginTop="10dp"
    android:background="@color/ligthgrey"
    android:onClick="layoutOutput">

    <TextView
        android:id="@+id/txOutputDeparture"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Abfahrt "
        android:textColor="@color/black"
        android:layout_alignParentLeft="true"
        android:maxLength="@android:integer/config_shortAnimTime"
        android:layout_marginTop="10dp"/>

    <TextView
        android:id="@+id/txOutputDuration"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/txOutputDeparture"
        android:text="Dauer"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_marginTop="10dp"/>

    <TextView
        android:id="@+id/txOutputTransition"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/txOutputDuration"
        android:text="Umstieg"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_marginTop="10dp"/>

</RelativeLayout>

这是我的游标声明:

SELECT f.id, strftime('%H:%M', f.abfahrt) AS Abfahrt," +
                "strftime('%H:%M', f.ankunft) AS Ankunft," +
                "strftime('%H:%M', strftime('%s',f.ankunft)- strftime('%s',f.abfahrt), 'unixepoch') AS Dauer," +
                "r.name AS Route," +
                "count(u.fahrt_id) AS Umstiege " +
                "FROM scotty_fahrt f " +
                "JOIN scotty_haltestelle start ON f.start_id = start.id " +
                "JOIN scotty_haltestelle ziel ON f.ziel_id = ziel.id " +
                "JOIN scotty_route r ON f.route_id = r.id " +
                "LEFT OUTER JOIN scotty_umstiegsstelle u ON f.id = u.fahrt_id " +
                "WHERE start.name = 'Haibach ob der Donau Ortsmitte' " +
                "AND ziel.name = 'Neufelden Busterminal (Schulzentrum)' " +
                "GROUP BY u.fahrt_id

这是我的表格:

CREATE TABLE "scotty_fahrt" (
"id" integer NOT NULL PRIMARY KEY,
"route_id" integer NOT NULL REFERENCES "scotty_route" ("id"),
"start_id" integer NOT NULL REFERENCES "scotty_haltestelle" ("id"),
"ziel_id" integer NOT NULL REFERENCES "scotty_haltestelle" ("id"),
"abfahrt" datetime NOT NULL,
"ankunft" datetime NOT NULL

CREATE TABLE "scotty_halt" (
"id" integer NOT NULL PRIMARY KEY,
"folgenummer" integer NOT NULL,
"route_id" integer NOT NULL REFERENCES "scotty_route" ("id"),
"haltestelle_id" integer NOT NULL REFERENCES "scotty_haltestelle" ("id"),
"abfahrt" datetime,
"ankunft" datetime)

CREATE TABLE "scotty_haltestelle" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(30) NOT NULL)

CREATE TABLE "scotty_route" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(30) NOT NULL)

CREATE TABLE "scotty_umstiegsstelle" (
"id" integer NOT NULL PRIMARY KEY,
"folgenummer" integer NOT NULL,
"fahrt_id" integer NOT NULL REFERENCES "scotty_fahrt" ("id"),
"haltestelle_id" integer NOT NULL REFERENCES "scotty_haltestelle" ("id"),
"route_id" integer NOT NULL REFERENCES "scotty_route" ("id"),
"abfahrt" datetime NOT NULL,
"ankunft" datetime NOT NULL)

请帮帮我!!

【问题讨论】:

  • “我的错误是带有 id 的东西。”想说明具体问题是什么?此外,while(cursor.moveToFirst()) 也不正确,因为如果游标中有任何行,它将无限循环。
  • 是的,我添加了 logcat ...但是我应该写什么?
  • 你的数据表有_id字段吗?
  • jeah 我实际上是使用该教程构建它,但我找不到失败

标签: android database sqlite android-cursoradapter


【解决方案1】:

明确说明column '_id' does not exist

您必须在数据库中添加字段名称_id 才能使用CursorAdapter

解决方案

如果您没有名为_id 的字段,请创建一个。如果您有id 作为字段,只需将其重命名为_id

编辑

CREATE TABLE "scotty_fahrt" (
"_id" integer NOT NULL PRIMARY KEY,
"route_id" integer NOT NULL REFERENCES "scotty_route" ("_id"),
"start_id" integer NOT NULL REFERENCES "scotty_haltestelle" ("_id"),
"ziel_id" integer NOT NULL REFERENCES "scotty_haltestelle" ("_id"),
"abfahrt" datetime NOT NULL,
"ankunft" datetime NOT NULL

CREATE TABLE "scotty_halt" (
"_id" integer NOT NULL PRIMARY KEY,
"folgenummer" integer NOT NULL,
"route_id" integer NOT NULL REFERENCES "scotty_route" ("_id"),
"haltestelle_id" integer NOT NULL REFERENCES "scotty_haltestelle" ("_id"),
"abfahrt" datetime,
"ankunft" datetime)

CREATE TABLE "scotty_haltestelle" (
"_id" integer NOT NULL PRIMARY KEY,
"name" varchar(30) NOT NULL)

CREATE TABLE "scotty_route" (
"_id" integer NOT NULL PRIMARY KEY,
"name" varchar(30) NOT NULL)

CREATE TABLE "scotty_umstiegsstelle" (
"_id" integer NOT NULL PRIMARY KEY,
"folgenummer" integer NOT NULL,
"fahrt_id" integer NOT NULL REFERENCES "scotty_fahrt" ("_id"),
"haltestelle_id" integer NOT NULL REFERENCES "scotty_haltestelle" ("_id"),
"route_id" integer NOT NULL REFERENCES "scotty_route" ("_id"),
"abfahrt" datetime NOT NULL,
"ankunft" datetime NOT NULL)

【讨论】:

  • 是否意味着我应该在游标中获取列 ID 或在数据库中创建一个新列?
  • @schocka94 你能发布你的数据库创建代码,它有“create table”字符串。我只是想看看那个字段名。所以我可以建议一些.. :)
  • 我应该在数据库中创建一个新列还是应该包含游标一个 id?
  • 好的..所以你已经声明了"id",只需用"_id"替换它就可以了。并替换这些依赖项。
  • 我应该将每个 colmun id 替换为 _id 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多