首页 \ 问答 \ Android OnItemClick Switch Case Toast消息(Android OnItemClick Switch Case Toast Message)

Android OnItemClick Switch Case Toast消息(Android OnItemClick Switch Case Toast Message)

我试图这样做,当用户点击图像时,会弹出一个带有相关名称的Toast。 我想我需要用switch语句来做这个,因为我有一个数组,但我不确定这是否真的是正确的方法。

这是我的代码:

public class MainActivity extends AppCompatActivity {
    Integer[] Family = {R.drawable.angelica, R.drawable.dad, R.drawable.enzoandbully, R.drawable.enzokathyalex, R.drawable.ernesto, R.drawable.gale, R.drawable.joel, R.drawable.lorenzo};
    ImageView pic;
    Integer member;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView grid = (GridView) findViewById(R.id.gridView);
        final ImageView pic = (ImageView) findViewById(R.id.imgFamily);
        grid.setAdapter(new ImageAdapter(this));
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(getBaseContext(), " " + (position + 1), Toast.LENGTH_SHORT).show( );
                pic.setImageResource(Family[position]);
            }
        });
    }
    public class ImageAdapter extends BaseAdapter {
        private Context context;
        public ImageAdapter(Context c) {
            context = c;
        }

        @Override
        public int getCount() {
            return Family.length;
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            pic = new ImageView(context);
            pic.setImageResource(Family[position]);
            pic.setScaleType(ImageView.ScaleType.FIT_XY);
            pic.setLayoutParams(new GridView.LayoutParams(330,300));
            return pic;
        }
    }


}

编辑:忘记XML代码:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp" tools:context=".MainActivity">

    <GridView
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:id="@+id/gridView"
        android:numColumns="2"
        android:columnWidth="160dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:gravity="center" />

    <ImageView
        android:id="@+id/imgFamily"
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:layout_gravity="center_horizontal"
        android:contentDescription="@string/imgFamily"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_row="12"
        android:layout_column="0" />
</GridLayout>

I'm trying to make it so when the user taps an image, a Toast pops up with the relevant name. I think I need to do this with a switch statement since I have an array, but am unsure if that's actually the correct way.

Here's my code:

public class MainActivity extends AppCompatActivity {
    Integer[] Family = {R.drawable.angelica, R.drawable.dad, R.drawable.enzoandbully, R.drawable.enzokathyalex, R.drawable.ernesto, R.drawable.gale, R.drawable.joel, R.drawable.lorenzo};
    ImageView pic;
    Integer member;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GridView grid = (GridView) findViewById(R.id.gridView);
        final ImageView pic = (ImageView) findViewById(R.id.imgFamily);
        grid.setAdapter(new ImageAdapter(this));
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(getBaseContext(), " " + (position + 1), Toast.LENGTH_SHORT).show( );
                pic.setImageResource(Family[position]);
            }
        });
    }
    public class ImageAdapter extends BaseAdapter {
        private Context context;
        public ImageAdapter(Context c) {
            context = c;
        }

        @Override
        public int getCount() {
            return Family.length;
        }

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            pic = new ImageView(context);
            pic.setImageResource(Family[position]);
            pic.setScaleType(ImageView.ScaleType.FIT_XY);
            pic.setLayoutParams(new GridView.LayoutParams(330,300));
            return pic;
        }
    }


}

Edit: Forgot XML Code:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp" tools:context=".MainActivity">

    <GridView
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:id="@+id/gridView"
        android:numColumns="2"
        android:columnWidth="160dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:gravity="center" />

    <ImageView
        android:id="@+id/imgFamily"
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:layout_gravity="center_horizontal"
        android:contentDescription="@string/imgFamily"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_row="12"
        android:layout_column="0" />
</GridLayout>

原文:https://stackoverflow.com/questions/33622824
更新时间:2023-04-12 15:04

最满意答案

所以你有几行看起来像这样:

    self.set_html("<h2> {0} </h2>").format(self.logout_link.get_html())

您可能希望它们看起来像:

    self.set_html("<h2> {0} </h2>".format(self.logout_link.get_html()))

So you have a few lines that looks like this:

    self.set_html("<h2> {0} </h2>").format(self.logout_link.get_html())

You probably want them to look like:

    self.set_html("<h2> {0} </h2>".format(self.logout_link.get_html()))

相关问答

更多
  • 是的,“一般来说”你可以很容易地拥有一个支持decorator模式的MVC框架。 特别是对于春天,是的,你可以“做到”(我个人不知道如何)但是这里有一个关于它的SO问题,解释如何解决别人对它的实现。 (所以'可以'完成) sitemesh和spring MVC装饰模式问题 Yes, "In general" you could easily have a MVC framework that supports the decorator pattern. For spring in particular, ...
  • 例如,他们谈论的是检查饮料2类型的代码 if (beverage2 instanceof DarkRoast) { } 一旦用Mocha或Whip装饰drink2,它就不再是DarkRoast了。 编辑:我还应该提一下,通常使用instanceof表示一个不完整使用OO的糟糕设计,这表示它在某些情况下很有用。 They're talking about code that checks the type of beverage2, for example if (be ...
  • 我不知道我是否正确地得到了这个问题,但是如果你想要一个方法drawBlackAndWhite() (假设它是RedShapeDecorator.draw()的模拟),你可以在你的装饰器中定义其他方法。 要在装饰器中获取draw3D()方法,您应该创建一个新的Shape接口实现,它可以绘制3D对象(因为现有实现Circle和Rectangle是平的)。 如果你希望所有这些方法在你的装饰器的一个实例中放入多个Shape字段。 但是由于Shape接口只有一个方法draw()所以你将无法通过引用调用方法drawBl ...
  • 不,这不是一个奇怪的巧合,不,python @decorators不实现GOF装饰器模式。 GOF(四人帮,以“设计模式”这本书的四位作者命名)装饰模式增加了“装饰”来改变对象的行为。 它通过创建可应用于未装饰基类的“装饰器”类来实现此目的。 Python装饰器为功能或类添加了“装饰”。 这些装饰器本质上是包装或修改函数/类的函数。 两者都是类似的,你可以重复使用装饰器多次添加相同的功能,但装饰器模式要求装饰器与你可以装饰的类一起设计。 Python装饰器反而只是在任何类的函数上进行工作。 No, it's ...
  • 了解作曲家和作曲家之间的区别很重要。 装饰器是一种组合形式,但主要的一点是,它可以让包装器被通常使用装饰对象的代码使用。 让我们用一个常见的例子来帮助探索这个问题。 考虑接口InputStream 。 我可能有一个方法将字节从一个流复制到另一个流: public static void copy(InputStream in, OutputStream out) { ... } 现在说我们有一个我们想要复制的文件。 我会创建一个FileInputStream并将其传递给copy() 。 但是说我得到一个要 ...
  • 我可能会遇到这样一个问题的设计模式是: 依赖注入和一个框架,Composite模式和Builder模式。 他们应该基本上让你将机器人的创作与他们的使用分开。 The design patterns I'd probably go with for such a problem are: Dependency Injection and a framework to go with it, the Composite pattern, and the Builder pattern. They should ...
  • 所以你有几行看起来像这样: self.set_html("

    {0}

    ").format(self.logout_link.get_html()) 您可能希望它们看起来像: self.set_html("

    {0}

    ".format(self.logout_link.get_html())) So you have a few lines that looks like this: self.set_html("

    {0}

    ").f ...
  • 装饰者是一种通用模式; 它可能意味着多种东西,具体取决于域名。 它们也被称为“包装器”或“适配器”,IMO更适用于功能包装范例。 也就是说,不要过于依赖精确的措辞:它们是模式 ,而不是一成不变的法则。 Decorators are a generic pattern; it can mean multiple things depending on the domain. They're also called "wrappers" or "adapters", which IMO is more appl ...
  • 假设Email是Java界面。 任何实现Email类都可以用来初始化变量email包括任何装饰器,因为装饰器实现接口或扩展抽象类(实现接口)。 当然,客户端可以知道什么是类实例,但他们通常不需要这样做。 只需使用email.getClass() 。 另一方面,装饰器与装饰物不是同一个物体。 它包装装饰物体。 因此TextEmail和SecuredEmail不是同一个对象。 如果代码看起来像: TextEmail txtEmail = new TextEmail(); SecuredEmail secured ...
  • 你应该阅读这个解释什么是python的装饰器。 我们与Python有关的“装饰者”与DecoratorPattern [...]并不完全相同。 Python装饰器是对Python语法的一种特定更改,它允许我们更方便地更改函数和方法(以及未来版本中可能的类)。 这支持DecoratorPattern的更易读的应用程序,但也支持其他用途。 因此,您将能够使用python的装饰器实现“经典”装饰器模式,但您将能够使用这些装饰器做更多(有趣;))事情。 在您的情况下,它可能看起来像: def applyToppin ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)