2017-10-13 11 views
0

내 응용 프로그램 용 위젯을 만들었지 만 목록보기를 채워야 할 때 막혀 있습니다. 나는 나의 WidgetDataProvider (RemoteViewsService.RemoteViewsFactory를 구현)의 onCreate()에서 호출 한 asynctask를 사용하여 웹 데이터베이스의 모든 항목을 가져온다. 문제는 보통 위젯을 만들 때 목록을 채우지 않아 홈 화면에서 위젯을 놓기 전에 몇 초 기다려야한다는 것입니다.AsyncTask에서 채워진 ListView가있는 Android 위젯

내가 잡아하고 다음과 같이 표시되는 위젯을 해제하는 경우 :

AppWidget

: 여기

right widget 내 코드입니다 : 나는 바로 다음과 같이 나타납니다 기다리면 empty widget

public class AppWidget extends AppWidgetProvider { 
static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) { 
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.app_widget); 

    Intent intent = new Intent(context, LoginActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 
    views.setOnClickPendingIntent(R.id.widget_title, pendingIntent); 

    views.setRemoteAdapter(R.id.widget_listView, new Intent(context, WidgetService.class)); 
    appWidgetManager.updateAppWidget(appWidgetId, views); 
} 

@Override 
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
    super.onUpdate(context, appWidgetManager, appWidgetIds); 
    for (int appWidgetId : appWidgetIds) { 
     updateAppWidget(context, appWidgetManager, appWidgetId); 
    } 
} 

@Override 
public void onEnabled(Context context) { 
} 

@Override 
public void onDisabled(Context context) { 
} 

}

** WidgetService **

public class WidgetService extends RemoteViewsService { 
@Override 
public RemoteViewsFactory onGetViewFactory(Intent intent) { 
    return new WidgetDataRemote(this, intent); 
}} 

WidgetDataRemote

public class WidgetDataRemoteimplements RemoteViewsService.RemoteViewsFactory { 
Context context; 
Intent intent; 
ArrayList<String> date = new ArrayList<>(); 
ArrayList<String> subj = new ArrayList<>(); 
ArrayList<String> type = new ArrayList<>(); 
ArrayList<String> comment = new ArrayList<>(); 
JSONParser jsonParser = new JSONParser(); 
ArrayList<String> subjFromDb = new ArrayList<>(); 
int pippo = 0; 

public WidgetDataRemote(Context context, Intent intent) { 
    this.context = context; 
    this.intent = intent; 
} 

@Override 
public RemoteViews getViewAt(int position) { 
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.item_widget); 
    remoteViews.setTextViewText(R.id.task_data, date.get(position)); 
    remoteViews.setTextViewText(R.id.task_subject, subj.get(position)); 
    remoteViews.setTextViewText(R.id.task_type, type.get(position)); 
    remoteViews.setTextViewText(R.id.task_comment, comment.get(position)); 
    return remoteViews; 
} 

@Override 
public void onCreate() { 
    new LoadSubjects().execute(); 
} 

@Override 
public void onDataSetChanged() { 
} 

@Override 
public void onDestroy() { 
    date.clear(); 
    subj.clear(); 
    type.clear(); 
    comment.clear(); 
} 

@Override 
public int getCount() { 
    return date.size(); 
} 

@Override 
public RemoteViews getLoadingView() { 
    return null; 
} 

@Override 
public int getViewTypeCount() { 
    return 1; 
} 

@Override 
public long getItemId(int id) { 
    return id; 
} 

@Override 
public boolean hasStableIds() { 
    return true; 
} 

class LoadSubjects extends AsyncTask<String, String, String> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     subjFromDb.clear(); 
     pippo = 0; 
    } 

    @Override 
    protected String doInBackground(String... args) { 
     List<NameValuePair> params = new ArrayList<>(); 
     params.add(new BasicNameValuePair("class", User.getClasse())); 
     JSONArray json = jsonParser.makeHttpRequestArray(Constants.URL_LOAD_SUBJECTS, "POST", params); 
     try { 
      if (!json.toString().equals("[null]")) { 
       for (int i = 0; i < json.length(); i++) { 
        JSONObject c = json.getJSONObject(i); 
        String subj = c.getString(Constants.TAG_NAME); 
        subjFromDb.add(subj); 
       } 
      } else { 
       pippo = 1; 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    protected void onPostExecute(String file_url) { 
     if (pippo == 0) { 
      new LoadTasks().execute(); 
     } 
    } 

    class LoadTasks extends AsyncTask<String, String, String> { 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pippo = 0; 
     } 

     @Override 
     protected String doInBackground(String... args) { 
      List<NameValuePair> params = new ArrayList<>(); 
      params.add(new BasicNameValuePair("id_class", User.getClasse())); 
      JSONArray json = jsonParser.makeHttpRequestArray(Constants.URL_LOAD_TASKS, "POST", params); 
      try { 
       if (!json.toString().equals("[null]")) { 
        for (int i = 0; i < json.length(); i++) { 
         JSONObject c = json.getJSONObject(i); 
         Date data = null; 
         try { 
          data = new SimpleDateFormat("yyyy-MM-dd").parse(c.getString(Constants.TAG_DATE)); 
         } catch (ParseException e) { 
          e.printStackTrace(); 
         } 
         String newDate = new SimpleDateFormat("dd/MM/yyyy").format(data); 
         if (subjFromDb.contains(c.getString(Constants.TAG_SUBJECT))) { 
          date.add(newDate); 
          subj.add(c.getString(Constants.TAG_SUBJECT)); 
          type.add(c.getString(Constants.TAG_TYPE)); 
          comment.add(c.getString(Constants.TAG_COMMENT)); 
         } 
        } 
       } else { 
        pippo = 1; 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     protected void onPostExecute(String file_url) { 
      if (pippo == 0){ 
       AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 
       ComponentName thisWidget = new ComponentName(context, WidgetDataProvider.class); 
       int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); 
       appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_listView); 
       // I use this to update the listview after fetch data. Is it correct? 
      } 
     } 
    } 
}} 

답변

0

나는 단지의 TimerTask를 사용하고 그가 onPostExecute 업데이트 프로세스 INT 수정 내 문제를 해결했다.

WidgetDataRemote

public class WidgetDataRemote implements RemoteViewsService.RemoteViewsFactory { 
final Context context; 
Intent intent; 
ArrayList<String> date = new ArrayList<>(); 
ArrayList<String> subj = new ArrayList<>(); 
ArrayList<String> type = new ArrayList<>(); 
ArrayList<String> comment = new ArrayList<>(); 
JSONParser jsonParser = new JSONParser(); 
ArrayList<String> subjFromDb = new ArrayList<>(); 
final Context sad; 
int pippo = 0; 

public WidgetDataRemote(Context context, Intent intent) { 
    this.context = context; 
    this.intent = intent; 
    sad = context; 
    final Handler handler = new Handler(); 
    Timer timer = new Timer(); 
    TimerTask doAsynchronousTask = new TimerTask() { 
     @Override 
     public void run() { 
      handler.post(new Runnable() { 
       public void run() { 
        try { 
         new LoadSubjects().execute(); 
        } catch (Exception ignored) {} 
       } 
      }); 
     } 
    }; 
    timer.schedule(doAsynchronousTask, 0, 60000); 
} 

@Override 
public RemoteViews getViewAt(int position) { 
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.item_widget); 
    if(date.size()>0) { 
     remoteViews.setTextViewText(R.id.task_data, date.get(position)); 
     remoteViews.setTextViewText(R.id.task_subject, subj.get(position)); 
     remoteViews.setTextViewText(R.id.task_type, type.get(position)); 
     remoteViews.setTextViewText(R.id.task_comment, comment.get(position)); 
    }else{ 
     remoteViews.setTextViewText(R.id.task_data, ""); 
     remoteViews.setTextViewText(R.id.task_subject, "loading"); 
     remoteViews.setTextViewText(R.id.task_type, ""); 
     remoteViews.setTextViewText(R.id.task_comment, ""); 
    } 
    return remoteViews; 
} 

@Override 
public void onCreate() { 
    Toast.makeText(context, "Loading...", Toast.LENGTH_LONG).show(); 
} 

@Override 
public void onDataSetChanged() { 
} 

@Override 
public void onDestroy() { 
    date.clear(); 
    subj.clear(); 
    type.clear(); 
    comment.clear(); 
} 

@Override 
public int getCount() { 
    return date.size(); 
} 

@Override 
public RemoteViews getLoadingView() { 
    return null; 
} 

@Override 
public int getViewTypeCount() { 
    return 1; 
} 

@Override 
public long getItemId(int id) { 
    return id; 
} 

@Override 
public boolean hasStableIds() { 
    return true; 
} 

class LoadSubjects extends AsyncTask<String, String, String> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     subjFromDb.clear(); 
     pippo = 0; 
    } 

    @Override 
    protected String doInBackground(String... args) { 
     List<NameValuePair> params = new ArrayList<>(); 
     params.add(new BasicNameValuePair("class", User.getClasse())); 
     JSONArray json = jsonParser.makeHttpRequestArray(Constants.URL_LOAD_SUBJECTS, "POST", params); 
     try { 
      if (!json.toString().equals("[null]")) { 
       for (int i = 0; i < json.length(); i++) { 
        JSONObject c = json.getJSONObject(i); 
        String subj = c.getString(Constants.TAG_NAME); 
        subjFromDb.add(subj); 
       } 
      } else { 
       pippo = 1; 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    protected void onPostExecute(String file_url) { 
     if (pippo == 0) { 
      new LoadTasks().execute(); 
     } 
    } 
} 

class LoadTasks extends AsyncTask<String, String, String> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     date.clear(); 
     subj.clear(); 
     type.clear(); 
     comment.clear(); 
     pippo = 0; 
    } 

    @Override 
    protected String doInBackground(String... args) { 
     List<NameValuePair> params = new ArrayList<>(); 
     params.add(new BasicNameValuePair("id_class", User.getClasse())); 
     JSONArray json = jsonParser.makeHttpRequestArray(Constants.URL_LOAD_TASKS, "POST", params); 
     try { 
      if (!json.toString().equals("[null]")) { 
       for (int i = 0; i < json.length(); i++) { 
        JSONObject c = json.getJSONObject(i); 
        Date data = null; 
        try { 
         data = new SimpleDateFormat("yyyy-MM-dd").parse(c.getString(Constants.TAG_DATE)); 
        } catch (ParseException e) { 
         e.printStackTrace(); 
        } 
        String newDate = new SimpleDateFormat("dd/MM/yyyy").format(data); 
        if (subjFromDb.contains(c.getString(Constants.TAG_SUBJECT))) { 
         date.add(newDate); 
         subj.add(c.getString(Constants.TAG_SUBJECT)); 
         type.add(c.getString(Constants.TAG_TYPE)); 
         comment.add(c.getString(Constants.TAG_COMMENT)); 
        } 
       } 
      } else { 
       pippo = 1; 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    protected void onPostExecute(String file_url) { 
     AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(sad); 
     appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetManager.getAppWidgetIds(new ComponentName(sad, AppWidget.class)), R.id.widget_listView); 
    } 
}} 
: 여기 변경 한 파일입니다