膨胀布局不起作用

时间:2014-02-11 11:04:38

标签: android database sqlite android-cursoradapter

Hy guys!

我有一个大问题。我正在尝试使用cursoradapter在listview中显示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)

请帮助我!!

1 个答案:

答案 0 :(得分: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)