ArrayAdapter文字和图片文字、图片、ArrayAdapter

2023-09-04 13:01:59 作者:见过花开的人

在我的活动我实现,其中包含一些文件的名称的列表。 每列表的项目是指一种在其中我想显示的图像的名称和所引用的图像的缩略图的布局。 ICAN使用ArrayAdapter显示的名字,但我不知道热插入的图像的缩略图。 所有的图像引用留在sd_card和我有他们的道路。 这里是单排布局:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =50dp
    机器人:方向=横向>

< ImageView的
    机器人:ID =@ + ID / ImageView的
    机器人:layout_width =36dp
    机器人:layout_height =WRAP_CONTENT
    机器人:SRC =@可绘制/ btn_nav_background_default/>

<的TextView
    机器人:ID =@ + ID / titoloTv
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

< / LinearLayout中>
 

和活动的布局

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>

<按钮
    机器人:ID =@ + ID / creaButton
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =center_horizo​​ntal
    机器人:文本=克雷亚UNA NUOVArealtàaumentata/>

<的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =center_horizo​​ntal
    机器人:文本=氩GIA创造
    机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?

<的ListView
    机器人:ID =@ + ID / ListView1的
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>


< / ListView控件>

< / LinearLayout中>
 
文字壁纸

和c固有适配器活动$ C $。

  ArrayAdapter<> arrayAdapter =新的ArrayAdapter<字符串>(这一点,R.layout.row,R.id.titoloTv,TARGETNAME);
    listView.setAdapter(arrayAdapter);

    listView.setOnItemClickListener(新AdapterView.OnItemClickListener(){
           @覆盖
           公共无效onItemClick(适配器视图<>适配器,最后查看componente,INT POS,长I​​D){

...................
           }

    });
 

我想用这个,但不能正常工作。

 字符串tempTarget;
    列表与LT;地图<字符串,对象>>数据=新的ArrayList<地图<字符串,对象>>();

   的for(int i = 0; I< ARelements.size();我++){
        元件芳= arIterator.next();

        地图<字符串,对象>地图=新的HashMap&所述;串,对象>(2);
        tempTarget = ar.getAttributeValue(目标);
        thumbnailBitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.de codeFILE(tempTarget),THUMBSIZE,THUMBSIZE);
        map.put(缩略图,thumbnailBitmap);
        map.put(TITOLO,tempTarget);
        data.add(图)
    }
   arIterator = NULL;

   SimpleAdapter simpleAdapter =新SimpleAdapter(这一点,数据,R.layout.row,新的String [] {缩略图,TITOLO},新的INT [] {R.id.imageView,R.id.titoloTv});


    listView.setAdapter(simpleAdapter);
 

解决方案

您需要实现一个自定义的数组适配器和指定的ImageView和TextView的布局/ ID在该适配器(S)。

是这样的:

 公共类CustomListViewAdapter扩展ArrayAdapter< RowItem> {


上下文语境;

公共CustomListViewAdapter(上下文的背景下,INT RESOURCEID,// RESOURCEID =布局
        名单< RowItem>项目){
    超(背景下,RESOURCEID,项目);
    this.context =背景;
}

/ *私有视图holder类* /
私有类ViewHolder {
    ImageView的ImageView的;
    TextView的txtTitle;
}

公共查看getView(INT位置,查看convertView,ViewGroup中父){
    ViewHolder支架=无效;
    RowItem rowItem =的getItem(位置);

    LayoutInflater mInflater =(LayoutInflater)上下文
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    如果(convertView == NULL){
        convertView = mInflater.inflate(R.layout.list_item,NULL);
        持有人=新ViewHolder();
        holder.txtTitle =(TextView中)convertView.findViewById(R.id.title);
        holder.imageView =(ImageView的)convertView.findViewById(R.id.icon);
        convertView.setTag(保持器);
    } 其他
        支架=(ViewHolder)convertView.getTag();

    holder.txtTitle.setText(rowItem.getTitle());
    holder.imageView.setImageResource(rowItem.getImageId());

    返回convertView;
}
 

}

然后:

 的ListView =(ListView控件)findViewById(R.id.list);
        CustomListViewAdapter适配器=新CustomListViewAdapter(这一点,
                R.layout.list_item,rowItems);
        listView.setAdapter(适配器);
        listView.setOnItemClickListener(本);
    }


@覆盖
公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,
        长ID){
    吐司面包= Toast.makeText(getApplicationContext()
        项+(位置+ 1)+:+ rowItems.get(位置),
        Toast.LENGTH_SHORT);
    toast.show();
}
 

请参阅扩展其他适配器也为列表视图和他们的自定义实现: 例如。 http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-arrayadapter/

In my activity I implement a list which contains the names of some files. Every list's item refers to a layout in which I'd like to show the name of the images and a thumbnail of the image referenced. Ican show the the name using an ArrayAdapter but i don't know hot to insert the image thumbnail. All the image referenced stay in sd_card and i have the path of they. Here is the single row layout:

<?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>

and the layout of the activity

<?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>

and the activity code inherent adapter.

    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){

...................            
           } 

    }); 

I'm trying using this but doesn't work properly.

    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);

解决方案

You need to implement a custom array adapter and specify that imageview and textview layout/id(s) in that adapter.

Something like:

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;
}

}

And then:

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();
}  

Refer Extending other Adapters too, for ListViews and their custom implementations: Eg. http://theopentutorials.com/tutorials/android/listview/android-custom-listview-with-image-and-text-using-arrayadapter/

 
精彩推荐
图片推荐