2012-07-17 68 views
0

我希望你能幫我解決這個問題。我ListActivity類woudlnt顯示在列表中的螞蟻項目Android:ListActivity不顯示任何項目

這是我的代碼:

import android.os.Bundle; 
import android.provider.BaseColumns; 
import android.app.Activity; 
import android.app.ListActivity; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.CursorAdapter; 
import android.widget.EditText; 
import android.widget.SimpleCursorAdapter; 
import android.support.v4.app.NavUtils; 

public class Sale2 extends ListActivity { 
    private static final int DIALOG_ID = 100; 

    private SQLiteDatabase database; 

    DBAdapter db; 

    private CursorAdapter dataSource; 

    private View entryView; 

    private EditText firstNameEditor; 

    private EditText lastNameEditor; 

    private static final String fields[] = { "_id", "_date", "_soldPrice" }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     try { 
      db = new DBAdapter(this); 
      db.open(); 
      Cursor data = db.getAllSales2(); 
      System.out.println("after getallsales2"); 
      dataSource = new SimpleCursorAdapter(this, R.layout.row, data, 
        fields, new int[] { R.id._id, R.id.first, R.id.last }); 
      System.out.println("after simplecursoradapter" 
        + dataSource.toString()); 

      setListAdapter(dataSource); 
      System.out.println("setListAdapter"); 
     } catch (Exception e) { 
      Log.e("ERROR", "ERROR IN CODE: " + e.toString()); 
      e.printStackTrace(); 
     } 
    } 

} 

,這是row.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/_id" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/first" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/last" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

注:

我確信光標有一些關於它的數據。但它wouldnt顯示它們

幫助表示讚賞

+0

在進行查詢後(在'Cursor data = db.getAllSales2(); ')之後放入'Log'條目並查看'data.getCount()'返回的數字。 – Luksprog 2012-07-17 14:47:52

+0

你確定你從sqlite數據庫中獲得正確的列名嗎? – waqaslam 2012-07-17 14:50:02

回答

-1

我認爲你需要設置適配器從XML加載列表對象。喜歡的東西:

listaNodiView = (ListView) findViewById(R.id.ListViewLista); 
listaNodiView.setListAdapter(dataSource); 

其中R.id.ListViewLista是你的佈局XML聲明裏面的清單資源的ID(不可row.xml)

+0

你錯了。從doc:定義此列表項視圖的佈局文件的資源標識符。佈局文件應至少包含在「to」中定義的那些命名視圖 – Blackbelt 2012-07-17 14:50:59

1

你缺少的setContentView(查看)的調用,其中包含一個ListView這應該用數據來填充從遊標

+1

錯誤,他擴展了一個ListActivity,該ListActivity具有一個ListView作爲其內容視圖。 – Luksprog 2012-07-17 14:55:57

+0

非常感謝你。它正在工作 – 2012-07-18 23:52:07

0

試試這個:

public class Sale2 extends ListActivity { 
    private static final int DIALOG_ID = 100; 

    private SQLiteDatabase database; 

    DBAdapter db; 

    private ListAdapter dataSource; 

    private View entryView; 

    private EditText firstNameEditor; 

    private EditText lastNameEditor; 

    private static final String fields[] = { "_id", "_date", "_soldPrice" }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     try { 
      db = new DBAdapter(this); 
      db.open(); 
      Cursor data = db.getAllSales2(); 
      System.out.println("after getallsales2"); 
      startManagingCursor(data); 
      dataSource = new SimpleCursorAdapter(this, R.layout.row, data, 
        fields, new int[] { R.id._id, R.id.first, R.id.last }); 
      System.out.println("after simplecursoradapter" 
        + dataSource.toString()); 

      setListAdapter(dataSource); 
      System.out.println("setListAdapter"); 
     } catch (Exception e) { 
      Log.e("ERROR", "ERROR IN CODE: " + e.toString()); 
      e.printStackTrace(); 
     } 
    } 

} 

我已經改變了的數據源ListAdapter,也是一個打了startManagingCursor

還看到過這裏

http://developer.android.com/reference/android/app/ListActivity.html

0

你忘了使用參考到ListView的例子,但你已經擴展了ListActivity而不是活動,你必須得到參考ListActivity的列表如下:

ListView lv =getListView();