I am pretty new to android studio and still learning from video tutorials. I found Picasso and want to use it for my project but when i tried to apply/practice in a listview i dont seem to get it, im having an error that say Target must not be null. Ive tried the solutions around but still nothing. Sorry for my bad english. here is my code please help me.
class MyAdapter extends ArrayAdapter<String> {
Context context;
public MyAdapter(Context context, int row_layout, String[] values) {
super(context, R.layout.row_layout2, values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageHolder holder = null;
holder = new ImageHolder();
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(R.layout.row_layout2, parent, false);
String tvShow = getItem(position);
TextView theTextView = (TextView) theView.findViewById(R.id.textView1);
theTextView.setText(tvShow);
holder.imageIcon = (ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(holder);
Picasso.with(this.context).load(getItem(R.drawable.sample4)).into(holder.imageIcon); //ERROR HERE
return theView;
}
static class ImageHolder
{
ImageView imageIcon;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
static class ImageHolder {
ImageView imageIcon;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] favoriteShows = {
"Hannibal", "Sherlock", "Supernatural"
};
//ImageHolder holder = new ImageHolder();
ListAdapter theAdapter = new MyAdapter(this, R.layout.row_layout, favoriteShows);
ListView theListView = (ListView) findViewById(R.id.theListView);
theListView.setAdapter(theAdapter);
theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
String tvShowPicked= "You selected " +
String.valueOf(adapterView.getItemAtPosition(position));
Toast.makeText(MainActivity.this, tvShowPicked, Toast.LENGTH_SHORT).show();
}
});
Ive alse tried to put the Picasso lines in MainActivity to see if anything changes.
holder.imageIcon = (ImageView) findViewById(R.id.imageView1);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
Picasso.with(this)
.load(R.drawable.sample4)
.into(holder.imageIcon);
Aucun commentaire:
Enregistrer un commentaire