【问题标题】:simpleAdapter thumbnail and text and onItemClick handlersimpleAdapter 缩略图和文本以及 onItemClick 处理程序
【发布时间】:2014-02-18 13:02:00
【问题描述】:

在我的应用程序中,我需要显示一个带有自己相对标题的缩略图列表。 这是我的实现。

    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);
        Log.i("list",thumbnailBitmap.toString());
    }
   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);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
           @Override  
           public void onItemClick(AdapterView<?> adapter, final View componente, int pos, long id){  
               List<Map<String,Object>> res =  new ArrayList<Map<String,Object>>();
               res =  (List<Map<String, Object>>) adapter.getItemAtPosition(pos); //classCastExeption
               Log.i("list","assegamento a res eseguito");
               titoloriga = (String) res.get(pos).get("titolo");
               Log.i("list","assegamento a titoloriga eseguito");
               Log.i("list", "Ho cliccato sull'elemento con titolo" + titoloriga+"    " +Integer.valueOf(pos)+"    "+Long.valueOf(id));
               registerForContextMenu(componente);
               componente.showContextMenu();

           } 

    }); 

我有两个问题: 1. 简单的适配器只显示标题而不显示缩略图。 2.我不知道如何处理onItemClick,我需要存储标题。这样我就有了 ClassCastExeption 和不安全演员的警告。 这是单行和列表视图的布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="65dp"
    android:layout_height="65dip"
     />

<TextView
    android:id="@+id/titoloTv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/imageView"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

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

谢谢

【问题讨论】:

    标签: android classcastexception onitemclicklistener simpleadapter


    【解决方案1】:

    在ListView的Item点击上,你不需要再次创建一个Hash map arraylist,你可以通过它存储在你的hashmap的ArrayList中。所以改变

       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
           @Override  
           public void onItemClick(AdapterView<?> adapter, final View componente, int pos, long id){  
    
               String title = data.get(pos).get("titolo");
               String thumbimage = data.get(pos).get("thumbnail");
               registerForContextMenu(componente);
               componente.showContextMenu();
    
           } 
    
    }); 
    

    【讨论】:

    • 谢谢它的工作原理。缩略图的视图呢?
    猜你喜欢
    • 2018-02-06
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    相关资源
    最近更新 更多