3
我正在創建一個電子郵件界面,使用自定義的CursorAdapter從數據庫填充ListView。此ListView中的項目然後顯示一個電子郵件線程,以非常相似的方式使用CursorAdapter填充ListView。我遇到的問題是第二個ListView將不會填充。Android CursorAdapter not calling newView
用於初始化這兩個適配器的代碼幾乎是相同的(我們可以說它們適用於所有意圖和目的),並且通過使用日誌語句,我已經能夠驗證光標被傳遞給第二個適配器不是空的並且包含它應該的所有數據。什麼似乎是問題是從來沒有調用newView方法。我試圖調查爲什麼會發生這種情況,而且什麼也沒有。我想知道的是爲什麼newView方法不會被調用。這個電話應該從哪裏觸發?
以下是ListActivity的onCreate方法。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.email_view);
cursor = EmailActivity.dbAdapter.fetchEmailThread(getIntent().getStringExtra("senderUsername"));
Log.i(TAG, getIntent().getStringExtra("senderUsername") + " " + cursor.getCount() + " " + cursor.getColumnCount());
startManagingCursor(cursor);
adapter = new EmailViewCursorAdapter(this, cursor);
setListAdapter(adapter);
}
這裏是適配器的代碼,只是構造函數和newView方法。
public EmailViewCursorAdapter(Context context, Cursor c) {
super(context, c, true);
inflater = ((Activity) context).getLayoutInflater();
usernameInd = c.getColumnIndex("sender_username");
createdInd = c.getColumnIndex("created_at");
bodyInd = c.getColumnIndex("body");
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.i(TAG, "Made it here - new view");
View view = inflater.inflate(R.layout.email_view_item, parent, false);
TextView senderName = (TextView) view.findViewById(R.id.sender_username);
TextView timeString = (TextView) view.findViewById(R.id.sent_time);
TextView emailBody = (TextView) view.findViewById(R.id.email_body);
senderName.setText(cursor.getString(usernameInd));
if(senderName.getText().equals(DataManager.getUser().getUsername()))
senderName.setTextColor(0x999999);
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date sent = format.parse(cursor.getString(createdInd));
timeString.setText(EmailModel.createTimeText(sent));
} catch (ParseException e) {
timeString.setText("");
}
emailBody.setText(cursor.getString(bodyInd));
return view;
}
是NewView的從來沒有所謂的每一行? CursorAdapter重用視圖,所以你不會爲每一行調用它。你的bindView中有什麼?日誌中的任何錯誤? (順便說一句,你應該將設置TextView值的代碼移動到bindView中)。 – Mike 2011-02-14 22:21:38