I’m not able to exbir a listview that uses an Arrayadapter<String>

Asked

Viewed 199 times

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. However I am 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 fill Activity that is not showing 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 who 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;
    }


}

Note: Before I created the class Arrayadapter worked well, but I had to create the Arrayadapter because I will need to use the getView().

Note 2: I put one adapter.getCount() now and found that the Adapter is really empty, but print the item, with the adapter.getItem(x);

Note: I am calling the Arrayadapter class in the correct way?

2 answers

4


The problem is in 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 it should return a View which will be the item of your ListView, then within your method getView 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 must implement the methods add and getItem also. In 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 don’t need to implement the method, or don’t @Override or always call the code super class, this way: super.METODO

1

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

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

Try returning the size of your String Array instead:

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

And also in the getItemId method 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 position of the item itself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.