我是Android的初學者程序員,我需要更改畫廊控件,並閱讀Devsmart horizontallistview控件,但是我無法運行它。HorizontalListView如何填充圖像數組?
我使用來自web服務的整數數組來動態填充我的舊圖庫控件。 我如何填寫horizontalllistview?
...在這一點上:
private void llenagallery() {
int i,Id;
for (i = 0; i<(Servicios.length()); i++) {
String valor = Character.toString(Servicios.charAt(i));
if (valor.equals("a")) {
Id = this.getResources().getIdentifier("a", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("b")) {
Id = this.getResources().getIdentifier("b", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("c")) {
Id = this.getResources().getIdentifier("c", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("d")) {
Id = this.getResources().getIdentifier("d", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("e")) {
Id = this.getResources().getIdentifier("e", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("f")) {
Id = this.getResources().getIdentifier("f", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("g")) {
Id = this.getResources().getIdentifier("g", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("i")) {
Id = this.getResources().getIdentifier("i", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("j")) {
Id = this.getResources().getIdentifier("j", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("l")) {
Id = this.getResources().getIdentifier("l", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("m")) {
Id = this.getResources().getIdentifier("m", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("n")) {
Id = this.getResources().getIdentifier("n", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("o")) {
Id = this.getResources().getIdentifier("o", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("p")) {
Id = this.getResources().getIdentifier("p", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("r")) {
Id = this.getResources().getIdentifier("r", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("s")) {
Id = this.getResources().getIdentifier("s", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("t")) {
Id = this.getResources().getIdentifier("t", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("u")) {
Id = this.getResources().getIdentifier("u", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("v")) {
Id = this.getResources().getIdentifier("v", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("w")) {
Id = this.getResources().getIdentifier("w", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("y")) {
Id = this.getResources().getIdentifier("y", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("z")) {
Id = this.getResources().getIdentifier("z", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("P")) {
Id = this.getResources().getIdentifier("euro1", "drawable", this.getPackageName());
imagenes.add(Id);
}else if (valor.equals("h")) {
Id = this.getResources().getIdentifier("euro2", "drawable", this.getPackageName());
imagenes.add(Id);
}
}
HorizontalListView hscroll = (HorizontalListView)findViewById(R.id.iconos);
hscroll.setAdapter(new ImageAdapter(this,imagenes));
但是當調試程序時,會顯示(的DINAMIC圖標) Thank's沒有和我的英語很抱歉。
編輯: 這是我ImageAdapter:
public class ImageAdapter extends BaseAdapter {
List<Integer> imageIDs;
private Context context;
public ImageAdapter(Context c, List<Integer> imagenes)
{
context = c;
imageIDs = imagenes;
}
//---returns the number of images---
public int getCount() {
return imageIDs.size();
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView = new ImageView(this.context);
imageView.setImageResource(imageIDs.get(position));
imageView.setLayoutParams(new HorizontalListView.LayoutParams(120,120));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//imageView.setBackgroundResource(mItemBackground);
return imageView;
}
}
Thank's尋求幫助。
這是一個完美的例子,你應該使用「開關」指令,使質量更好的代碼... – Marek
是的,你是對的。更多的工作!謝謝。 –
毫米開關不是posibble白色字符串,例子只能與枚舉,我沒有這個。值是一個字符串的字符。 (前'abcdefgh'),我們將角色提取爲角色,我需要根據角色的值分配一個圖標,並在horizontallistview中一起顯示,因爲圖庫控件不會在某些新聞android設備中運行。 –