用java(機器人工作室)如何使用處理器和聽一個線程響應
我有這個線程
void obtenerDatosBackground() {
Thread EnviarCoordenadasHilo = new Thread(new Runnable() {
public void run() {
obtenerDatos();
}
});
statusSync.CheckConnection(EnviarCoordenadasHilo);
}
數據線程發送到UiThread,我有這個在我的代碼:
void configHandler()
{
//con esto le digo al handler que esta adjuntado a la UI
handler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
}
但我不知道如何設置此處理程序的線程。
最後我用一個偵聽獲取結果,並更新用戶界面,但我得到這個消息:
只有創建視圖層次可以觸摸它 享有原來的線程。
我已閱讀(略)有關handler,但我怎麼能設置一個處理程序(非UI)線程更新UI時,我得到的結果
我的代碼改變這一點,但它但不工作
void obtenerDatosBackground()
{
Thread EnviarCoordenadasHilo = new Thread(new Runnable() {
public void run() {
obtenerDatos();
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() { //do what you want to do here
int uno=1;
}
});
}
});
statusSync.CheckConnection(EnviarCoordenadasHilo);
}
記住 我使用的是偵聽線程響應
void setmListado()
{
adapter = new VehiculoAdapter(getActivity(), mListado);
lvListado.setAdapter(adapter);
lvListado.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
vehicleObject = (Vehiculo) adapter.getItem(i);
edtEconomicNumber=vehicleObject.getNumeroEconomico();
if(mListener!=null)
mListener.onDismissResult(vehicleObject,edtEconomicNumber);
else
Log.w(getTag(),"Falta implementar OnFragmentInteractionListener");
dismiss();
}
});
txtBuscar.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
//f_car_listado.this.adapter.getFilter().filter(arg0);
//boolean uno=false;
if (count < before) {
// We're deleting char so we need to reset the adapter data
adapter.resetData();
}
adapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
在這裏我得到了來自線程responde
@Override
public void onFragmentFinished(boolean terminoBien, List<VehiculoResponseDTO> listado) {
dialogFragment.dismiss();
if(terminoBien)
{
if(listado.size()>0) {
mListado = ListadoDTOaVehiculos(listado);
//adapter = new VehiculoAdapter(getActivity(), mListado);
//lvListado.setAdapter(adapter);
setmListado();
//adapter.notifyDataSetChanged();
}
else {
this.dismiss();
f_seguimiento.DialogMensaje(getString(R.string.nohayvehiculos), getString(R.string.nohayvehiculosMensaje), true, null, getActivity());
}
}else
{
this.dismiss();
f_seguimiento.DialogMensaje(getString(R.string.ocurrioProblema),getString(R.string.errorVehiculos),true,null,getActivity());
}
}
在這個代碼在哪裏線程? – angel
只需從要更新視圖的地方調用上述代碼即可。 – Ashish
空隙obtenerDatosBackground(){ 螺紋EnviarCoordenadasHilo =新主題(新的Runnable(){ 公共無效的run(){ obtenerDatos();處理程序的處理程序=新處理程序(Looper.getMainLooper()); handler.post (新的Runnable(){@Override 公共 無效的run(){// 你想在這裏做 } 什麼});} }); statusSync.CheckConnection(EnviarCoordenadasHilo); } – Ashish