gpt4 book ai didi

android - 整数数组到字符串数组

转载 作者:行者123 更新时间:2023-11-30 02:20:31 25 4
gpt4 key购买 nike

想把整数数组改成字符串数组,以便在gridview中查看图片。Arrayadapter在android中只支持字符串数组做动画`

int[] resource={R.drawable.chinanight,R.drawable.huhu,R.drawable.jkjkj,R.drawable.kkk};
String aa=Arrays.toString(resource);
String[] ay=aa.split(aa);

GridView grid;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_view);

grid=(GridView)findViewById(R.id.gridView1);


ArrayAdapter ar=new ArrayAdapter(this,android.R.layout.simple_list_item_1,ay);
grid.setAdapter(ar);

Animation asd=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.right);
grid.startAnimation(asd);
}`

请帮忙

最佳答案

适配器的要点是将一组数据链接到一组 View (由 AdapterView 创建,即 ListView)——你不需要给它一个 View 数组来使用。创建一个 Arraylist 来引用你的可绘制对象。

    ArrayList<Pair<String, Integer>> array = new ArrayList<Pair<String, Integer>>();
array.add(Pair.create("Drawable1",R.drawable.chinanight));
array.add(Pair.create("Drawable2", R.drawable.dfg));
array.add(Pair.create("Drawable3", R.drawable.sf));
array.add(Pair.create("Drawable4", R.drawable.sdf));


ArrayAdapter ar=new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
grid.setAdapter(ar);

在适配器的 GetView() 内部

public View getView(int position, View convertView, ViewGroup container) {
if(convertView == null) {
convertView = LayoutInflator.from(getContext()).inflate(R.layout.single_list_row, container, false);
}
Pair<String, Integer> item = getItem(position);
title.setCompoundDrawables(item.second, 0, 0, 0);
return convertView;
}
};

关于android - 整数数组到字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28599349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com