效果:点击开始按钮弹出开始选择框,点击巡检方式弹出可单选的菜单,点击巡检人员弹出可多选的菜单
1,单选多选下拉采用dialog自定义view的方式
部分代码截图:
(1)点击开始巡检添加点击事件
(2)dialog自定义view引入下拉菜单的布局
创建dialog
(3)巡检方式单选下拉采用android spinner自带下拉
(4)巡检人员多选下拉采用插件,按照参考引入部分文件即可
参考地址
部分代码:
dialog_patrol_start.xml
dialogView() 部分:
/**
* 开始巡检弹出菜单
*/
private void dialogView() {
View view1 = LayoutInflater.from(mContext).inflate(R.layout.dialog_patrol_start,null);
//巡检方式
spWay = (Spinner) view1.findViewById(R.id.spWay);
adapterWay = new ArrayAdapter<>(mContext, android.R.layout.simple_dropdown_item_1line,way);
spWay.setAdapter(adapterWay);
spWay.setSelection(0, true);//让第一个数据项已经被选中
//巡检人员
// spPerson = (Spinner) view1.findViewById(R.id.spPerson);
spPerson = (SpinnerViewMultiDialog) view1.findViewById(R.id.spPerson);
tvShow = (TextView) view1.findViewById(R.id.tvShow);
mSpinnerPersonList = new ArrayList();
//模拟获取数据集合
try{
mSpinnerPersonList = parseJsonArray("spinners4.txt");
}catch (Exception e) {
e.printStackTrace();
}
//设置下拉菜单显示的列表项文本
if (mSpinnerPersonList != null && mSpinnerPersonList.size() > 0){
spPerson.setData(mSpinnerPersonList);//设置下拉菜单列表集合源
spPerson.setHint("请选择巡检人员");
}
//下拉菜单列表的列表项的点击事件监听
// spPerson.setOnSpinnerConfirmClickListener(new OnSpinnerConfirmClickListener() {
// @Override
// public void OnConfirmed(ArrayList selecteIndexList) {
// StringBuffer str1 = new StringBuffer();
// for(int i=0;i townAdapter = new ArrayAdapter(mContext,android.R.layout.simple_spinner_item, personList);
// townAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// spPerson.setAdapter(townAdapter);
Button cancel = (Button) view1.findViewById(R.id.btnCancel);
Button ok = (Button) view1.findViewById(R.id.btnOk);
//创建Dialog
final AlertDialog dialog = new AlertDialog.Builder(mContext)
.create();
dialog.setTitle("开始巡检");
dialog.setCancelable(false);
//设置自定义view
dialog.setView(view1);
dialog.show();
//取消按钮监听点击事件
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
//确定按钮监听点击事件
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// if (spPerson.getText().toString().equals("00000000"))
// {
// Toast.makeText(mContext,"密码正确!",Toast.LENGTH_SHORT).show();
// dialog.dismiss();
// }
// else
// {
// Toast.makeText(mContext,"密码错误,请重新输入!",Toast.LENGTH_SHORT).show();
// inputPwd.setText("");
// }
}
});
}
adapter.java
package com.andbase.search.adapter;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.ab.util.AbToastUtil;
import com.andbase.R;
import com.andbase.gettask.model.PatrolTask;
import com.andbase.history.dao.HistoryTaskDao;
import com.andbase.model.appModel.SpinnerOption;
import com.andbase.search.activity.PatrolEndReasonActivity;
import com.andbase.search.activity.SearchActivity;
import com.andbase.search.activity.SearchPointActivity;
import com.andbase.util.spinner.SpinnerViewMultiDialog;
import com.andbase.util.spinner.SpinnerViewPop;
import com.andbase.util.spinner.listener.OnSpinnerConfirmClickListener;
import com.google.gson.Gson;
import com.andbase.util.spinner.SpinnearBean;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* author:lmx
* date:2018/4/19
* description:巡检
*/
public class SearchAdapter extends BaseAdapter {
private SearchActivity mContext;
//单行的布局
private int mResource;
//列表展现的数据
private List extends Map> mData;
//Map中的key
private String[] mFrom;
//view的id
private int[] mTo;
//下拉菜单
private String way[] = new String[]{"在线巡检","离线巡检"};
private Spinner spWay;
private int wayindex;
private String person [][] = null;
private ArrayAdapter adapterWay,adapterPerson;
private HistoryTaskDao historyTaskDao;
private List personList = new ArrayList();
private String hisTaskIds;//
//下拉多选
private SpinnerViewMultiDialog spPerson;
private TextView tvShow;
/**下拉菜单列表集合*/
private ArrayList mSpinnerPersonList;
/**
* 构造方法
* @param context
* @param data 列表展现的数据
* @param resource 单行的布局
* @param from Map中的key
* @param to view的id
*/
public SearchAdapter(SearchActivity context, List extends Map> data,int resource, String[] from, int[] to){
mContext = context;
mData = data;
mResource = resource;
mFrom = from;
mTo = to;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position){
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
final ViewHolder holder;
if(convertView == null){
//使用自定义的list_items作为Layout
convertView = LayoutInflater.from(mContext).inflate(mResource, parent, false);
//使用减少findView的次数
holder = new ViewHolder();
holder.itemNo = ((TextView) convertView.findViewById(mTo[0]));
holder.lineName = ((TextView) convertView.findViewById(mTo[1]));
holder.patrolTime = ((TextView) convertView.findViewById(mTo[2]));
holder.finish = ((Button) convertView.findViewById(mTo[3]));
holder.guid = ((TextView) convertView.findViewById(mTo[4]));
holder.status = ((ImageView) convertView.findViewById(mTo[5]));
holder.start = ((Button) convertView.findViewById(mTo[6]));
//设置标记
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
//设置数据
final Map dataSet = mData.get(position);
if (dataSet == null) {
return null;
}
//获取该行数据
final Object itemNo = dataSet.get(mFrom[0]);
final Object lineName = dataSet.get(mFrom[1]);
final Object patrolTime = dataSet.get(mFrom[2]);
final Object finish = dataSet.get(mFrom[3]);
final Object guid = dataSet.get(mFrom[4]);
final Object status = dataSet.get(mFrom[5]);
final Object start = dataSet.get(mFrom[6]);
/*** 有用 ***/
final Object startTime = dataSet.get(mFrom[6]);
final Object endTime = dataSet.get(mFrom[7]);
final Object lineCode = dataSet.get(mFrom[8]);
final Object executeStatus = dataSet.get(mFrom[9]);
final Object isOrder = dataSet.get(mFrom[10]);
/*** 有用 ***/
holder.itemNo.setText(itemNo.toString());
holder.lineName.setText(lineName.toString());
// holder.patrolTime.setText(patrolTime.toString());
holder.finish.setText(finish.toString());
holder.guid.setText(guid.toString());
// holder.status.setText(status.toString());
holder.status.equals(status.toString());
/** 开始巡检 **/
holder.start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialogView();
}
});
return convertView;
}
/**
* ViewHolder类
*/
static class ViewHolder {
TextView itemNo;//编号
TextView lineName;//巡检线路
TextView patrolTime;//巡检时间
ImageView status;//任务状态
TextView guid;//id
TextView lineCode;//巡检线路
Button start;//开始巡检
Button finish;//结束巡检
}
/**
* 开始巡检弹出菜单
*/
private void dialogView() {
View view1 = LayoutInflater.from(mContext).inflate(R.layout.dialog_patrol_start,null);
//巡检方式
spWay = (Spinner) view1.findViewById(R.id.spWay);
adapterWay = new ArrayAdapter<>(mContext, android.R.layout.simple_dropdown_item_1line,way);
spWay.setAdapter(adapterWay);
spWay.setSelection(0, true);//让第一个数据项已经被选中
//巡检人员
// spPerson = (Spinner) view1.findViewById(R.id.spPerson);
spPerson = (SpinnerViewMultiDialog) view1.findViewById(R.id.spPerson);
tvShow = (TextView) view1.findViewById(R.id.tvShow);
mSpinnerPersonList = new ArrayList();
//模拟获取数据集合
try{
mSpinnerPersonList = parseJsonArray("spinners4.txt");
}catch (Exception e) {
e.printStackTrace();
}
//设置下拉菜单显示的列表项文本
if (mSpinnerPersonList != null && mSpinnerPersonList.size() > 0){
spPerson.setData(mSpinnerPersonList);//设置下拉菜单列表集合源
spPerson.setHint("请选择巡检人员");
}
//下拉菜单列表的列表项的点击事件监听
// spPerson.setOnSpinnerConfirmClickListener(new OnSpinnerConfirmClickListener() {
// @Override
// public void OnConfirmed(ArrayList selecteIndexList) {
// StringBuffer str1 = new StringBuffer();
// for(int i=0;i townAdapter = new ArrayAdapter(mContext,android.R.layout.simple_spinner_item, personList);
// townAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// spPerson.setAdapter(townAdapter);
Button cancel = (Button) view1.findViewById(R.id.btnCancel);
Button ok = (Button) view1.findViewById(R.id.btnOk);
//创建Dialog
final AlertDialog dialog = new AlertDialog.Builder(mContext)
.create();
dialog.setTitle("开始巡检");
dialog.setCancelable(false);
//设置自定义view
dialog.setView(view1);
dialog.show();
//取消按钮监听点击事件
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
//确定按钮监听点击事件
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// if (spPerson.getText().toString().equals("00000000"))
// {
// Toast.makeText(mContext,"密码正确!",Toast.LENGTH_SHORT).show();
// dialog.dismiss();
// }
// else
// {
// Toast.makeText(mContext,"密码错误,请重新输入!",Toast.LENGTH_SHORT).show();
// inputPwd.setText("");
// }
}
});
}
/**
* 解析JSON文件的简单数组
*/
private ArrayList parseJsonArray(String fileName) throws Exception{
ArrayList itemsList = new ArrayList();
// String jsonStr = getStringFromAssert(mContext, fileName);
// if(jsonStr.equals("")){
// return null;
// }
// JSONObject allData = new JSONObject(jsonStr); //全部内容变为一个项
// JSONArray jsonArr = allData.getJSONArray(LISTROOTNODE); //取出数组
// for(int x = 0;x