8

I think i'm turning crazy. Something so simple has bind a custom adapter to a Listview is giving me a headache.

Post the code and explain then:

MainActivity.java

package com.example.pruebalist;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    private static String[] data = new String[] {"0","1","2","3"};

    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Log.v("MainActivity","Inside MainActivity");
        setContentView(R.layout.main);
        ListView lstView = (ListView)findViewById(R.id.listNoticias);

        ArrayAdapter<String> adapter = new LstAdapter(this, R.layout.row, data);
        lstView.setAdapter(adapter);
    }

}

LstAdapter.java

package com.example.pruebalist;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class LstAdapter extends ArrayAdapter<String>{

    private String[] mData;
    private Context mContext;
    int layoutResourceId;

    public LstAdapter(Context context, int textViewResourceId, String[] values) {
        super(context, textViewResourceId, values);
        mContext = context;
        mData = values;
        layoutResourceId = textViewResourceId;

        Log.v("LstAdapter","Inside LstAdapter");
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View v = convertView;
        Log.v("LstAdapter","Inside getView");

        if(v==null){
            LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
            v = inflater.inflate(layoutResourceId, parent,false);
        }

        String item = mData[position];
        if(item!=null){
            TextView txtItem = (TextView)v.findViewById(R.id.texto);
            if(txtItem!=null){
                txtItem.setText(item);
            }
        }

        return v;
    }

}

The ListView is never show. And getView is never used, logCat doesn't show "Inside Getview".

What's wrong?

1
  • 1
    Can you post your layout file
    – RvdK
    Commented Aug 20, 2012 at 13:33

4 Answers 4

28

Main problem is

@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

change it to

@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mData.length;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return mData[arg0];
    }

Check out this Vogella Tutorial on List View & List Activity, Try to use ViewHolder in List Adapter it will increase your view performance.

4
  • OMG, thanks! After that i think i'm going to jump through my window Commented Aug 20, 2012 at 13:49
  • 2
    .....I can't believe that I've used this exact method for a custom listview adapter about 50 times and hit this snag that cost me 4 hours of my life.
    – dell116
    Commented Sep 9, 2012 at 17:03
  • I encountered this after I changed my array adapter from using a String[] to using a custom object. While I understand the need to implement these methods, because I hadn't done so before it never occurred to me when refactoring. The documentation mentions overriding toString() and getView(), however, it should also mention getCount() since not doing so causes nothing to display.
    – farnett
    Commented Feb 2, 2015 at 18:13
  • I encountered this issue too and you really save me :)
    – Ralphilius
    Commented Apr 21, 2015 at 9:12
10

This is because you left the default implementation of getCount() method which returns 0, so the Adapter thinks there are no elements to display in ListView.

It should return mData.length

@Override
public int getCount() {
   return mData.length;
}
3

You have not implemented getCount() and getItemId(...).

You can just take out those methods and use the defaults if you do not plan on needing custom implementation for them.

Take them out for now and then try.

2

The right way of doing this is to call super(context, textViewResourceId, values) - which you are already doing. In that case you can remove the getCount() and getItemId(...) functions completely. The class which your List is derrived from will automatically return the correct values.

  • Cheers
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.