Trying to override getView in a SimpleCursorAdapter gives NullPointerExceptio

后端 未结 3 1388
滥情空心
滥情空心 2020-12-09 19:29

Would very much appreciate any help or hint on were to go next.

I\'m trying to change the content of a row in ListView programmatically. In one row there are 3 TextV

3条回答
  •  没有蜡笔的小新
    2020-12-09 20:06

    Actually you should use a ViewBinder to achieve your custom binding. This way you do not have to override any code. Just make sure that you return true when you have set the value of the view otherwise the adapter will overwrite the value.

    public void refresh() {
        Cursor cursor = ....get cursor....
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
                R.layout.list_item, 
                cursor, 
                new String[] { KEY_NAME, KEY_SHORT_DESC}, 
                new int[] { R.id.icon, R.id.text1});
        adapter.setViewBinder(new ProductViewBinder());
        setAdapter(adapter);
    }
    
    private static class ProductViewBinder implements ViewBinder {
    
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            if (view instanceof ProgressBar) {
                String result = cursor.getString(2);
                Int resInt = Int.parseInt(result);
    
                if (resInt == 0) {
                    ((ProgressBar)view).setIndeterminate(true);
                    return true;
                }
            }
    
            return false;
        }
    }
    

提交回复
热议问题