【问题标题】:How to show images in a button from a dynamic list?如何在动态列表中的按钮中显示图像?
【发布时间】:2017-10-18 06:01:14
【问题描述】:

我在 Android 中做一个应用程序,我希望有一个图像列表,每次单击按钮时都会随机显示在按钮的背景中。

存储它们的最佳方式是什么?每次单击按钮时,我都想缩小列表以不再显示该图像

【问题讨论】:

    标签: android arrays image list setbackground


    【解决方案1】:

    您可以通过在每次单击并添加一些新项目/图像时删除最后一个项目/图像来做到这一点。这是基本列表视图中简单数组列表的示例。在您的活动布局中添加列表视图并将此代码添加到活动中,您将获得简单的列表,每次单击按钮时添加和删除项目。如果您不根据自己的喜好和需要修改子句,从技术上讲,您的列表可能永远只有 2 号。

        ListView list = (ListView) findViewById(R.id.listView);
        Button button = (Button) findViewById(R.id.button);
    
        List<String> arrayList = new ArrayList<String>();
    
        ArrayAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList);
    
        list.setAdapter(adapter);
    
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                // add item
                arrayList.add(newItem);
    
                // make sure you have at least two items in list so you don't have empty list and remove last item
                if(arrayList.size())
                    arrayList.remove(lastItem);
    
                // make sure your adapter has changed
                adapter.notifyDataSetChanged();
            }
        });
    

    【讨论】:

    • 嗨 tompadre,我认为这并不能解决我提出的问题,是吗?我只想让图像“在本地”,然后随机来到每个图像,例如生成一个介于 0 和数组长度之间的随机数。谢谢你的帮助:)
    • 你的问题不是很清楚。你在本地想要什么?你想要手机上的 imsges 还是想要在本地缓存 url 或 img64?您写道,您想要通过每次点击添加或减少的“本地图像”。我的代码将完全做到您在资产中有 URL 数组或图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2021-12-17
    • 2021-11-21
    • 2014-05-07
    相关资源
    最近更新 更多