Android中adapt的应用

SimpleAdapter 实例:

List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();
for(int i= 0;i <  imageIds.length;i++){
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("image", imageIds[i]);
listItems.add(listItem);
}

SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.cell, new String[]{"image"}, new int[]{R.id.image1});

GridView grid = (GridView) findViewById(R.id.grid01);
grid.setAdapter(simpleAdapter);

grid.setOnItemSelectedListener(new OnItemSelectedListener() {


@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
switcher.setImageResource(imageIds[position % imageIds.length]);
}


@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}


});





ExpandableListAdapter 实例1:

public class ExpandableListViewTest extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个BaseExpandableListAdapter对象
ExpandableListAdapter adapter = new BaseExpandableListAdapter()
{
int[] logos = new int[]
{
R.drawable.p,
R.drawable.z,
R.drawable.t
};
  private String[] armTypes = new String[]
{ "神族兵种", "虫族兵种", "人族兵种"};
private String[][] arms = new String[][]
{
{ "狂战士", "龙骑士", "黑暗圣堂", "电兵" },
{ "小狗", "刺蛇", "飞龙", "自爆飞机" },
{ "机枪兵", "护士MM" , "幽灵" }
};
//获取指定组位置、指定子列表项处的子列表项数据
@Override
public Object getChild(int groupPosition, int childPosition)
{
return arms[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition)
{
return arms[groupPosition].length;
}
private TextView getTextView()
{
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(ExpandableListViewTest.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
//该方法决定每个子选项的外观
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
//获取指定组位置处的组数据
@Override
public Object getGroup(int groupPosition)
{
return armTypes[groupPosition];
}
@Override
public int getGroupCount()
{
return armTypes.length;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
//该方法决定每个组选项的外观
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
LinearLayout ll = new LinearLayout(ExpandableListViewTest.this);
ll.setOrientation(0);
ImageView logo = new ImageView(ExpandableListViewTest.this);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
@Override
public boolean hasStableIds()
{
return true;
}
};


ExpandableListView expandListView = (ExpandableListView)
findViewById(R.id.list);
expandListView.setAdapter(expandableListAdapter);
}
}


ExpandableListAdapter 实例2:

public class SelectCityActivity extends ExpandableListActivity

{
//定义省份数组
private String[] provinces = new String[]
{ "广东", "广西", "湖南"};
private String[][] cities = new String[][]
{
{ "广州", "深圳", "珠海", "中山" },
{ "桂林", "柳州", "南宁", "北海" },
{ "长沙", "岳阳" , "衡阳" , "株洲" }
};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ExpandableListAdapter adapter = new BaseExpandableListAdapter()
{
//获取指定组位置、指定子列表项处的子列表项数据
@Override
public Object getChild(int groupPosition, int childPosition)
{
return cities[groupPosition][childPosition];
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition)
{
return cities[groupPosition].length;
}
private TextView getTextView()
{
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(SelectCityActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
//该方法决定每个子选项的外观
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
//获取指定组位置处的组数据
@Override
public Object getGroup(int groupPosition)
{
return provinces[groupPosition];
}
@Override
public int getGroupCount()
{
return provinces.length;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
//该方法决定每个组选项的外观
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
LinearLayout ll = new LinearLayout(SelectCityActivity.this);
ll.setOrientation(0);
ImageView logo = new ImageView(SelectCityActivity.this);
ll.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
@Override
public boolean hasStableIds()
{
return true;
}
};
// 设置该窗口显示列表
setListAdapter(adapter);
getExpandableListView().setOnChildClickListener(
new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView parent, View source,
int groupPosition, int childPosition, long id)
{
//获取启动该Activity之前的Activity对应的Intent
Intent intent = getIntent();
Bundle data = new Bundle();
data.putString("city" ,cities[groupPosition][childPosition]);
intent.putExtras(data);
// 设置该SelectActivity的结果码,并设置结束之后退回的Activity
SelectCityActivity.this.setResult(0 , intent);
//结束SelectCityActivity。
SelectCityActivity.this.finish();
return false;

}
});
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值