我有SQLite DB,我试图在listView中显示查询的值。 我按照this教程为listView创建了一个自定义适配器。还使用了this个问题作为参考。
我无法在listView上看到任何数据。
自定义listView行应该包含3个项目 - 描述,日期&金额(视图在this xml中定义)。这些值是从db中获取的。从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()方法的代码,摆脱了其他情况。我正在改变代码和在这里粘贴了一个有缺陷的代码。 @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;
}
答案 0 :(得分:2)
试试这个
您尚未将交易对象设置为列表,因此您的列表为空
添加此
transactionList.add(transactionItem);
在showList()
方法
答案 1 :(得分:2)
我认为你应该将convertview替换为element .. like this
TextView descTextView = (TextView) element.findViewById(R.id.descText_Report);
descTextView.setText(transactionItem.getDescription());
TextView dateTextView = (TextView) element.findViewById(R.id.dateText_Report);
dateTextView.setText(transactionItem.getDate());
TextView amountTextView = (TextView) element.findViewById(R.id.amountText_Report);
amountTextView.setText(Float.toString(transactionItem.getAmount()));
return element;
答案 2 :(得分:1)
当convertView为null时,你有一个convertView.findViewById()的错误,这可能会失败。首次使用适配器并且最初没有传入convertView时会发生这种情况。
我建议:
@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
TransactionItem transactionItem = transactionList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_entry, null);
}
// 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;
}