在适配器中显示进度对话框

时间:2015-12-23 00:24:02

标签: android adapter progressdialog

我想在适配器中显示进度对话框,例如BaseExpandableListAdapter和BaseAdapter,我从Google api获取一些数据,这需要时间,以便更好地显示对用户的响应。我不知道它没有显示,但我在活动中添加了相同的代码并且它正在工作。

pDialog = ProgressDialog.show(MapActivity.this, "dialog title",
                    "dialog message", false);
pDialog.setContentView(R.layout.pgress);
new Thread(new Runnable() {
    @Override
    public void run()
    {
        // do the thing that takes a long time

        MapWalking = getDistanceInfo(p, dest);
        MapDriving = getDistanceInfo2(p, dest);
        MapTransit = getDistanceInfo3(p, dest);

        runOnUiThread(new Runnable() {
            @Override
            public void run()
            {
                pDialog.dismiss();
            }
        });
    }
}).start();



	public class ExpandListAdapter2 extends BaseExpandableListAdapter implements View.OnClickListener {

		Context context;
		LayoutInflater inflater;

		private List<Bank> banks=null;

		private List<AnywallPost> AnywallPostlist2 = null;
		private ArrayList<AnywallPost> arraylist;


		String MapWalking;
		String MapDriving;
		String MapTransit;


		public ExpandListAdapter2(Context context,
								  List<AnywallPost> AnywallPostlist2) {
			this.context = context;
			this.AnywallPostlist2 = AnywallPostlist2;
			inflater = LayoutInflater.from(context);
			this.arraylist = new ArrayList<AnywallPost>();
			this.arraylist.addAll(AnywallPostlist2);




			for (AnywallPost bank2 : AnywallPostlist2) {
				bank2.setChildren(new ArrayList<AnywallPost>());



				bank2.getChildren().add(bank2);
				//Log.d("array",branch.get("NameVal").toString());


			}

		}


		public class ViewHolder {
			TextView TVal;
			SegmentedGroup segmented;
			Button book;
			TextView lt111;
			TextView lt222;
			TextView lt333;

			TextView Name;
			ImageView falg;

			public ViewHolder(View v) {
				this.TVal = (TextView) v.findViewById(R.id.textView29);
				this.segmented = (SegmentedGroup) v.findViewById(R.id.segmentedmode);
				this.lt111 = (TextView) v.findViewById(R.id.textViewlt1);
				this.lt222 = (TextView) v.findViewById(R.id.textViewlt2);
				this.lt333 = (TextView) v.findViewById(R.id.textViewlt3);
				this.Name=(TextView)v.findViewById(R.id.textViewserviceName);
				this.falg=(ImageView)v.findViewById(R.id.imageViewserviceImg);
				this.book=(Button)v.findViewById(R.id.button);



			}


		}

		public Object getChild(int groupPosition, int childPosition) {

			return AnywallPostlist2.get(groupPosition).getChildren().get(childPosition);
		}

		public void onGroupExpanded(int groupPosition) {
			for(int i=0;i<AnywallPostlist2.size();i++){
				if(i==groupPosition){
					System.out.println("Nothing");
	
				}
				else{
					lv.collapseGroup(i);
				}
				
			}
			super.onGroupExpanded(groupPosition);
		}
		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
								 boolean isLastChild, View convertView, ViewGroup parentView) {
			final AnywallPost parent2 = AnywallPostlist2.get(groupPosition);
			final AnywallPost child = parent2.getChildren().get(childPosition);

			convertView = inflater.inflate(R.layout.spinner_itemfinal3, parentView, false);


			/*final TextView TVal = (TextView) convertView.findViewById(R.id.textView29);
			SegmentedGroup segmented=(SegmentedGroup) convertView.findViewById(R.id.segmentedmode);
			lt11 = (TextView) convertView.findViewById(R.id.textViewlt1);
			lt22=(TextView)convertView.findViewById(R.id.textViewlt2);
			lt33=(TextView)convertView.findViewById(R.id.textViewlt3);
			Button book=(Button)convertView.findViewById(R.id.button);*/
			sid = parent2.get("Sid").toString();
			holder = new ViewHolder(convertView);
			holder.lt111.setOnClickListener(this);
			holder.lt222.setOnClickListener(this);
			holder.lt333.setOnClickListener(this);

			pDialog = ProgressDialog.show(MapActivity.this, "dialog title",
					"dialog message", false);
			pDialog.setContentView(R.layout.pgress);
			new Thread(new Runnable() {
				@Override
				public void run()
				{
					// do the thing that takes a long time
// do the thing that takes a long time


					MapWalking = getDistanceInfo(p, dest);
					MapDriving = getDistanceInfo2(p, dest);
					MapTransit = getDistanceInfo3(p, dest);

					runOnUiThread(new Runnable() {
						@Override
						public void run()
						{
							pDialog.dismiss();
						}
					});
				}
			}).start();
			

}
}

public String getDistanceInfo(LatLng origin, LatLng dest) {
			StringBuilder stringBuilder = new StringBuilder();
			String str_origin = "origin=" + origin.latitude + "," + origin.longitude;

			// Destination of route
			String str_dest = "destination=" + dest.latitude + "," + dest.longitude;

			String dura = "";
			try {

				String sensor = "sensor=false";
				String output = "json";
				String mode = "mode=walking";
				String parameters = str_origin + "&" + str_dest + "&" + sensor + "&" + mode;
				String key="key=AIzaSyAQokRsF58j9EeK9VsOgFFgU3IGEpoCSfM";

				// Output format


				// Building the url to the web service
				String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + parameters +"&"+ key;


				//String url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + str_origin + "," + str_dest + "&destination=" + destinationAddress + "&mode=driving&sensor=false";

				HttpPost httppost = new HttpPost(url);

				HttpClient client = new DefaultHttpClient();
				HttpResponse response;
				stringBuilder = new StringBuilder();


				response = client.execute(httppost);
				HttpEntity entity = response.getEntity();
				InputStream stream = entity.getContent();
				int b;
				while ((b = stream.read()) != -1) {
					stringBuilder.append((char) b);
				}
			} catch (ClientProtocolException e) {
			} catch (IOException e) {
			}

			JSONObject jsonObject = new JSONObject();
			try {

				jsonObject = new JSONObject(stringBuilder.toString());

				JSONArray array = jsonObject.getJSONArray("routes");

				JSONObject routes = array.getJSONObject(0);

				JSONArray legs = routes.getJSONArray("legs");

				JSONObject steps = legs.getJSONObject(0);

				JSONObject duration = steps.getJSONObject("duration");
				dura = duration.getString("text");

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return dura;
		}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在BaseAdapter中引用上下文:

private Context mContext = null;

添加构造函数

public MyListAdapter(Context context)
{
    this.mContext = context;
}

然后在初始化进度对话框时引用上下文

pDialog = ProgressDialog.show(mContext, "dialog title",
                "dialog message", false);