2012-12-27 347 views
1

我有一個gridview,我在對話視圖中膨脹。 現在,當我打開對話框(使用網格視圖)時,它會顯示一組10個圖像。獲取GridView項目的圖像名稱

當對話框被取消時,我選擇的圖像被設置在ImageView上。

現在我的查詢是我可以得到圖像的名稱??在ImageView上設置?

我必須保存它somehwere。

@Override 
public void onClick(View view) { 
    // TODO Auto-generated method stub 
    final Dialog groupIconsDialog = new Dialog(CreateGroupActivity.this); 
    groupIconsDialog.setTitle("Choose Group Icon"); 
    groupIconsDialog.setContentView(R.layout.group_icons_layout); 
    //calling and setting the image icons to the grid view adapter 
    GridView groupIconsGrid = (GridView)groupIconsDialog.findViewById(R.id.grid_groupIcons); 
    groupIconsGrid.setAdapter(new GroupIconAdapter(CreateGroupActivity.this)); 

    groupIconsGrid.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, 
         int position, long arg3) { 
        // TODO Auto-generated method stub 

        group_icon.setImageResource(new GroupIconAdapter(CreateGroupActivity.this).mThumbIds[position]); 
        groupIconsDialog.dismiss(); 

       } 

      }); 

      groupIconsDialog.show(); 

     } 
    }); 

ImageAdapter

public class GroupIconAdapter extends BaseAdapter { 

    private Context mContext; 

    //icons image array 
    public Integer[] mThumbIds = { 
       R.drawable.mogra,R.drawable.rhino, 
       R.drawable.zebra,R.drawable.lion, 
       R.drawable.mogra,R.drawable.rhino, 
       R.drawable.giraffee,R.drawable.giraffee, 
       R.drawable.lion,R.drawable.rhino 
     }; 

    public GroupIconAdapter(Context c) { 
     // TODO Auto-generated constructor stub 
     mContext = c; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return mThumbIds.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return mThumbIds[position]; 
    } 

    @Override 
    public long getItemId(int arg0) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public View getView(int position, View view, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     ImageView imageView = new ImageView(mContext); 
     imageView.setImageResource(mThumbIds[position]); 
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     imageView.setLayoutParams(new GridView.LayoutParams(50, 50)); 
     return imageView; 
     } 
} 

回答

4

是的,你可以得到圖像的名稱,如果你有圖片的資源ID。您可以使用getResourceEntryName(resourceId)

String name = getResources().getResourceEntryName(mThumbIds[position]); 
+0

它不工作 – Aditya

+0

http://stackoverflow.com/questions/31826494/image-is-not-attaching-in-gmail-and-facebook – Aditya

+0

我得到的圖像與GAMIL名附件..當檢查其類型..它顯示文件類型 – Aditya