Custom Listview with Picasso

Asked

Viewed 99 times

0

Good guys, I’ve done this topic~>Listview images and quantity - Android

They gave me some tips and now I’m remaking him like they told me.

I made a custom Adapter for 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;
    }
}

This is where I try to wear:

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 along those lines:

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

of the error 'cannot resolve method super(Activit, int, string)'

and in the PicassoTest:

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

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

How can I fix this?

3 answers

0

You are declaring the item layout twice.

The right is only here:

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

Up on the line:

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

Trade this R.layout.list_picasso for image

being like this:

super(context, imagem, nome);
  • continues the same error with this modification.

0

In your Activity you are declaring Adapter with 2 parameters:

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

Your constructor has 3 parameters:

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

0


The reason for the mistakes.

First the one to which the error refers:

cannot resolve method super(Activit, int, string)

super refers to the manufacturer of the inherited class, in this case to an Arrayadapter.
Arrayadapter have no constructor with that 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)

In addition, you are passing arguments of the Context and List type when expected, in the first two parameters, the Activity and String types.

How to solve.

From what I understand, what you want is:

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

First it is necessary to have a class that encapsulates the name and the url:

public class Skin{

    private String nome;
    private String url;

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

    //getters

}

The method getTest() must 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 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);
....
  • as in query returns 2 strings I thought I only needed it. How to solve errors?

  • The table image field what is?

  • a string with the image url

  • after almost 2 weeks trying to do this you solved my problem. Thank you very much, it worked perfectly!

Browser other questions tagged

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