如何为每个在Android中使用GridView显示的imageView添加不同的toast消息

时间:2017-03-21 14:08:47

标签: java android gridview

activity_main

<GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="8"
        tools:ignore="MissingConstraints" />

ImageAdapter.java

  package com.example.android.whattheemoticon;

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

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

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

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(mThumbIds[position]);
            return imageView;
        }
        private Integer[] mThumbIds = {
                R.drawable.smileys, R.drawable.animals,
                R.drawable.food, R.drawable.activities,
                R.drawable.symbols, R.drawable.objects,
                R.drawable.travel, R.drawable.flags,
        };
    }

MainActivity.java

    package com.example.android.whattheemoticon;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.GridView;
    import android.widget.Toast;

    public class MainActivity extends AppCompatActivity {

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

    //display images using gridview.
            GridView gridview = (GridView) findViewById(R.id.gridview);
            gridview.setAdapter(new ImageAdapter(this));
    //onClickListeners for each clickable imageViews.
     gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       public void onItemClick(AdapterView<?> parent, View v,
                                        int position, long id) {
//create toast for view when clicked.
     Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

我试图为使用GridView显示的每个图片添加toast消息,我最终添加了toast的代码以适用于每个网格。我已完成在GridView中添加和显示图像。请帮助我了解如何在用户点击的同时为每个图像显示正确的吐司信息。

2 个答案:

答案 0 :(得分:0)

@Rajashekar。这是一个例子。

public class MainActivity extends AppCompatActivity {

     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

        //display images using gridview.
        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));
        final ArrayList<String> message = new ArrayList<>();
        message.add("Message for image 0");
        message.add("Message for image 1");
        message.add("Message for image 2");
        //onClickListeners for each clickable imageViews.
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
   //create toast for view when clicked.
    String mess = message.get(position);
    Toast.makeText(MainActivity.this, "" + mess, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

答案 1 :(得分:0)

声明一个这样的数组:

String arr[ImageCount] = {"Message for first image", "Message for second image"......};

然后使用函数打印Toast Message,如下所示:

public void showToast(int pos){
       for(int i = 0; i<=pos; i++){
                if(i == pos){
                     Toast.makeText(Context, arr[i], Toast.LENGTH_SHORT).show();
                     break;
                 }
        }
  }

现在调用函数:

public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
           showToast(position);
            }