Custom Listview with Picasso

0

Well guys, I already did this topic ~ > Image and Quantity Listview - Android

They gave me some tips and now I'm redoing it as they told me.

I made a custom adapter for the listview.

public class CustomAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private final String nome;
    private final Integer imagem;

    public CustomAdapter(Activity context, String nome, Integer imagem) {
        super(context, R.layout.list_picasso, nome);
        this.context = context;
        this.nome = nome;
        this.imagem = imagem;
    }

    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.list_picasso, null,true);

        TextView txtTitle = (TextView) rowView.findViewById(R.id.nome);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.imagem);

        txtTitle.setText(nome);
        Picasso.with(getContext())
            .load(imagem)
            .into(imageView);

        return rowView;
    }
}

Here's where I try to use:

public class PicassoTest extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picassotest);

        DatabaseAccess.getInstance(getApplicationContext()).open();
        List<String> names = DatabaseAccess.getInstance(getApplicationContext()).getTest();
        DatabaseAccess.getInstance(getApplicationContext()).close();

        ListView listView1 = (ListView)findViewById(R.id.Lista);

        CustomAdapter adapter =  new CustomAdapter(getApplicationContext(), names)
        listView1.setAdapter(adapter);
    }
}

And this is the query:

public List<String> getTest() {
    List<String> list = new ArrayList<>();
    Cursor cursor = database.rawQuery("SELECT nome, imagem FROM skin", null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        list.add(cursor.getString(0));
        list.add(cursor.getString(1));
        cursor.moveToNext();
    }
    cursor.close();
    return list;
}

The problems: In custom Adapter on this line:

super(context, R.layout.list_picasso, nome);

Give the error     'can not solve super method (Activit, int, string)'

and PicassoTest :

CustomAdapter adapter =  new CustomAdapter(getApplicationContext(), names);

The context and name does not return, just the image.

How can I resolve this?

    
asked by anonymous 02.02.2017 / 16:03

3 answers

0

The reason for the errors.

First one to which the error refers:

  

can not solve super method (Activit, int, string)

super refers to the constructor of the inherited class, in this case an ArrayAdapter. | ArrayAdapter does not have any constructor with this signature.

Second, you are instantiating CustomAdapter with only 2 arguments

new CustomAdapter(getApplicationContext(), names) 

while declaring the constructor with 3

public CustomAdapter(Activity context, String nome, Integer imagem)

Besides you are passing arguments of type Context and List when you expect the Activity and String types in the first two parameters.

How to solve.

From what I understand, what you want is:

  • Display a list of names with their images
  • The names and url of the image are in the database.
  • The Adapter is responsible for obtaining the images using the Picasa Api.

First you need to have a class that encapsulates the name and url :

public class Skin{

    private String nome;
    private String url;

    public Skin(String nome, String url){
        this.nome = nome;
        this.url = url;
    }

    //getters

}

The getTest() method should return a list of Skins:

public List<Skin> getTest() {
    List<Skin> list = new ArrayList<>();
    Cursor cursor = database.rawQuery("SELECT nome, imagem FROM skin", null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        list.add(new Skin(cursor.getString(0), cursor.getString(1));
        cursor.moveToNext();
    }
    cursor.close();
    return list;
}

The Adapter will receive this list in the constructor and will use it to populate the views of each ListView item:

public class CustomAdapter extends ArrayAdapter<Skins> {

    private final Context context;
    private final List<Skin> skins;

    public CustomAdapter(Context context, List<Skin> skins) {
        super(context, R.layout.list_picasso, skins);
        this.context = context.getApplicationContext();
        this.Skins = skin;
    }

    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.list_picasso, parent, false);

        TextView txtTitle = (TextView) rowView.findViewById(R.id.nome);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.imagem);
        Skin skin = skins.get(position);

        txtTitle.setText(skin.getNome());
        Picasso.with(context)
            .load(skin.getUrl())
            .into(imageView);

        return rowView;
    }
}

Create the adapter as follows:

....
....
DatabaseAccess.getInstance(getApplicationContext()).open();
List<Skins> skins = DatabaseAccess.getInstance(getApplicationContext()).getTest();
DatabaseAccess.getInstance(getApplicationContext()).close();

ListView listView1 = (ListView)findViewById(R.id.Lista);
CustomAdapter adapter =  new CustomAdapter(this, skins);

listView1.setAdapter(adapter);
....
    
02.02.2017 / 16:44
0

You are declaring the layout of the item twice.

The correct one is just here:

View rowView = inflater.inflate(R.layout.list_picasso, null,true);

Up on the line:

super(context, R.layout.list_picasso, nome);

Change this R.layout.list_picasso by image

It looks like this:

super(context, imagem, nome);
    
02.02.2017 / 16:07
0

In your activity you are declaring the adapter with 2 parameters:

CustomAdapter adapter = new CustomAdapter(getApplicationContext(), names)

Its counter has three parameters:

public CustomAdapter(Activity context, String nome, Integer imagem) { 
    super(context, R.layout.list_picasso, nome); 
 .....
}
    
02.02.2017 / 16:21