2015-12-23 2 views
0

Я хочу показать диалог прогресса в адаптере, такой как 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(); 

\t public class ExpandListAdapter2 extends BaseExpandableListAdapter implements View.OnClickListener { 
 

 
\t \t Context context; 
 
\t \t LayoutInflater inflater; 
 

 
\t \t private List<Bank> banks=null; 
 

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

 

 
\t \t String MapWalking; 
 
\t \t String MapDriving; 
 
\t \t String MapTransit; 
 

 

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

 

 

 

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

 

 

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

 

 
\t \t \t } 
 

 
\t \t } 
 

 

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

 
\t \t \t TextView Name; 
 
\t \t \t ImageView falg; 
 

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

 

 

 
\t \t \t } 
 

 

 
\t \t } 
 

 
\t \t public Object getChild(int groupPosition, int childPosition) { 
 

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

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

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

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

 

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

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

 

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

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

 
} 
 
} 
 

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

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

 
\t \t \t String dura = ""; 
 
\t \t \t try { 
 

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

 
\t \t \t \t // Output format 
 

 

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

 

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

 
\t \t \t \t HttpPost httppost = new HttpPost(url); 
 

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

 

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

 
\t \t \t JSONObject jsonObject = new JSONObject(); 
 
\t \t \t try { 
 

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

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

 
\t \t \t \t JSONObject routes = array.getJSONObject(0); 
 

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

 
\t \t \t \t JSONObject steps = legs.getJSONObject(0); 
 

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

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

 
\t \t \t return dura; 
 
\t \t }

+0

пожалуйста, напишите свой код в BaseExpandableListAdapter или BaseAdapter и код, который вы получите данные из Google API –

+0

@ PhanVănLinh Я отправил BaseExpandableListAdapter и функцию, чтобы получить продолжительность от Google направления апи. но обратите внимание, что адаптер внутри моего основного класса активности, тот же файл класса Java. – Mariam

ответ

0

В ваших BaseAdapter сделать ссылку на контекст:

private Context mContext = null; 

И добавить конструктор

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

Тогда refere контекст сти при инициализации вашего прогресса диалогового

pDialog = ProgressDialog.show(mContext, "dialog title", 
       "dialog message", false); 
Смежные вопросы