2012-11-15 65 views
0

我不習慣這個適配器的東西,我有一個CursorAdaptor的問題。這裏是我的課:CursorAdapter throwing「Couldnt read [...] col -1」

public class TicketsAdapter extends CursorAdapter { 
    private Cursor cursor; 
    private Context context; 
    private final LayoutInflater inflater; 

    public TicketsAdapter (Context context, Cursor cursor) { 
     super(context, cursor, FLAG_REGISTER_CONTENT_OBSERVER); 
     this.cursor = cursor; 
     this.context = context; 
     inflater = LayoutInflater.from(context); 
    } 

    public void bindView(View view, Context context, Cursor cursor) { 
     //cursor.moveToFirst();  I have tried this, same error 
     //try { I have used this 'try' and find out here's the code where an exception is being throwed 
     TextView ticketId = (TextView) view.findViewById(R.id.ticketId); 
     TextView ticketCourse = (TextView) view.findViewById(R.id.ticketCourse); 
     TextView ticketDate = (TextView) view.findViewById(R.id.ticketDate); 
     TextView ticketTime = (TextView) view.findViewById(R.id.ticketTime); 
     RelativeLayout row = (RelativeLayout) view.findViewById(R.id.ticketRow); 
     TextView jogodaveia = (TextView) view.findViewById(R.id.textView1); 

     ticketId.setText(cursor.getString(cursor.getColumnIndex("ticket_id"))); 
     ticketCourse.setText(cursor.getString(cursor.getColumnIndex("course"))); 
     ticketDate.setText(cursor.getString(cursor.getColumnIndex("date"))); 
     ticketTime.setText(cursor.getString(cursor.getColumnIndex("departure"))); 

     String present = cursor.getString(cursor.getColumnIndex("present")); 

     if (present.equals("0")) { 
      row.setBackgroundColor(Color.rgb(210, 245, 210)); 
      jogodaveia.setTextColor(Color.rgb(16, 181, 4)); 
     } else { 
      row.setBackgroundColor(Color.rgb(255, 255, 255)); 
      jogodaveia.setTextColor(Color.rgb(206, 109, 0)); 
     } 
     //} catch (Exception e) {android.util.Log.w("ADAPTER", e.toString());} 
    } 

    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     final View view = inflater.inflate(R.layout.ticket,parent,false); 
     return view; 
    } 
} 

在我Tickets.java我:

public class Tickets extends ListActivity { 
    private SQLiteDatabase db=null; 
    private Cursor cursor=null; 
    private ListView layout; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tickets); 

     layout = getListView(); 
     layout.setDividerHeight(3); 

     db = (new DatabaseHelper(this)).getWritableDatabase(); 
     cursor = db.rawQuery("SELECT _id, ticket_id, course, date, departure FROM tickets ORDER BY date", 
                  null); 
     //cursor.moveToFirst(); I have tried this here too 
     layout.setAdapter(new TicketsAdapter (Tickets.this, cursor)); 

    } 
    .... 
} 

我得到的錯誤是

Failed to read row 0, column -1 from a CursorWindow which has 5 rows, 5 columns. 
11-15 00:03:30.700: W/ADAPTER(1756): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 

,但仍顯示我的名單,但不工作正常。我做錯了什麼想法?

回答

2
Failed to read row 0, column -1 

這意味着你的的一個未找到,行索引是罰款。


在這裏,你問"present"

String present = cursor.getString(cursor.getColumnIndex("present")); 

但你在查詢忘記"present"

cursor = db.rawQuery("SELECT _id, ticket_id, course, date, departure FROM tickets ORDER BY date", null); 

而且你會發現所有的觀念的newView()一次並將它們存儲在ViewHolder中。列索引也是如此。沒有必要重複搜索相同的信息,這會給你一個更快的應用程序。

+0

我不能...相信.../facepalm 感謝您的幫助人,我一直在想,查詢返回null或類似的東西...再次感謝! –

+0

沒問題,很高興我能幫忙! – Sam

相關問題