Azarnoy Azarnoy - 4 months ago 18
Android Question

Android can`t get images from api with picasso

I'm trying to get a picture of the api and put them in a list , but when I`m write picasso, Picasso.with field (context) context emphasizes the red line , I do not where it should be ? Tell me please.

Adapter:

class MyAdapter extends ArrayAdapter<Places> {


public MyAdapter(Context context, List<Places> objects) {
super(context, R.layout.list_item, objects);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.nameOfPlace = (TextView) rowView.findViewById(R.id.name_Id);
holder.subcategory_name = (TextView) rowView.findViewById(R.id.subcategory_Id);
holder.geometryName = (TextView) rowView.findViewById(R.id.geometry_Id);
holder.imageView = (ImageView) rowView.findViewById(R.id.imageView);
holder.imageView2 = (ImageView) rowView.findViewById(R.id.imageView2);
holder.rating = (TextView) rowView.findViewById(R.id.rating_Id);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}

Places places = getItem(position);
holder.nameOfPlace.setText(places.getName());
holder.subcategory_name.setText(places.getSubcategory_name());
holder.geometryName.setText(places.getGeometry_name());

holder.imageView2.setImageResource(R.drawable.love_5033);
holder.rating.setText("Рейтинг: " + places.getRating() + " В избранном: " + places.getFavorite());

//There is Picasso
Picasso.with(context).load(places.getCsv_image()) .into(holder.imageView);



return rowView;
}

class ViewHolder {

public TextView nameOfPlace;
public TextView subcategory_name;
public TextView geometryName;
public TextView rating;
public ImageView imageView;
public ImageView imageView2;
}
}


Places class:

public class Places implements Serializable {


String name;
String geometry_name;
String rating;
String subcategory_name;
String favorite;
String csv_image;

public Places(String name, String geometry_name, String rating,String subcategory_name, String favorite,String csv_image) {
this.name = name;
this.geometry_name = geometry_name;
this.rating = rating;
this.subcategory_name = subcategory_name;
this.favorite = favorite;
this.csv_image = csv_image;

}



public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}


public String getGeometry_name() {
return geometry_name;
}

public void setGeometry_name(String geometry_name) {
this.geometry_name = geometry_name;
}

public String getRating() {
return rating;
}

public void setRating(String rating) {
this.rating = rating;
}

public String getSubcategory_name() {
return subcategory_name;
}

public void setSubcategory_name(String subcategory_name) {
this.subcategory_name = subcategory_name;
}

public String getFavorite() {
return favorite;
}

public void setFavorite(String favorite) {
this.favorite = favorite;
}

public String getCsv_image() {
return csv_image;
}

public void setCsv_image(String csv_image) {
this.csv_image = csv_image;
}
}


Initalize adapter, in success of retrofit:

Retrofit.getPlaces(new Callback<List<Places>>() {
@Override
public void success(final List<Places> places, Response response) {


FileOutputStream fos = null;
try {
fos = openFileOutput("countries_file", Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(places);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


listView.setAdapter(new MyAdapter(MainActivity.this, places));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent mIntent = new Intent(MainActivity.this, MapActivity.class);
Places plasez = places.get(position);
mIntent.putExtra("key", plasez);
startActivity(mIntent);
}
});


}

@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();



}
});




}


Full MainActivity:

public class MainActivity extends AppCompatActivity {

final String LOG_TAG = "myLogs";

ListView listView;

TextView name;
TextView subcategory_name;
TextView geometry_name;
TextView rating;




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

name = (TextView) findViewById(R.id.name_Id);
subcategory_name = (TextView) findViewById(R.id.subcategory_Id);
geometry_name = (TextView) findViewById(R.id.geometry_Id);
rating = (TextView) findViewById(R.id.rating_Id);
listView = (ListView)findViewById(R.id.listVieww);




FileInputStream fis;
try {
fis = openFileInput("countries_file");
ObjectInputStream ois = new ObjectInputStream(fis);
final ArrayList<Places> returnlist = (ArrayList<Places>) ois.readObject();
ois.close();
listView.setAdapter(new MyAdapter(this, returnlist));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent mIntent = new Intent(MainActivity.this, MapActivity.class);
Places placez = returnlist.get(position);
mIntent.putExtra("key", placez);
startActivity(mIntent);

}
});
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}





Retrofit.getPlaces(new Callback<List<Places>>() {
@Override
public void success(final List<Places> places, Response response) {
Log.d(LOG_TAG, "получили данные");

FileOutputStream fos = null;
try {
fos = openFileOutput("countries_file", Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(places);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


listView.setAdapter(new MyAdapter(MainActivity.this, places));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent mIntent = new Intent(MainActivity.this, MapActivity.class);
Places plasez = places.get(position);
mIntent.putExtra("key", plasez);
startActivity(mIntent);
}
});


}

@Override
public void failure(RetrofitError error) {
Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();



}
});




}




class MyAdapter extends ArrayAdapter<Places> {
private Context context;




public MyAdapter(Context context, List<Places> objects) {
super(context, R.layout.list_item, objects);


}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.nameOfPlace = (TextView) rowView.findViewById(R.id.name_Id);
holder.subcategory_name = (TextView) rowView.findViewById(R.id.subcategory_Id);
holder.geometryName = (TextView) rowView.findViewById(R.id.geometry_Id);
holder.imageView = (ImageView) rowView.findViewById(R.id.imageView);
holder.imageView2 = (ImageView) rowView.findViewById(R.id.imageView2);
holder.rating = (TextView) rowView.findViewById(R.id.rating_Id);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}

Places places = getItem(position);
holder.nameOfPlace.setText(places.getName());
holder.subcategory_name.setText(places.getSubcategory_name());
holder.geometryName.setText(places.getGeometry_name());

holder.imageView2.setImageResource(R.drawable.love_5033);
holder.rating.setText("Рейтинг: " + places.getRating() + " В избранном: " + places.getFavorite());
Picasso.with(context).load(places.getCsv_image()) .into(holder.imageView);


return rowView;
}

class ViewHolder {

public TextView nameOfPlace;
public TextView subcategory_name;
public TextView geometryName;
public TextView rating;
public ImageView imageView;
public ImageView imageView2;

}
}

}

Answer
private Context context;

public MyAdapter(Context context, List<Places> objects) {
super(context, R.layout.list_item, objects);
this.context = context;}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View rowView = convertView;
    if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.list_item, parent, false);
        holder = new ViewHolder();
        holder.nameOfPlace = (TextView) rowView.findViewById(R.id.name_Id);
        holder.subcategory_name = (TextView) rowView.findViewById(R.id.subcategory_Id);
        holder.geometryName = (TextView) rowView.findViewById(R.id.geometry_Id);
        holder.imageView = (ImageView) rowView.findViewById(R.id.imageView);
        holder.imageView2 = (ImageView) rowView.findViewById(R.id.imageView2);
        holder.rating = (TextView) rowView.findViewById(R.id.rating_Id);
        rowView.setTag(holder);
    } else {
        holder = (ViewHolder) rowView.getTag();
    }

    Places places = getItem(position);
    if (!TextUtils.isEmpty(places.getCsv_image())) {
            Picasso.with(context).into(holder.imageView)
                    .load(places.getCsv_image());}

    return rowView;
}
Comments