2016-01-19 1 views
-1

Je reçois des résultats du serveur lorsque l'utilisateur tape sa requête. Initialement, je tirais une requête sur chaque personnage entré. Maintenant, j'ai mis un délai de 500 ms après que l'utilisateur tape dans son dernier caractère & puis je tire la requête pour récupérer les résultats du serveur. Je reçois l'erreur indiqué dans la question sur la ligne suivante, à l'intérieur onSuccess():

search_listview.setAdapter(searchAdaptor); 

Voici mon code:

@Override 
public void afterTextChanged(Editable s) { 
if(!s.equals("")&&s.length()!=0){ 
     TextView tv_emptyView = (TextView)findViewById(R.id.tv_emptyView); 
     tv_emptyView.setVisibility(View.GONE); 
     if(frndList!=null&&frndList.size()!=0) 
     frndList.removeAll(frndList); 
     if(searchAdaptor!=null){ 
      searchAdaptor.notifyDataSetChanged(); 
     } 
     search_listview.setVisibility(View.VISIBLE); 
     pageNumber=0; 

    timer.cancel(); 
    timer = new Timer(); 
    timer.schedule(
      new TimerTask() { 
       @Override 
       public void run() { 
        getListbyName(++pageNumber); 
       } 
      }, 
      DELAY 
    ); 
}else if(s.equals("")||s.length()==0){ 
     TextView tv_emptyView = (TextView)findViewById(R.id.tv_emptyView); 
     tv_emptyView.setVisibility(View.VISIBLE); 
     search_listview.setVisibility(View.GONE); 
    } 

} 

private void getListbyName(int page) { 
    GlobalSearchRequestHandler globalSearchRequest = new GlobalSearchRequestHandler(
      this, this, UserInfoSettings.INSTANCE.getUserId(), 
      editTextSearch.getText().toString(), page + "", true,progressBar); 
    globalSearchRequest.execute(); 
} 
@Override 
public void onSuccess(Object response) { 

    if (response instanceof GlobalSearchBean) { 
     GlobalSearchBean advSearchResultbean = (GlobalSearchBean) response; 
     if (advSearchResultbean.getErrorCode().equalsIgnoreCase("000")) { 
      frndList = advSearchResultbean.getUserList(); 
       if (frndList != null && frndList.size() > 0) { 
       loading = true; 
       if(searchAdaptor!=null&& pageNumber!=1){ 
        searchAdaptor.updateFriendList(frndList); 
       }else{ 
        searchAdaptor = new SmartEntourageSearchAdaptor(this, AdvanceSearchScreenActivity.this, frndList, true); 
        search_listview.setAdapter(searchAdaptor); 
        searchAdaptor.notifyDataSetChanged(); 
       } 
      } else{ 
       if (searchAdaptor != null && pageNumber != 1) { 
        loading = true; 
        searchAdaptor.updateFriendList(frndList); 
       } else { 
        TextView tv_emptyView = (TextView) findViewById(R.id.tv_emptyView); 
        tv_emptyView.setVisibility(View.VISIBLE); 
        search_listview.setVisibility(View.GONE); 
       } 
      } 
     } 
    } 
} 

Je suis allé par & essayé réponses sur stackoverflow mais ne fonctionne pas.

EDIT: GlobalSearchRequestHandler:

public class GlobalSearchRequestHandler extends JsonHttpResponseHandler { 

private Context mContext; 
//private Dialog mDialog; 
private View progressBar; 
private String encodedRequest; 
private HttpCommunicationListener mHandler; 
private boolean mShowProgress; 
private static final String contentType = "application/x-www-form-urlencoded"; 

public GlobalSearchRequestHandler(Context mContext, 
     HttpCommunicationListener handler, String userId, String keyword, 
     String pageNumber, boolean canShowProgress,View pb) { 
    this.mContext = mContext; 
    this.encodedRequest = encodedRequest(userId, keyword, pageNumber); 
    this.mHandler = handler; 
    this.mShowProgress = canShowProgress; 
    this.progressBar= pb; 
} 

private String encodedRequest(String userId, String keyword, 
     String pageNumber) { 

    JSONObject genInfoObj = new JSONObject(); 
    JSONObject globalSearch = new JSONObject(); 

    globalSearch.put("userId", userId); 
    globalSearch.put("keyword", MysnlUtils.replaceSpecialCharacterEncodeRequest(keyword)); 
    globalSearch.put("pageNumber", pageNumber); 

    genInfoObj.put("globalSearch", globalSearch); 
    genInfoObj.put(getString(R.string.genInfo), MysnlUtils.getGenInfo()); 

    return genInfoObj.toString(); 
} 

private String getString(int id) { 
    return MysnlApplication.mAppContext.getResources().getString(id); 
} 

public void execute() { 
    try { 
     encodedRequest = "xmlrequest=" + encodedRequest; 
     Logger.e(encodedRequest); 
     StringEntity entity = new StringEntity(encodedRequest); 
     AsyncHttpClient client = new AsyncHttpClient(); 

     KeyStore trustStore = KeyStore.getInstance(KeyStore 
       .getDefaultType()); 
     trustStore.load(null, null); 
     MySSLSocketFactory sf = new MySSLSocketFactory(trustStore); 
     sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 
     client.setSSLSocketFactory(sf); 
     /* 
     * final int DEFAULT_TIMEOUT = 20 * 1000; 
     * client.setTimeout(DEFAULT_TIMEOUT); 
     */ 

     client.post(MysnlApplication.mAppContext, 
       getString(R.string.php_server_url), entity, contentType, 
       this); 
    } catch (Exception e) { 
     Logger.e(e.getMessage()); 
     ErrorBean errorBean = new ErrorBean(); 
     errorBean.setErrorCode(AppConstant.BAD_REQUEST); 
     errorBean.setErrorMsg(e.getMessage()); 
     mHandler.onFail(errorBean); 
    } 
} 

@Override 
public void onSuccess(org.json.JSONObject response) { 
    Logger.e("onSuccess : "+response.toString());  

    GlobalSearchBean bean = null; 
    try { 
     org.json.JSONObject headJsonObject = (org.json.JSONObject) response.get("globalSearch"); 
     Gson gson = new Gson(); 
     bean = gson.fromJson(headJsonObject.toString(), GlobalSearchBean.class); 
     if (bean != null) { 
      mHandler.onSuccess(bean); 
     } 
     else{ 
      mHandler.onFail(bean); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

@Override 
public void onFailure(Throwable error, String content) { 
    Logger.e("onFailure : "+content.toString()); 
    ErrorBean errorBean=new ErrorBean(); 
    errorBean.setErrorCode(AppConstant.BAD_REQUEST); 
    errorBean.setErrorMsg(content); 
    mHandler.onFail(errorBean); 
    try { 
     progressBar.setVisibility(View.GONE); 
     /*if (mDialog != null && mDialog.isShowing()) { 
      mDialog.dismiss(); 
     }*/ 
    } catch (Exception e) { 
    } 

    super.onFailure(error, content); 
} 

@Override 
public void onStart() { 
    try { 
     if(mShowProgress){ 
      progressBar.setVisibility(View.VISIBLE); 
      /*mDialog = MysnlUtils.getProgressDialog(mContext); 
      mDialog.show();*/ 
     } 
    } catch (Exception e) { 
    } 

    super.onStart(); 
} 

@Override 
public void onFinish() { 
    try { 
     progressBar.setVisibility(View.GONE); 
     /*if (mDialog != null && mDialog.isShowing()) { 
      mDialog.dismiss(); 
     }*/ 
    } catch (Exception e) { 
    } 

    super.onFinish(); 
} 

}

+0

affichez votre Asynctask GlobalSearchRequestHandler – Rajesh

+0

Copie possible de [Android "Seul le thread d'origine qui a créé une hiérarchie de vues peut toucher ses vues."] (Http://stackoverflow.com/questions/5161951/android-only-the-original-thread -that-créé-une-vue-hiérarchie-peut-toucher-sa-vi) – Selvin

+0

@Selvin J'ai dit que j'ai essayé des solutions données sur stackoverflow. –

Répondre

1

Remplacer ceci:

timer.cancel(); 
timer = new Timer(); 
timer.schedule(
     new TimerTask() { 
      @Override 
      public void run() { 
       getListbyName(++pageNumber); 
      } 
     }, 
     DELAY 
); 

Avec ceci:

search_listview.postDelayed(new Runnable() { 
    @Override 
    public void run() { 
     getListbyName(++pageNumber); 
    } 
}, DELAY); 
+0

Cela ne m'aide pas. En effet, il envoie une requête sur chaque personnage entré et n'attend pas 500ms. –

+0

Vous pouvez stocker le runnable et le retirer du gestionnaire si vous le souhaitez. Ou utilisez simplement runOnUiThread avec votre propre solution de temporisation. –

+0

Je ne l'ai pas fait avant. Assez clueless. Mais je dois résoudre ce problème depuis que j'ai été affecté dans mon bureau. Je serai utile si je reçois un prix de code pour résoudre ce problème. –