我已經讓自己的CustomArrayAdapter顯示了巴西餐館的列表。我已經使用GetView方法來創建自己的自定義視圖。填充自定義數組適應器
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent,false);
String [] items= getResources().getStringArray(R.array.locations_array);
TextView tv= (TextView) row.findViewById(R.id.textView1);
ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
tv.setText(items[position]);
iv.setImageResource(R.drawable.brazil);
return row;
}
}
當前這個新的GetView類從資源xml文件拉入文本字符串並將其放入列表項中。
- 如果我想要在應用程序中生成額外數據的數組,我假設我不會在GetView類中生成數組,因爲每次創建新行時都會重新創建該數組。
- 我在哪裏放置代碼來創建數組,以及如何將這些數據調用到上面的GetView代碼中?
嗨,當我嘗試這種解決方案 - 我得到一個消息,新的lData不能解析爲類型! – 2012-01-02 02:32:30
是的,在包含MyDataClass的行中有一個錯誤lData = new lData(); - 只是編輯它 – CodeDownZero 2012-01-02 02:51:35