我有 SQLite DB,我正在尝试在 listView 中显示查询值。我按照本教程为 listView 创建了一个自定义适配器。也用这个问题作为参考。
我无法看到我的 listView 上显示的任何数据。
自定义 listView 行应该有 3 个项目 - 描述、日期和金额(在此xml 中定义的视图)。这些值是从数据库中获取的。从 db 获取看起来不错。
我的自定义适配器代码:
public class TransactionListAdapter extends BaseAdapter {
Context context;
ArrayList<TransactionItem> transactionList;
// Code for TransactionItem class : https://gist.github.com/bharatkrishna/5175105
public TransactionListAdapter(Context context, ArrayList<TransactionItem> list) {
this.context = context;
transactionList = list;
}
@Override
public int getCount() {
return transactionList.size();
}
@Override
public Object getItem(int position) {
return transactionList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("adpater", "I am in adapter"); // Doesn't seem to get to this point as I don't see the log message
View element;
TransactionItem transactionItem = transactionList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
element = inflater.inflate(R.layout.row_entry, null);
} else {
element = convertView;
}
// Code of XML file with these widgets defined: https://gist.github.com/bharatkrishna/5175158
TextView descTextView = (TextView) convertView.findViewById(R.id.descText_Report);
descTextView.setText(transactionItem.getDescription());
TextView dateTextView = (TextView) convertView.findViewById(R.id.dateText_Report);
dateTextView.setText(transactionItem.getDate());
TextView amountTextView = (TextView) convertView.findViewById(R.id.amountText_Report);
amountTextView.setText(Float.toString(transactionItem.getAmount()));
return convertView;
}
}
我在我的活动中使用这个适配器如下:
public class ReportsActivity extends Activity {
ListView listView;
private DatabaseHelper db;
private Cursor transactions;
...
...
private void showList() {
ArrayList<TransactionItem> transactionList = new ArrayList<TransactionItem>();
db = new DatabaseHelper(this);
transactions = db.getTransaction();
// This successfully dumps the db contents to log output. Querying from DB seems to be working fine.
Log.d("db", DatabaseUtils.dumpCursorToString(transactions));
if (transactions != null && transactions.getCount() != 0) {
do {
TransactionItem transactionItem = new TransactionItem();
transactionItem.setDescription(transactions.getString(transactions.getColumnIndex("description")));
transactionItem.setDate(transactions.getString(transactions.getColumnIndex("date")));
transactionItem.setAmount(transactions.getFloat(transactions.getColumnIndex("amount")));
} while (transactions.moveToNext());
}
TransactionListAdapter transactionListAdapter = new TransactionListAdapter(ReportsActivity.this, transactionList);
listView.setAdapter(transactionListAdapter); //listView is defined in onCreate() method
}
}
我哪里错了。
谢谢。
编辑:更改了 TransactionListAdapter 的 getView() 方法的代码,摆脱了 else 情况。我正在更改代码并在此处粘贴了错误代码。这是@James Baca 发现的。我现在有:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TransactionItem transactionListItems = transactionList.get(position);
Log.d("adpater", "I am in adapter");
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_entry, null);
}
TextView descTextView = (TextView) convertView.findViewById(R.id.descText_Report);
descTextView.setText(transactionListItems.getDescription());
TextView dateTextView = (TextView) convertView.findViewById(R.id.dateText_Report);
dateTextView.setText(transactionListItems.getDate());
TextView amountTextView = (TextView) convertView.findViewById(R.id.amountText_Report);
amountTextView.setText(Float.toString(transactionListItems.getAmount()));
return convertView;
}