1

I have successfully implemented lazy loading for the listview. Everything is alright; loading of images and text. I have 2 piece of information for text; Name and description. I am able to display flawlessly the image and name. But once I try to include the description in it, it seems alright until a certain position. Program is forced closed at that position. Can't figure out what is causing it to close. Any idea what could be the cause? Results displayed on logcat only prints till about half of total number of items to be print.

Program is able to retrieve all images and display them correctly. This is for sure. But having problem retrieving the whole piece of text content. It only retrieves half of the total text available.

I am using the example from here.

LazyAdapter.java

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private String[] data;
private String[] text;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, String[] d, String[] t) {
    activity = a;
    data=d;
    text = t;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.length;
}    

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder{
    public TextView text;
    public ImageView image;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;
    if(convertView==null){
        vi = inflater.inflate(R.layout.item, null);
        holder=new ViewHolder();
        holder.text=(TextView)vi.findViewById(R.id.text);;
        holder.image=(ImageView)vi.findViewById(R.id.image);
        vi.setTag(holder);
    }
    else
        holder=(ViewHolder)vi.getTag();

    holder.text.setText(text[position]);
    holder.image.setTag(data[position]);
    imageLoader.DisplayImage(data[position], activity, holder.image);
    return vi;
}
}

Error exception in logcat is shown below.

ERROR/AndroidRuntime(270): java.lang.ArrayIndexOutOfBoundsException
ERROR/AndroidRuntime(270):     at com.lazy.LazyAdapter.getView(LazyAdapter.java:59)
ERROR/AndroidRuntime(270):     at android.widget.AbsListView.obtainView(AbsListView.java:1294)
ERROR/AndroidRuntime(270):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
ERROR/AndroidRuntime(270):     at android.widget.ListView.fillDown(ListView.java:652)
ERROR/AndroidRuntime(270):     at android.widget.ListView.fillGap(ListView.java:623)
ERROR/AndroidRuntime(270):     at android.widget.AbsListView.trackMotionScroll    (AbsListView.java:2944)
ERROR/AndroidRuntime(270):     at android.widget.AbsListView$FlingRunnable.run   (AbsListView.java:2485)
ERROR/AndroidRuntime(270):     at android.os.Handler.handleCallback(Handler.java:587)
ERROR/AndroidRuntime(270):     at android.os.Handler.dispatchMessage(Handler.java:92)
ERROR/AndroidRuntime(270):     at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(270):     at android.app.ActivityThread.main(ActivityThread.java:4627)
ERROR/AndroidRuntime(270):     at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(270):     at java.lang.reflect.Method.invoke(Method.java:521)
ERROR/AndroidRuntime(270):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
ERROR/AndroidRuntime(270):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
ERROR/AndroidRuntime(270):     at dalvik.system.NativeStart.main(Native Method)
5
  • Have you looked at logcat? What does it say? Commented Dec 12, 2011 at 7:41
  • I printed my results on logcat and everything is fine. Retrieved results are perfectly correct. But error shows - 07:42:42.230: ERROR/AndroidRuntime(270): java.lang.ArrayIndexOutOfBoundsException, 07:42:42.230: ERROR/AndroidRuntime(270): at tp.edu.sg.LazyAdapter.getView(LazyAdapter.java:59) Commented Dec 12, 2011 at 7:46
  • does the problem appear when you scroll to most bottom item? Commented Dec 12, 2011 at 7:50
  • "But error shows..." - So why didn't you put that in your question as well as the code which is causing that exception? You asked "Any idea what could be the cause?", the answer is NO. Unless you show code and exceptions, nobody can help you. If you want an answer then you need to provide a good question. Commented Dec 12, 2011 at 7:51
  • Marek, It only appears after I reach a certain position. About halfway through. MisterSquonk, My bad on the asking of question. I have included more info about my problem Commented Dec 12, 2011 at 8:20

2 Answers 2

2

The array or the array list you are sending to the adapter might be from some other activity, the list should have been initialized outside of onCreate() of that Activity so it's not getting renewed every time you load your list.

So probable solution is initialize it inside onCreate. For example:

public static List<ClassObject> myList;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_event_main);
    myList= new ArrayList<ClassObject>();
    <b>/***Then set the adapter******/</b>

    ListView lv;
    lv = (ListView)findViewById(R.id.cem_listview);
    adapter = new LazyAdapter(MyActivity.this, R.layout.MyActivity,myList);
    lv.setAdapter(adapter);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Apparently you goofed with dereferencing some array / array list in your datapter at LazyAdapter.java:59 . In case this was last entry, you have most probably off-by-one error

Writing and executing meaningful unit tests would save you from this condition.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.