1.之所以选择弹出窗口中显示,是为了一段代码可以学会两个功能,弹出窗口和ListView显示请求的值
2.弹出窗口的代码:
if(mPopupWindow == null){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View mView = inflater.inflate(R.layout.pop_window,null); //R.layout.pop_window是弹出的窗口布局
ListView mListView = (ListView)mView.findViewById(R.id.carList);
CarsApater apdater = new CarsAdapter(getApplicationContext(),carList);
mListView.setAdapter(adapter);
WindowManager windowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
mPopupWindow = new PopupWindow(mView.windowManager.getDefaultDisplay().getWidth(),mView.windowManager.getDefaultDisplay().getHeidht()/3); //设置弹出窗口大小
mPopupWindow.setFocusable(true); //使其聚焦
mPopupWindow.setOutsideTouchable(true); //设置允许在外点击消失
mPopupWindow.showAsDropDown(parent,windowManager.getDefaultDisplay().getWidth()/2- mPopupWindow,getWidth()/2,windowManager.getDefaultDisplay().getHeidht()); //parent在自己代码中是onClick(View parent)
}
2.CarsAdpter代码:
public class CarsAdapter extends BaseAdapter{
private List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
private int mCount =0;
private Context context;
public CarsAdapter(Context context,List<Map<String,Object>> data){
this.context = context;
this.data = data;
}
@Override
public int getCount(){
return data.size;
}
@Override
public Object getItem(int position){
return data.get(position);
}
@Override
public long getItemId(int position){
return position;
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
ViewHolder tag = null;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(getLayoutResource(),null);
tag = getTag(convertView);
convertView.setTag(tag);
}else{
tag = (ViewHolder) convertView.getTag();
}
try{
Map<String,Object> carPath = data.get(position);
bindData(tag,carPath);
}catch(Exception e){
}
return convertView;
}
static class ViewHolder{
TextView carNo,ownerName,speed;
ImageButton vedioBtn;
}
public void bindData(ViewHolder tag,Map<String,Object> data){
tag.carNo.setText(data.get("carNo").toString());
tag.ownerName.setText(data.get("ownerName").toString());
tag.speed.setText(data.get("speed").toString());
tag.videoBtn,setOnClickListener(new View.OnClickListener(){
public void onClick(View arg0){
String param="XXXXXXXXXxx";
ComponentName componentName = new ComponentName("package","package.MainActivity"); //第一个参数是其他应用的包名,第二个参数是要启动Activity
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("xx",params);
intent.setComponent(componentName);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
}
public int getLayoutResource(){
return R.layout.item_car;
}
public ViewHolder getTag(View convertView){
ViewHolder mViewHolder = new ViewHolder();
mViewHolder.carNo = (TextView) convertView.findViewById(R.id.carNo);
mViewHolder.ownerName = (TextView) convertView.findViewById(R.id.carNo);
mViewHolder.speed = (TextView) convertView.findViewById(R.id.carNo);
mViewHolder.videoBtn = (ImageButton) convertView.findViewById(R.id.videoBtn);
return mViewHolder;
}
public void addNewsItem(List<Map<String,Object>> newsItemCollection){
data.addAll(newItemCollection);
}
}