0

I am using EndlessAdapter from commonsguy with a SimpleAdapter. I can load data when I make a scroll down without problems, but I have a NullPointerException when I make a scroll up. The problem is in the method

    @Override
 public View getView(int position, View convertView,ViewGroup parent) {
  return wrapped.getView(position, convertView, parent);
 }

from the class AdapterWrapper.

The call to wrapped.getView(position, convertView,parent) raises the exception and I don´t know why.

This is my implementation of EndlessAdapter:

//Inner class in SearchTextActivity
    class DemoAdapter extends EndlessAdapter {

      private RotateAnimation rotate = null;

      DemoAdapter(ArrayList<HashMap<String, String>> result) {
       super(new SimpleAdapter(SearchTracksActivity.this,
         result,
         R.layout.textlist_item,
         PROJECTION_COLUMNS,
         VIEW_MAPPINGS));

       rotate = new RotateAnimation( 0f,
         360f,
         Animation.RELATIVE_TO_SELF,
         0.5f,
         Animation.RELATIVE_TO_SELF,
         0.5f);
       rotate.setDuration(600);
       rotate.setRepeatMode(Animation.RESTART);
       rotate.setRepeatCount(Animation.INFINITE);
      }

      @Override
      protected View getPendingView(ViewGroup parent) {
       View row=getLayoutInflater().inflate(R.layout.textlist_item, null);

       View child=row.findViewById(R.id.title);
       child.setVisibility(View.GONE);

       child=row.findViewById(R.id.username);
       child.setVisibility(View.GONE);

       child=row.findViewById(R.id.throbber);
       child.setVisibility(View.VISIBLE);
       child.startAnimation(rotate);

       return row;
      }


      @Override
      @SuppressWarnings("unchecked")
      protected void rebindPendingView(int position, View row) {
       HashMap<String, String> res = (HashMap<String, String>)getWrappedAdapter().getItem(position);


       View child=row.findViewById(R.id.title);
       ((TextView)child).setText(res.get("title"));
       child.setVisibility(View.VISIBLE);    

       child=row.findViewById(R.id.username);
       ((TextView)child).setText(res.get("username"));
       child.setVisibility(View.VISIBLE);

       ImageView throbber=(ImageView)row.findViewById(R.id.throbber);
       throbber.setVisibility(View.GONE);
       throbber.clearAnimation();

      }

      boolean mFinal = true;

      @Override
      protected boolean cacheInBackground() {

       EditText searchText = (EditText)findViewById(R.id.searchText);
       String textToSearch = searchText.getText().toString();

       Util.getSc().searchText(textToSearch , offset, limit, new ResultListener<ArrayList<Text>>() {

        @Override
        public void onError(Exception e) {
         e.toString();
         mFinal = false;
        }

        @Override
        public void onSuccess(ArrayList<Text> result) {


         if(result.size() == 0){
          mFinal = false;
         }else{
          texts.addAll(result);

          offset++;
         }
        }
       });

       return mFinal;
      }

      @Override
      protected void appendCachedData() {

       for(Text text : texts){
        result.add(text.getMapValues());
       }
       texts.clear();
      }
     }

And I use it this way:

public class SearchTextActivity extends AbstractListActivity {

 private static final String[] PROJECTION_COLUMNS = new String[] {
  TextStore.Text.TITLE,
  TextStore.Text.USER_NAME};

 private static final int[] VIEW_MAPPINGS = new int[] {
  R.id.Text_title,
  R.id.Text_username};

 ArrayList<HashMap<String, String>> result;

 static ArrayList<Text> texts;
 static int offset = 0;
 static int limit = 1;

 @Override
 void onAbstractCreate(Bundle savedInstance) {
  setContentView(R.layout.search_tracks);
  setupViews();
 }

 private void setupViews() {

  ImageButton searchButton = (ImageButton)findViewById(R.id.searchButton);
  updateView();
 }

 SimpleAdapter adapter;

 void updateView(){
  if(result == null) {
   result = new ArrayList<HashMap<String, String>>();
  }

  if(tracks == null) {
   texts = new ArrayList<Text>();
  }
 }

 public void sendQuery(View v){
  offset = 0;
  texts.clear();
  result.clear();

  setListAdapter(new DemoAdapter(result));
 }
}

Does anybody knows what could be the problem?

tshepang
  • 12,111
  • 21
  • 91
  • 136
ahmontero
  • 351
  • 1
  • 6
  • 12
  • I have not tried SimpleAdapter with EndlessAdapter. If the `NullPointerException` is directly on the `wrapped.getView(position, convertView,parent)` line, then `wrapped` must be `null`. – CommonsWare Mar 25 '10 at 23:14
  • Thanks for your response, but I have checked that 'wrapped' is not null. The error comes from wrapped.getView(position, convertView,parent). It is inside the SimpleAdapter. Maybe is a problem related to the index 'position', but there is a View on this position. I don´t know what is happening there. – ahmontero Mar 26 '10 at 09:16

1 Answers1

1

It is inside the SimpleAdapter.

I would start by getting rid of the EndlessAdapter and just have your list be on the SimpleAdapter.

If that works, then there may be a bug in the way EndlessAdapter works with the adapter it wraps -- in that case, I would need a sample project that demonstrates the error.

If the SimpleAdapter fails without the EndlessAdapter, you will need to do more investigating of how you are setting up the SimpleAdapter.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I have been testing and I have been able to recreate the error. If I use the SimpleAdpater everything is ok, but if I use EndlessAdapter with SimpleAdapter, I have the problem before described. I have made a demo project in Eclipse with the error here: http://bit.ly/alTapV Thanks for your help – ahmontero Mar 26 '10 at 23:08
  • `EndlessAdapter` will not work with `SimpleAdapter` any time soon. I do not recommend `SimpleAdapter`, period. There is no problem that `SimpleAdapter` solves that `ArrayAdapter` cannot solve more efficiently (less data copying = less CPU time, less garbage collection, etc.). – CommonsWare Mar 27 '10 at 00:15
  • Ok, I understand. Then I will try to use ArrayAdapter. Thanks for your time. – ahmontero Mar 27 '10 at 12:03