如何从网格布局到警报对话框获取图像视图

时间:2017-02-07 10:48:51

标签: android android-fragments grid-layout

我有一个图像数组,想要显示它的图像,当我们点击网格项时提醒对话框。

这是我的班级:

mainactivity.java

package glassfibre.hospital2;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity
{
    GridView grid;
    String[] web = {
        "Food",
        "Doctor",
        "Water",
        "Feel Cold",
        "Adjust Bed",
        "Toilet"

} ;
int[] imageId = {
        R.drawable.food,
        R.drawable.doctor,
        R.drawable.water2,
        R.drawable.lowtemp,
        R.drawable.bed,
        R.drawable.toilet
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CustomGrid adapter = new CustomGrid(MainActivity.this, web, imageId);
    grid=(GridView)findViewById(R.id.grid);
    grid.setAdapter(adapter);
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Toast.makeText(MainActivity.this, "You Clicked at " +web[+     position], Toast.LENGTH_SHORT).show();
            mydialog dialog = new mydialog();

            dialog.show(getSupportFragmentManager(),"my_dialog");
        }
    });
}
}

CustomGrid.java

package glassfibre.hospital2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] web;
final int[] Imageid;

public CustomGrid(Context c,String[] web,int[] Imageid ) {
    mContext = c;
    this.Imageid = Imageid;
    this.web = web;
}

@Override
public int getCount() {

    return web.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        grid = new View(mContext);
        grid = inflater.inflate(R.layout.grid_single, null);
        TextView textView = (TextView) grid.findViewById(R.id.grid_text);
        ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
        textView.setText(web[position]);
        imageView.setImageResource(Imageid[position]);
    } else {
        grid = (View) convertView;
    }
    return grid;
}
}

grid_single.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >

<ImageView
    android:id="@+id/grid_image"
    android:src="@drawable/food"
    android:layout_width="150dp"
    android:layout_height="150dp">
</ImageView>

<TextView
    android:id="@+id/grid_text"
    android:text="@string/food"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_below="@id/grid_image"
    android:gravity="center"
    android:textSize="20sp" >
</TextView>

</RelativeLayout>

mydialog.java

package glassfibre.hospital2;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;

import java.util.zip.Inflater;

import static android.R.attr.gridViewStyle;
import static android.R.attr.id;
import static android.R.attr.thumbPosition;
import static glassfibre.hospital2.R.layout.grid_single;


public class mydialog extends DialogFragment implements View.OnClickListener
{
Button bt;
ImageView iv;
CustomGrid cg;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup          container, @Nullable Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.my_dialog,null);
    bt=(Button)view.findViewById(R.id.button);
    bt.setOnClickListener(this);
    iv= (ImageView) view.findViewById(R.id.imageView);
    setCancelable(false);
    AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());

// > here i wanted to set image which i click on grid view

    return view;
}

@Override
public void onClick(View view)
{
    if(view.getId()==R.id.button)
    {
        dismiss();
    }
}
}

my_dialog.xml

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

<TextView
    android:text="I want"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="20dp"
    android:id="@+id/textView3"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:gravity="center"
    android:textSize="20dp"/>

<ImageView
    android:layout_width="150dp"
    android:layout_height="150dp"
    app:srcCompat="@drawable/food"
    android:id="@+id/imageView"
    android:layout_marginTop="19dp"
    android:layout_below="@+id/textView3"
    android:layout_centerHorizontal="true" />

<Button
    android:text="OK"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="12dp"
    android:id="@+id/button" />

</RelativeLayout>

我有一个图像数组,想要显示它的图像,当我们点击网格项时提醒对话框。

2 个答案:

答案 0 :(得分:0)

做这样的事情:

grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
        Toast.makeText(MainActivity.this, "You Clicked at " +web[+     position], Toast.LENGTH_SHORT).show();
        mydialog dialog = new mydialog();
        int imageResource = imageId[position];
        dialog.setImageResource(imageResource);
        dialog.show(getSupportFragmentManager(),"my_dialog");

    }
});

在你的mydialog类中声明int变量

private int imageResource;

并创建一个方法:

public void setImageResource(int imageResource) {
    this.imageResource = imageResource;
}

现在您可以在mydialog类的onCreateView方法中将图像资源设置为imageview

答案 1 :(得分:0)

在myDialog类中,只有一个存储Drawable Resource id的setter方法,如下所示

public void setImageIdToDisplay(int aDrawableId) {
    mDrawableId = aDrawableId;
}

并在myDialog类的oncreateview方法中,只需将图片资源ID设置为imageview

iv.setImageResource(mDrawableId);

并在您的 MainActivity onItemClick gridview列表器中,传递drawable资源ID,如下所示

grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
        Toast.makeText(MainActivity.this, "You Clicked at " +web[+     position], Toast.LENGTH_SHORT).show();
        mydialog dialog = new mydialog();
        dialog.setImageIdToDisplay(imageId[position]);

        dialog.show(getSupportFragmentManager(),"my_dialog");

    }
});