2014-02-11 122 views
0

Hy guys!充氣佈局不起作用

我有一個很大的問題。我正在試圖使用cursoradapter在列表視圖中顯示來自sqlite數據庫的數據。爲此,我有一個自己創建的佈局和一個光標。我使用了方法bindview和newview。

我的錯誤是與id的東西。

這裏是我的adapterclass:

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) 

請幫助我!

+0

「我的錯誤是什麼一個ID。」注意指定具體問題是什麼?另外,while(cursor.moveToFirst())'不正確,因爲如果遊標中有任何行,它將無限循環。 – laalto

+0

是的,我添加了logcat ...但我應該寫什麼? – schocka94

+0

http://tausiq.wordpress.com/2012/08/22/android-list-view-from-database-with-cursor-adapter/請參閱本教程 –

回答

1

它明確指出column '_id' does not exist

你必須在你的數據庫中使用CursorAdapter添加一個字段名稱_id

解決方案

如果您還沒有命名_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) 
+0

是否意味着我應該在遊標中獲得一個列ID或在數據庫中創建一個新的? – schocka94

+0

@ schocka94你可以發佈你的數據庫創建代碼,它具有「創建表」字符串..我只是想看到字段名稱。所以,我可以建議一些.. :) –

+0

我應該在數據庫中創建一個新的列或應該包含光標一個ID? – schocka94