I'm not able to get a listView that uses an ArrayAdapterString

1

I have a main class that inherits from Activity where I have my views, but I needed to create another class that inherits ArrayAdapter. But I'm not able to present my listView, the strange thing is that when you take an item from the list and print it works. Can someone give a help? Thank you in advance!

This method is in my class that inherits Activity. This is the method to populate the activity that is not displaying anything.

//recebo um vetor de String com dados do banco
public void preencheListView(String[] paises) {

        lv = new ListView(this);
        //instancio aqui
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, paises);
        //aqui eu consigo imprimir um pais para teste, o que significa que o adapter está ok
        System.out.println("teste: "+adapter.getItem(1));
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
        linearlayout.addView(lv);

    }
}

This is my class that inherits ArrayAdapter

public class ArrayAdapter extends ArrayAdapter<String> {

    public Context ctx;
    public int rec;
    public String[] paises;



    public ArrayAdapter(Context context, int resource, String[] p) {
        super(context, android.R.layout.simple_list_item_1, p);
        this.ctx = context;
        this.rec = resource;
        this.paises = p;
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        return null;
    }

    public int getRec() {
        return rec;
    }

    public void setRec(int rec) {
        this.rec = rec;
    }

    public Context getCtx() {
        return ctx;
    }

    public void setCtx(Context ctx) {
        this.ctx = ctx;
    }

    public String[] getPaises() {
        return paises;
    }

    public void setPaises(String[] paises) {
        this.paises = paises;
    }


}

Obs2: I put a getView() now and found that the adapter is empty even though I print the item with adapter.getCount()

Obs3: I'm calling the ArrayAdapter class the right way?

    
asked by anonymous 23.09.2015 / 16:14

2 answers

4

The problem is with the method

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
         // TODO Auto-generated method stub
         return null;
    }

It should not return null, but should return a View that will be the item of its ListView , then within its getView method, make the following code:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    return super.getView(position, convertView, parent);
}

You should implement the add and getItem methods as well. This way:

@Override
public void add(String value) {
    super.add(object);
}

@Override
public String getItem(int position) {
    return super.getItem(position);
}
  

Not implementing the methods will also solve your problem.   If you do not need to implement the method, either do not @Override or always call the super class code, like this: super.METODO

    
23.09.2015 / 16:41
1

In your adapter I saw that you are returning 0 in the getCount method in this part of your code:

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

Try instead to return the size of your Strings Array like this:

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

And also in the method getItemId has to return the position that is sent as parameter:

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

So you can return the getCount of your object correctly and also return the Id of each item that is nothing more than the item's own position.

    
23.09.2015 / 16:42