Trying to override getView in a SimpleCursorAdapter gives NullPointerExceptio

后端 未结 3 1379
滥情空心
滥情空心 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 19:57

    For CursorAdapter and subclasses, you should override newView() and bindView() instead of getView().

    More importantly, though, you should not get calling super.getView(). That is where you are crashing.

    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
  • 2020-12-09 20:19

    There is no need for getView() when extending the SimpleCursorAdapter class. All the functionality of getView() we get from the overriden newView() and bindView() methods.

    if (convertview == null) // is equal to `newView()` and
    if (convertview != null) // is equal to `bindView()`
    

    One main difference is in getView(); we have the position as a parameter and in simpleCursorAdapter we get the position as getPositionForView(view) view is a parameter of bindView().

    example: Mostly we are using Listview to put values...so you call ListViewObj.getPositionForView(view)

    0 讨论(0)
提交回复
热议问题