【发布时间】:2017-10-18 06:01:14
【问题描述】:
我在 Android 中做一个应用程序,我希望有一个图像列表,每次单击按钮时都会随机显示在按钮的背景中。
存储它们的最佳方式是什么?每次单击按钮时,我都想缩小列表以不再显示该图像
【问题讨论】:
标签: android arrays image list setbackground
我在 Android 中做一个应用程序,我希望有一个图像列表,每次单击按钮时都会随机显示在按钮的背景中。
存储它们的最佳方式是什么?每次单击按钮时,我都想缩小列表以不再显示该图像
【问题讨论】:
标签: android arrays image list setbackground
您可以通过在每次单击并添加一些新项目/图像时删除最后一个项目/图像来做到这一点。这是基本列表视图中简单数组列表的示例。在您的活动布局中添加列表视图并将此代码添加到活动中,您将获得简单的列表,每次单击按钮时添加和删除项目。如果您不根据自己的喜好和需要修改子句,从技术上讲,您的列表可能永远只有 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();
}
});
【讨论】: