0

我有 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;
}
4

3 回答 3

2

试试这个

您尚未将交易对象设置为列表,因此您的列表为空

添加这个

transactionList.add(transactionItem);

showList()在你的方法的while循环中

于 2013-03-16T06:44:20.453 回答
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;
于 2013-03-16T07:44:38.393 回答
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;
}
于 2013-03-16T06:31:34.890 回答