读书人

扩张GridView显示文字以及BaseAdapter

发布时间: 2012-08-26 16:48:06 作者: rapoo

扩展GridView显示文字以及BaseAdapter的集成

可以参考http://developer.android.com/guide/tutorials/views/hello-gridview.html
然后只需要修改:
public class MyAdapter extends BaseAdapter {
???? private Context context;
???? private String[] texts = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "eee", "hhh", "iii"};
???? public MyAdapter(Context context) {???
????? this.context = context;
???? }
???? public int getCount() {??? return 9;}
???? public Object getItem(int position) {??? return null;}
???? public long getItemId(int position) {??? return 0;}
???? public View getView(int position, View convertView, ViewGroup parent) {
???????? TextView tv;???
???????? if (convertView == null) {
???????????????? tv = new TextView(context);???????
???????????????? tv.setLayoutParams(new GridView.LayoutParams(85, 85));
???????????????????? }
?????????? else {
????????????????? tv = (TextView) convertView;??
?????????????????? }
???????????? tv.setText(texts[position]);???
???????????? return tv;
?????? }

view集成的方法
public View getView(int position, View contentView, ViewGroup arg2)
??? {??????? ViewHolder holder;???????
?????????? if (contentView == null) {
?????????????????????? holder = new ViewHolder();???????????
?????????????????????? contentView = inflater.inflate(R.layout.my_magic_list,null);??????????
??????????????????????? holder.label = (TextView) contentView.findViewById(R.id.label);???????????
??????????????????????? contentView.setTag(holder);??????? }
??????????? else {??????????? holder = (ViewHolder) contentView.getTag();??????? }???????
??????????? holder.label.setText(getLabel());???????
??????????? return contentView;??? }

?Use convertView
?If you have images, don't scale your images on the fly. Use Bitmap.createScaledBitmap to create a scaled bitmap and put that into your views
?Use a ViewHolder so you don't have to call a bunch of findViewByIds() every time
?Decrease the complexity of the views in your listview. The fewer subviews, the better. RelativeLayout is much better at this than, say, LinearLayout. And make sure to use if you're implementing custom views.

4.LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);???
final View dialogView = li.inflate(R.layout.edit_event, null);???
ArrayList<String> routes = new ArrayList<String>();?
ArrayAdapter<String> aa = new ArrayAdapter<String>(GOFdroid.this, android.R.layout.simple_spinner_item, routes);
??? aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);??
???? Spinner destSpinner = (Spinner) dialogView.findViewById(R.id.edit_event_destination);
??????? String dest = events.get(pos).getDestination();
?????????? int routesPos = routes.indexOf(dest);
????????????? Log.d(TAG, "Dest: " + dest + ", pos: " + routesPos);?
??????????????
?????????????????? destSpinner.setAdapter(aa);
destSpinner.setSelection(routesPos);
最后一句是在spiner没有显示之前显示一个特定信息

?

5.

? ?
? ? ? ? ? ? new int[] { android.R.id.text1 });?
5.只想显示一个

display a static list of String objects

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new String[] {People.NAME});?
add more String objects to the list later

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new ArrayList<String>());?

读书人网 >移动开发

热点推荐