I have found a code for a listview with images on this page: http://cursoandroidstudio.blogspot.com.es/2014/07/listview-con-imagenes.html I need it to work with bitmap type, I have changed everything of the type int[] to Bitmap[] . The result is as follows:
public class MainActivity extends AppCompatActivity {
ListViewAdapter adapter;
String[] titulo = new String[]{
"titulo1",
"titulo2",
"titulo3",
"titulo4",
};
Bitmap[] imagenes = {
BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher),
BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher),
BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher),
BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView lista = (ListView) findViewById(R.id.listView1);
adapter = new ListViewAdapter(this, titulo, imagenes);
lista.setAdapter(adapter);
}
}
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
Context context;
String[] titulos;
Bitmap[] imagenes;
LayoutInflater inflater;
public ListViewAdapter(Context context, String[] titulos, Bitmap[] imagenes) {
this.context = context;
this.titulos = titulos;
this.imagenes = imagenes;
}
@Override
public int getCount() {
return titulos.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView txtTitle;
ImageView imgImg;
//http://developer.android.com/intl/es/reference/android/view/LayoutInflater.html
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.list_row, parent, false);
// Locate the TextViews in listview_item.xml
txtTitle = (TextView) itemView.findViewById(R.id.list_row_title);
imgImg = (ImageView) itemView.findViewById(R.id.list_row_image);
// Capture position and set to the TextViews
txtTitle.setText(titulos[position]);
imgImg.setImageBitmap(imagenes[position]);
return itemView;
}
}
But it doesn't work for me, does anyone know why or how I can fix it?
Remove the array you declared since you need the context at that point:
and declare your bitmap array like this: