Java 如何设置从gridview单击的图像?

Java 如何设置从gridview单击的图像?,java,android,Java,Android,我能够在gridview中显示被单击的项目的ID,但是我想要实现的是,当我单击gridview中的任何图像时,它显示为图像,而且,当我使用下面的代码单击任何图像时,它只显示ID,但是,当我试图显示图像本身时,应用程序崩溃,出现LinearLayout,无法将其转换为ImageView 礼品_布局_2.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.and

我能够在gridview中显示被单击的项目的ID,但是我想要实现的是,当我单击gridview中的任何图像时,它显示为图像,而且,当我使用下面的代码单击任何图像时,它只显示ID,但是,当我试图显示图像本身时,应用程序崩溃,出现LinearLayout,无法将其转换为ImageView

礼品_布局_2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_history_menu_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">


<ImageView
    android:id="@+id/close"
    android:layout_width="15dp"
    android:layout_height="15dp"
    android:layout_gravity="right"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    android:src="@drawable/close" />


<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:text="Smiles"
    android:textColor="#000000"
    android:textSize="17sp"
    android:textStyle="bold"
    android:layout_gravity="left"
    android:layout_marginLeft="15dp"/>


<GridView
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_marginTop="10dp"
    android:numColumns="4"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:gravity="center|center_horizontal|center_vertical"
    android:columnWidth="100dp"
    android:stretchMode="columnWidth"
    android:scrollbars="vertical"
    android:scrollbarSize="3dp"
    android:scrollIndicators="left"
    android:id="@+id/grid_view2"
    android:paddingLeft="10dp"
    android:layout_marginBottom="10dp"
    android:layout_gravity="center|center_horizontal|center_vertical"
    android:clipChildren="false"
    android:clipToPadding="false"/>


</LinearLayout>
smiles\u items\u layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView android:id="@+id/smile_image_view"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:layout_gravity="center"/>


</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<ImageView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/smile_image_view"
        android:layout_width="45dp"
        android:layout_height="45dp"
 />
BottomSheetDialog_Smiles.java

@Override
public View getView(final int position, View convertView, ViewGroup parent) 
{

final Holder holder = new Holder();
LayoutInflater inflater = (LayoutInflater) 
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
grid = inflater.inflate(R.layout.smile_items_layout, null);
lottieImage = inflater.inflate(R.layout.activity_streaming2, null);

holder.img = (ImageView) grid.findViewById(R.id.smile_image_view);
holder.img.setImageResource(mThumbIds[position]);

gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long 
l) {
JSONDictionary imageChat = new JSONDictionary();
int imageId = (int) getItem(i);
imageChat.put("message",imageId);
Communicator.getInstance().emit("new chat message", imageChat);
}
});

return grid;
}

请尝试下面的代码,单击已单击的“重新运行视图”

gridView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
   ImageView imageView = (ImageView)view;
   //if you want to get image from imageview
   imageView .getDrawable()
  }
});
更新

你用两种方法做

第一条路: 从xml中删除LinerLayout并使ImageView作为LinerLayout的父视图只包含一个这样的视图,如果您使用它,则无需更改java代码

smiles\u items\u layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageView android:id="@+id/smile_image_view"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:layout_gravity="center"/>


</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<ImageView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/smile_image_view"
        android:layout_width="45dp"
        android:layout_height="45dp"
 />

你的网格图像正确吗?当点击网格的一个图像时会发生什么?确切地说,当点击图像时,它应该被发送到服务器,因为我正在使用socket io,如果我将字符串或int而不是图像,然而,我需要的是显示图像本身。我建议您将新图像保存在片段中,并使用片段转换让图像显示在屏幕上。如果你感到迷茫,我可以发布一个真实的答案。我可以发送我代码中显示的图像id,所以为了发送图像而不是文本,我需要一些特定的东西吗?对不起,我把你的问题搞错了。检查此答案我已经尝试了您编写的代码从imageview获取图像,但是应用程序崩溃并出现错误java.lang.ClassCastException:android.widget.LinearLayout无法转换为android.widget.imageview,请帮助我尝试了所有操作,但仍然没有working@O.D我需要显示gridView2的xml。你在gridView2中膨胀了哪种xml,那是post,你在cliclkso获得微笑时必须获得的图像的哪种内容?你看到这个图像视图了吗?我已经尝试过了,它提供了一些文本,比如android.support.v7.widget.AppCompactImageViewbaa77fcV.ED…….52,0-1871357F080101 app:id/smile\u image\u view,但没有提供图像本身,而且当我更改xml时,它没有提供图像,它是完全空白的