Dans mon activité, j'implémente une liste contenant les noms de certains fichiers. Chaque élément de la liste fait référence à une mise en page dans laquelle je souhaite afficher le nom des images et une vignette de l'image référencée. Je peux montrer le nom en utilisant un ArrayAdapter mais je ne sais pas trop comment insérer la vignette de l'image. Toutes les images référencées restent dans sd_card et j'ai le chemin d'accès. Voici l'unique disposition des lignes:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="50dp"
Android:orientation="horizontal" >
<ImageView
Android:id="@+id/imageView"
Android:layout_width="36dp"
Android:layout_height="wrap_content"
Android:src="@drawable/btn_nav_background_default" />
<TextView
Android:id="@+id/titoloTv"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
et la mise en page de l'activité
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="vertical" >
<Button
Android:id="@+id/creaButton"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center_horizontal"
Android:text="Crea una nuova realtà aumentata" />
<TextView
Android:id="@+id/textView1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:layout_gravity="center_horizontal"
Android:text="Ar già create"
Android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
Android:id="@+id/listView1"
Android:layout_width="match_parent"
Android:layout_height="match_parent" >
</ListView>
</LinearLayout>
et l'adaptateur inhérent au code d'activité.
ArrayAdapter<?> arrayAdapter = new ArrayAdapter<String>(this,R.layout.row,R.id.titoloTv,targetName);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, final View componente, int pos, long id){
...................
}
});
J'essaie de l'utiliser mais cela ne fonctionne pas correctement.
String tempTarget;
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
for(int i = 0; i<ARelements.size();i++){
Element ar = arIterator.next();
Map<String,Object> map = new HashMap<String,Object>(2);
tempTarget = ar.getAttributeValue("TARGET");
thumbnailBitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(tempTarget), THUMBSIZE, THUMBSIZE);
map.put("thumbnail", thumbnailBitmap);
map.put("titolo", tempTarget);
data.add(map);
}
arIterator= null;
SimpleAdapter simpleAdapter = new SimpleAdapter(this,data,R.layout.row,new String[] {"thumbnail","titolo"},new int[] {R.id.imageView, R.id.titoloTv});
listView.setAdapter(simpleAdapter);
Vous devez implémenter un adaptateur multidisque personnalisé et spécifier cette disposition/id (s) imageview et textview dans l'adaptateur.
Quelque chose comme:
public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
public CustomListViewAdapter(Context context, int resourceId, //resourceId=your layout
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
}
Et alors:
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.show();
}
Reportez-vous également à l'extension d'autres adaptateurs pour ListView (s) et leur implémentation personnalisée:
Par exemple http://theopentutorials.com/tutorials/Android/listview/Android-custom-listview-with-image-and-text-using-arrayadapter/
Vous n'avez pas nécessairement besoin d'un ArrayAdapter personnalisé, un SimpleAdapter fera l'affaire pour un combo miniature/texte. Vous pouvez stocker le chemin d'accès à la vignette et le texte souhaité dans un tableau HashMap, puis l'appliquer à l'aide d'un SimpleAdapter. Par exemple, si vous avez 5 vignettes + texte et tableaux de chaque:
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 5; i++) {
Map<String, Object> datum = new HashMap<String, Object>(2);
datum.put("thumbnail", thumbnail[i]);
datum.put("name", text[i]);
data.add(datum);
}
YourListViewId.setAdapter(new SimpleAdapter(this, data, R.layout.yoursinglerowlayout, new String[] {"thumbnail","name"}, new int[] {R.id.imageView, R.id.titoloTv}));
Premièrement: vous devrez changer votre ArrayAdapter en BaseAdapter car BaseAdapter vous donne plus de flexibilité avec les vues. Essayez d'utiliser ce code et voyez comment cela fonctionne:
Voici comment obtenir des images de la carte SD et les insérer dans une liste
public class GetSdCardContent extends Activity {
public static Cursor cursor;
private int columnIndex;
private File file;
private String SD_CARD_ROOT;
ArrayList<String> f = new ArrayList<String>();
File[] listFile;
ImageAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sdcard_layout);
getSdcardImages();
ListView lv1 = (ListView) findViewById(R.id.sdlistView1);
adapter = new ImageAdapter();
lv1.setAdapter(adapter);
}
public void getSdcardImages() {
File file = new File(
Android.os.Environment.getExternalStorageDirectory().getPath(),
"Pictures");
if (file.isDirectory()) {
listFile = file.listFiles();
for (int i = 0; i < listFile.length; i++) {
f.add(listFile[i].getAbsolutePath());
Log.i("FILES:"+"---", f.toString());
}
}
}
Ceci est l'adaptateur que j'utilise. C'est un adaptateur de base et je ne reçois que des images pour l'instant. Vous pouvez également ajouter votre TextView si vous le souhaitez.
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return f.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.custom_listview_items,
null);
holder.imageview = (ImageView) convertView
.findViewById(R.id.customimageView1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
try {
holder.imageview.setImageBitmap(getResizedBitmap(myBitmap, 300, 300));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertView;
}
}
Cette méthode est pour bitmap. Si vous voulez redimensionner votre image.
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
class ViewHolder {
ImageView imageview;
}
}
Dernière partie: Deux mises en page.
One: mise en page pour les éléments.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="vertical" >
<ImageView
Android:id="@+id/customimageView1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:src="@drawable/bluebutton" />
</LinearLayout>
Deuxièmement: Mise en page principale. (ListView)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="vertical" >
<ListView
Android:id="@+id/sdlistView1"
Android:layout_width="match_parent"
Android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
J'espère que cela vous aidera .. :)
Je l'ai résolu en utilisant cette classe
public class ExtendedSimpleAdapter extends SimpleAdapter{
List<HashMap<String, Object>> map;
String[] from;
int layout;
int[] to;
Context context;
LayoutInflater mInflater;
public ExtendedSimpleAdapter(Context context, List<HashMap<String, Object>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
layout = resource;
map = data;
this.from = from;
this.to = to;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return this.createViewFromResource(position, convertView, parent, layout);
}
private View createViewFromResource(int position, View convertView,
ViewGroup parent, int resource) {
View v;
if (convertView == null) {
v = mInflater.inflate(resource, parent, false);
} else {
v = convertView;
}
this.bindView(position, v);
return v;
}
private void bindView(int position, View view) {
final Map dataSet = map.get(position);
if (dataSet == null) {
return;
}
final ViewBinder binder = super.getViewBinder();
final int count = to.length;
for (int i = 0; i < count; i++) {
final View v = view.findViewById(to[i]);
if (v != null) {
final Object data = dataSet.get(from[i]);
String text = data == null ? "" : data.toString();
if (text == null) {
text = "";
}
boolean bound = false;
if (binder != null) {
bound = binder.setViewValue(v, data, text);
}
if (!bound) {
if (v instanceof Checkable) {
if (data instanceof Boolean) {
((Checkable) v).setChecked((Boolean) data);
} else if (v instanceof TextView) {
// Note: keep the instanceof TextView check at the bottom of these
// ifs since a lot of views are TextViews (e.g. CheckBoxes).
setViewText((TextView) v, text);
} else {
throw new IllegalStateException(v.getClass().getName() +
" should be bound to a Boolean, not a " +
(data == null ? "<unknown type>" : data.getClass()));
}
} else if (v instanceof TextView) {
// Note: keep the instanceof TextView check at the bottom of these
// ifs since a lot of views are TextViews (e.g. CheckBoxes).
setViewText((TextView) v, text);
} else if (v instanceof ImageView) {
if (data instanceof Integer) {
setViewImage((ImageView) v, (Integer) data);
} else if (data instanceof Bitmap){
setViewImage((ImageView) v, (Bitmap)data);
} else {
setViewImage((ImageView) v, text);
}
} else {
throw new IllegalStateException(v.getClass().getName() + " is not a " +
" view that can be bounds by this SimpleAdapter");
}
}
}
}
}
private void setViewImage(ImageView v, Bitmap bmp){
v.setImageBitmap(bmp);
}
}