How to pass data from AsyncTask to Ui Thread - android

I'm implementing an asyncTask class to load the data from server.
I have the data already and I want to pass it to the UI Thread, but I don't know how.
This is the code:
public class InboxHandler extends FragmentActivity implements OnLineSelectedListener {
static final int GET_CODE = 0;
private TextView textView3, textView2;
private Button button1, button2, button3;
private LinearLayout tab1, tab2, tab3;
private CheckBox cbVerPorCategoria;
private ActionBar actionBar;
private TextView txtTitle;
private Settings settings;
FragmentTransaction fragmentTransaction;
BottomPanel bottomPanel;
public List<OrderRequest> orderRequestArray;
private OrderRequestParser orderRequestParser;
public static int number;
int clickCounter = 0;
private boolean doubleBackToExitPressedOnce = false;
private static Context context;
DataLoader dataLoader;
private MyAsynckTask myAsynckTask;
public InboxHandler(){}
public List<OrderRequest> getOrderRequestArray() {
return orderRequestArray;
}
public void setOrderRequestArray(List<OrderRequest> orderRequestArray) {
this.orderRequestArray = orderRequestArray;
}
public static Context getContext()
{
return context;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inbox);
context = this.getApplicationContext();
settings = Settings.getInstance();
settings.setPrefs(getSharedPreferences(settings.getPREFERENCES(), Activity.MODE_PRIVATE));
settings.setPrefsEditor(settings.getPrefs().edit());
settings.getPrefsEditor().putBoolean("isLoggedIn", true);
settings.isVistaSimple = true;
actionBar = (ActionBar) findViewById(R.id.actionbar);
initComponents();
prepareActionBar();
}
private void initComponents() {
try {
MyAsynckTask myAsynckTask = new MyAsynckTask(this);
myAsynckTask.execute();
//HERE I USED A SUGGESTION IN A SIMILAR QUESTION. THAT IS: PASS THE ACTIVITY TO THE ASYNCTASK, AND FROM IT, SAVE IN THE ACTIVITY, THE DATA THAT IU WANT FROM THE ASYNCTASK. I DO THAT, BUT IN THE ACTIVITY, THE DATA IS NULL I SUPPOSE DUE TO, I DONT EVEN SEE IN THE LOGCAT, THE FOLLOWING RESULT:
try{
Log.e("e", ""+this.getOrderRequestArray().get(0).getDocNumber());
} catch(Exception e){
e.printStackTrace();
}
//If you see the onPostExecute, I test if the data I want is there. and it is there! I set that to the list of the class InboxHandler, but in the above code, when i try to test if it is there, it´s not. And that code don´t even appear in logcat. The "Log.e("e", ""+this.getOrderRequestArray().get(0).getDocNumber());" nor the "e.printStackTrace();"
bottomPanel =new BottomPanel();
Bundle bundle = new Bundle();
bundle.putSerializable("arrayPedidos", (Serializable) orderRequestArray);
bottomPanel.setArguments(bundle);
//actionBar.setTitle("Inbox");
NotificationFragment notificationFragment = new NotificationFragment(orderRequestArray, actionBar);
Bundle bundleNotificationFragment = new Bundle();
bundleNotificationFragment.putString("title", "Inbox");
notificationFragment.setArguments(bundleNotificationFragment);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.frame_list_container, notificationFragment);
fragmentTransaction.add(R.id.frame_bottom_panel, bottomPanel);
fragmentTransaction.commit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private class MyAsynckTask extends AsyncTask {
private String stringMensaje;
public List<OrderRequest> orderRequestArray;
private OrderRequestParser orderRequestParser;
private Request request;
private Settings settings;
private InboxHandler inboxHandler;
public MyAsynckTask(InboxHandler inboxHandler){
this.inboxHandler = inboxHandler;
}
#Override
protected Object doInBackground(Object... params) {
List<OrderRequest> array = this.getOrderRequests();
settings = Settings.getInstance();
if(array != null){
settings.setOrderRequestArray(array);
orderRequestArray = array;
Log.e("orderRequestArray", "Me trajo el orderRequestArray en doInBackground!");
} else {
Log.e("orderRequestArray", "No me trajo el orderRequestArray en doInBackground!");
}
return array;
}
#Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
List<OrderRequest> array = (List<OrderRequest>) result;
this.setOrderRequestArray(array);
if(orderRequestArray != null){
inboxHandler.setOrderRequestArray(orderRequestArray);
Log.e("orderRequestArray", "is not null en onPostExecute!");
} else {
Log.e("orderRequestArray", "is null en onPostExecute!");
}
if(inboxHandler.getOrderRequestArray() != null){
Log.e("inboxHandler.getOrderRequestArray()", "is not null en onPostExecute!");
}
else {
Log.e("orderRequestArray", "is null en onPostExecute!");
}
}
public List<OrderRequest> getOrderRequests() {
orderRequestParser = new OrderRequestParser();
orderRequestArray = new ArrayList<OrderRequest>();
List<OrderRequest> orderRequestArray = orderRequestParser.listOrderRequest();
Settings.getInstance().setOrderRequestArray(orderRequestArray);
if(orderRequestArray != null){
Log.e("orderRequestArray", "Me trajo el orderRequestArray en getOrderRequestArray!");
} else {
Log.e("orderRequestArray", "No me trajo el orderRequestArray en getOrderRequestArray!");
}
return orderRequestArray;
}
public void setOrderRequestArray(List<OrderRequest> orderRequestArray) {
this.orderRequestArray = orderRequestArray;
}
public List<OrderRequest> getOrderRequestArray() {
return orderRequestArray;
}
}

protected void onPostExecute(Object result)
it is running in GUI thread, you can access GUI elements there.
and only there should be the:
try{
Log.e("e", ""+this.getOrderRequestArray().get(0).getDocNumber());
} catch(Exception e){
e.printStackTrace();
}
and
Bundle bundle = new Bundle();
bundle.putSerializable("arrayPedidos", (Serializable) orderRequestArray);
bottomPanel.setArguments(bundle);
I can't see any reason, why is this question down voted.

Related

how to synchronise Between class controller contains AsyncTask and fragment

I am very very tired
I can't change visibility or an object in the fragment from the class controller
exmple addIteamsAutomatic.progressBar.setVisibility(View.GONE); return nullpointer
FragmentAddIteamsAutomatic :
public class FragmentAddIteamsAutomatic extends Fragment {
private EditText ssid, paswd;
public TextView afichage;
public Button parainage;
public Button validation;
public ProgressBar progressBar ;
public LinearLayout linearLayoutParm;
public static String sSSID,pWD;
private ControllerAddIteam controleAdd=null;
public FragmentAddIteamsAutomatic()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.add_iteams_automatic, container, false);
controleAdd.getInstance(getActivity());
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
ssid = (EditText) view.findViewById(R.id.ssid);
paswd = (EditText) view.findViewById(R.id.password);
parainage = (Button) view.findViewById(R.id.btnParainage);
validation = (Button) view.findViewById(R.id.btnValid);
afichage = (TextView) view.findViewById(R.id.affichage);
linearLayoutParm = (LinearLayout) view.findViewById(R.id.linearLayParam);
progressBar.setVisibility(View.GONE);
afichage.setVisibility(View.GONE);
validation.setVisibility(View.GONE);
parainage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sSSID = ssid.getText().toString();
pWD = paswd.getText().toString();
if (sSSID.equals(""))
Toast.makeText(getActivity(), "Vous Dever Remplir Tous les champs", Toast.LENGTH_LONG).show();
else
parainer();
}
});
validation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
controleAdd.addSwitchToBase();
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
ControllerAddIteam.accesDistant.send("getIteams", new JSONArray());
// finish();
}
});
return view;
}
private void parainer(){
controleAdd.getInstanceExecuteHandle();
}
}
ControllerAddIteam :
public class ControllerAddIteam {
private static ControllerAddIteam instanceAdd = null;
private static Context context;
private static WifiUtils wifiUtils;
public static String SSID = null;
public static AccesDistant accesDistant;
public static Handler mHandler;
public static final ControllerAddIteam getInstance(Context context) {
if (context != null)
ControllerAddIteam.context = context;
if (ControllerAddIteam.instanceAdd == null) {
ControllerAddIteam.instanceAdd = new ControllerAddIteam();
accesDistant = new AccesDistant();
}
return ControllerAddIteam.instanceAdd;
}
public static void getInstanceExecuteHandle() {
new ParainageHandle().execute();
}
static class ParainageHandle extends AsyncTask<String, String, String> {
FragmentAddIteamsAutomatic addIteamsAutomatic=new FragmentAddIteamsAutomatic();
#Override
protected void onPreExecute() {
super.onPreExecute();
addIteamsAutomatic.progressBar.setVisibility(View.GONE);
addIteamsAutomatic.afichage.setVisibility(View.GONE);
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
addIteamsAutomatic.progressBar.setVisibility(View.GONE);
if(s.equals("valid"))
{
addIteamsAutomatic.linearLayoutParm.setVisibility(View.GONE);
addIteamsAutomatic.validation.setVisibility(View.VISIBLE);
addIteamsAutomatic.parainage.setVisibility(View.GONE);
}
else if(s.equals("notvalid"))
{
addIteamsAutomatic.parainage.setVisibility(View.VISIBLE);
}
}
#Override
protected void onProgressUpdate(String... values) {
addIteamsAutomatic.afichage.setVisibility(View.VISIBLE);
addIteamsAutomatic.progressBar.setVisibility(View.VISIBLE);
if (values[0].equals("actwifi")) {
if (values[1].equals("true"))
addIteamsAutomatic.afichage.setText("WIFI DEJA ACTIVEE");
else
addIteamsAutomatic.afichage.setText("ACTIVATION WIFI EN COURS...");
} else if (values[0].equals("scan"))
addIteamsAutomatic.afichage.setText("START SCAN FOR Iteams STiTo ... Please Wait");
else if (values[0].equals("find"))
addIteamsAutomatic.afichage.setText("STiTo : "+getTypeFromSsid(SSID)+" DETECTEE : "+SSID);
else if (values[0].equals("connect"))
addIteamsAutomatic.afichage.setText("CONNECTION WITH " + SSID + "En cours ...");
else if (values[0].equals("connectOk"))
addIteamsAutomatic.afichage.setText("CONNECTION WITH " + SSID + "ETABLISHED");
else if (values[0].equals("connectKo"))
addIteamsAutomatic.afichage.setText("PROBLEM OF CONNECTION WITH " + SSID);
else if (values[0].equals("config")) {
addIteamsAutomatic.afichage.setText("SENDING OF CONFIGURATION TO: "+getTypeFromSsid(SSID)+"AND SAVING DATA");
accesDistant.sendConfig(addIteamsAutomatic.sSSID,addIteamsAutomatic.pWD);
....
You declare fragment in AsyncTask and doesn't call replace or add, it mean this fragment never show and it not call onCreateView
FragmentAddIteamsAutomatic addIteamsAutomatic=new FragmentAddIteamsAutomatic();
Maybe you should pass reference addIteamsAutomatic to class ControllerAddIteam. but please make sure it will be call on MainThread, because AsyncTask has method doInBackground in background Thread. best practice is wrap fragment reference by WeakReference
public class AddIteamActivity extends AppCompatActivity {
ViewPager pager;
TabLayout tab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_iteam);
pager = findViewById(R.id.pager);
tab = findViewById(R.id.tab);
AddIteamsAdapter viewPagerAdapter = new AddIteamsAdapter(getSupportFragmentManager());
pager.setAdapter(viewPagerAdapter);
tab.setupWithViewPager(pager);
}
}

ProgressDialog not showing up within a Fragment

I have the following class:
...
public class FragmentMapa extends Fragment {
/*
* Atributos
*/
private static String LOG_TAG = "FragmentMapa";
private HomeActivity homeActivity;
private GoogleMap mMapa;
private DrawerLayout mDrawer;
private ActionBarDrawerToggle mDrawerToggle;
private ListView mDrawerList;
private ListView mDrawerRightList;
private RelativeLayout mDrawerRelativeLayout;
private String[] mRightDrawerMenuTitles;
private ImageView mDiputacionLogo;
private IncidenciasFetchAsyncTask mFetchIncidenciasTask;
private Incidencias mIs;
private CamarasFetchAsyncTask mFetchCamarasTask;
private Camaras mCams;
private ViabilidadesInvernalesFetchAsyncTask mFetchViabilidadesInvernalesTask;
private ViabilidadesInvernales mVis;
private static LatLng POS_CENTRAL = new LatLng(43.243968,-2.896957);
private static LatLng limiteSurOesteBizkaia = new LatLng(42.895853,-3.594589);
private static LatLng limiteNorEsteBizkaia = new LatLng(43.540351,-2.180099);
private static final LatLngBounds BOUNDS = new LatLngBounds(limiteSurOesteBizkaia, limiteNorEsteBizkaia);
private ArrayList<Marker> markersIncidencias = new ArrayList<Marker>();
private ArrayList<Marker> markersObras = new ArrayList<Marker>();
private ArrayList<Marker> markersCamaras = new ArrayList<Marker>();
private ArrayList<Marker> markersViabilidadInvernal = new ArrayList<Marker>();
/*
* Métodos
*/
public FragmentMapa() {
}
#Override
public void onAttach (Activity activity) {
super.onAttach(activity);
homeActivity = (HomeActivity) activity;
mRightDrawerMenuTitles = getResources().getStringArray(R.array.mapa_submenu_options);
mDrawer = homeActivity.getmDrawer();
mDrawerRightList = homeActivity.getmDrawerRightList();
mDrawerRightList.setAdapter(new ArrayAdapter<String>(
homeActivity.getSupportActionBar().getThemedContext(),
R.layout.rightdrawer_map_list_item,
mRightDrawerMenuTitles
));
mDrawerRightList.setOnItemClickListener(new DrawerItemClickListener());
}
#Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.mapa, container, false);
setupMapIfNeeded();
return rootView;
}
private void setupMapIfNeeded() {
if( mMapa == null ){
SupportMapFragment smf = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapaPrincipal);
if( smf != null ){
//Toast.makeText(getActivity(), "VAAAMOOSS", Toast.LENGTH_SHORT).show();
mMapa = smf.getMap();
}/*else{
Toast.makeText(getActivity(), "smf es null...", Toast.LENGTH_SHORT).show();
}*/
if( mMapa != null ){
setupMap();
}
}
}
private void setupMap() {
//Toast.makeText(getActivity(), "A configurar el mapa!!", Toast.LENGTH_SHORT).show();
CameraPosition camPos;
camPos = new CameraPosition.Builder()
.target(POS_CENTRAL)
.zoom((float) 9.5)
.build();
final CameraUpdate camUpd =
CameraUpdateFactory.newCameraPosition(camPos);
mMapa.animateCamera(camUpd);
mMapa.setOnCameraChangeListener(new OnCameraChangeListener() {
#Override
public void onCameraChange(CameraPosition newPos) {
float maxZoom = 17.0f;
if( !BOUNDS.contains(newPos.target) ){
//Mover la cámara al centro si se
//va más allá de los límites
mMapa.animateCamera(camUpd);
}
if(newPos.zoom > maxZoom){
mMapa.animateCamera(CameraUpdateFactory.zoomTo(maxZoom));
}
}
});
mMapa.setOnInfoWindowClickListener( new OnInfoWindowClickListener(){
public void onInfoWindowClick(Marker aMarker) {
Toast.makeText(getActivity(), "info window pulsado", Toast.LENGTH_SHORT).show();
if( markersCamaras.contains(aMarker) ){
Camara c = mCams.getCamaraByCoord(aMarker.getPosition());
homeActivity.showCameraFragment(c);
}
}
});
}
#Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public void onViewStateRestored (Bundle savedInstanceState) {
super.onViewStateRestored(savedInstanceState);
}
#Override
public void onStart () {
super.onStart();
}
#Override
public void onResume () {
super.onResume();
}
#Override
public void onPause () {
super.onPause();
}
#Override
public void onStop () {
super.onStop();
}
#Override
public void onDestroyView () {
super.onDestroyView();
}
#Override
public void onDestroy () {
super.onDestroy();
}
#Override
public void onDetach () {
super.onDetach();
}
/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final int thePos = position;
mDrawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
#Override
public void onDrawerClosed(View drawerView) {
boolean wasChecked = !mDrawerRightList.isItemChecked(thePos);
//Toast.makeText(homeActivity, "Item pulsado: " + wasChecked, Toast.LENGTH_SHORT).show();
mDrawerRightList.setItemChecked(thePos, !wasChecked);
switch (thePos) {
case 0:
//Incidencias
//Toast.makeText(homeActivity, "Incidencias", Toast.LENGTH_SHORT).show();
if(!wasChecked){
//Toast.makeText(homeActivity, "Incidencias estaba sin pulsar", Toast.LENGTH_SHORT).show();
getIncidenciasObras();
introducirIncidencias();
}else{
//Toast.makeText(homeActivity, "Incidencias estaba pulsado", Toast.LENGTH_SHORT).show();
removeIncidenciasMarkers();
}
break;
case 1:
//Obras
//Toast.makeText(homeActivity, "Obras", Toast.LENGTH_SHORT).show();
if(!wasChecked){
//Toast.makeText(homeActivity, "Obras estaba sin pulsar", Toast.LENGTH_SHORT).show();
getIncidenciasObras();
introducirObras();
}else{
//Toast.makeText(homeActivity, "Obras estaba pulsado", Toast.LENGTH_SHORT).show();
removeObrasMarkers();
}
break;
case 2:
//Cámaras
//Toast.makeText(homeActivity, "Cámaras", Toast.LENGTH_SHORT).show();
if(!wasChecked) {
getCamaras();
introducirCamaras();
}else
removeCamarasMarkers();
break;
case 3:
//Viabilidad invernal
//Toast.makeText(homeActivity, "Viabilidad invernal", Toast.LENGTH_SHORT).show();
if(!wasChecked){
getViabilidadesInvernales();
introducirViabilidadInvernal();
}else
removeViabilidadInvernalMarkers();
break;
default:
//Toast.makeText(homeActivity, "Default", Toast.LENGTH_SHORT).show();
break;
}
}
});
if(mDrawer.isDrawerOpen(Gravity.END))
mDrawer.closeDrawer(mDrawerRightList);
}
}
private void getViabilidadesInvernales(){
mVis = ViabilidadesInvernales.getInstance();
if(mVis.isEmptyViabilidadesInvernales()){
mFetchViabilidadesInvernalesTask = new ViabilidadesInvernalesFetchAsyncTask(homeActivity);
try {
mVis = mFetchViabilidadesInvernalesTask.execute("es").get();
} catch (InterruptedException e) {
Log.e(LOG_TAG, "Error InterruptedException: " + e.getMessage());
e.printStackTrace();
} catch (ExecutionException e) {
Log.e(LOG_TAG, "Error ExecutionException: " + e.getMessage());
e.printStackTrace();
}
}
}
...
private void introducirViabilidadInvernal() {
if(markersViabilidadInvernal.isEmpty()){
Marker aMarker;
for (ViabilidadInvernal vi : mVis.getViabilidades()) {
String estado = "";
BitmapDescriptor icon;
if(vi.getEstado() == PuertoEstado.ABIERTO){
estado = getResources().getString(R.string.mapa_puerto_abierto);
icon = BitmapDescriptorFactory.fromResource(R.drawable.marker_puertoabierto);
}else{
//vi.getEstado() == PuertoEstado.CERRADO
estado = getResources().getString(R.string.mapa_puerto_cerrado);
icon = BitmapDescriptorFactory.fromResource(R.drawable.marker_puertocerrado);
}
aMarker = mMapa.addMarker(new MarkerOptions()
.position(vi.getCoord())
.title(vi.getT())
.snippet(estado)
.icon(icon));
markersViabilidadInvernal.add(aMarker);
}
}
}
private void removeViabilidadInvernalMarkers() {
for(Marker aMarker : markersViabilidadInvernal){
aMarker.remove();
}
}
...
private class ViabilidadesInvernalesFetchAsyncTask extends AsyncTask<String, Void, ViabilidadesInvernales>{
private ProgressDialog mPd;
private HomeActivity ownerActivity;
private Context context;
private Exception exceptionToBeThrown;
public ViabilidadesInvernalesFetchAsyncTask(Activity activity){
this.ownerActivity = (HomeActivity) activity;
context = activity;
this.exceptionToBeThrown = null;
mPd = new ProgressDialog(context);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
mPd.setTitle("cargando");
mPd.setMessage("miralo");
//mPd.setTitle(getResources().getString(R.string.mapa_cargando_titulo));
//mPd.setMessage(getResources().getString(R.string.mapa_cargando, getResources().getString(R.string.mapa_cargando_elemento_viabilidad)));
mPd.setCancelable(false);
mPd.setIndeterminate(true);
mPd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mPd.show();
}
#Override
protected ViabilidadesInvernales doInBackground(String... params) {
String idioma = params[0];
if(!idioma.equalsIgnoreCase("es") && !idioma.equalsIgnoreCase("eu")){
idioma = "es";
}
SystemClock.sleep(5000);
ViabilidadesInvernalesParser vip = new ViabilidadesInvernalesParser();
ViabilidadesInvernales lasViabilidades = vip.parse(idioma);
Log.d(LOG_TAG, lasViabilidades.toString());
ViabilidadesInvernales vis = ViabilidadesInvernales.getInstance();
return vis;
}
#Override
protected void onPostExecute(ViabilidadesInvernales vis) {
super.onPostExecute(vis);
mVis = vis;
if(mPd != null){
mPd.dismiss();
}
// Check if exception exists.
//if (exceptionToBeThrown != null) {
//TODO
//ownerActivity.handleXXX();
//throw exceptionToBeThrown;
//}
}
}
....
}
The aim is to show a Dialog (ProgressDialog) when an AsyncTask is executed, so that the Dialog remains in front of the UI for a moment and then, the map (which was being shown previously) returns to the front. More exactly, you launch the application (home activity), you open the left drawer, press Mapa and go to the current fragment. An empty map is shown. You open the right drawer and press a button. Just afterwards, the process of launching AsyncTask is executed (and my desired ProgressDialog).
I don't know why, but the ProgressDialog is not being shown, but I have realised that the "sleep" process is ok (and the AsyncTask is executed properly every time).
Can anybody lend me a hand?
PS: My main activity (HomeActivity) launches fragments. The main activity has two drawers: left drawer is always visible, and right drawer is only visible when launching the FragmentMapa (the fragment I'm showing you right now). Might this issue be due to the Drawer behaviour?
THANK YOU SO MUCH
SOLVED. Anyone who faces this problem, check the following post: https://stackoverflow.com/a/3291713/828551.
You must create an interface, create an instance of it within the AsyncTask and implement the interface the main class where you launch the AsyncTask.

How to save a List from an Adapter to a xml File in Android?

I have developed an app capable of detecting BLE signals and others parameters. I use a BaseAdapter to develop the ListView for showing each item. The problem is that I want to save those data in a xml file when the scan has finished (after a period of time I have established) but I don´t know how to do it.
In this class I do the scan of BLE and is where I want to initiate the process of saving the List when it has passed the time of scanning:
public class ScanBleActivity extends ScanBaseActivity {
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler = new Handler();
//private List<BluetoothDevice> mydata;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 20000;
/* (non-Javadoc)
* #see com.zishao.bletest.ScanBaseActivity#initScanBluetooth()
*/
protected void initScanBluetooth() {
BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = manager.getAdapter();
startScanLen(true);
}
#Override
protected void onDestroy() {
super.onDestroy();
if (mScanning) {
startScanLen(false);
}
}
/**
*
* #param enable
*/
private void startScanLen(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
try {
savedata(true);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
And here is my Adapter:
public class LeDeviceListAdapter extends BaseAdapter {
public List<BluetoothDevice> data;
private Activity context;
private final HashMap<BluetoothDevice, Integer> rssiMap = new HashMap<BluetoothDevice, Integer>();
public LeDeviceListAdapter(Activity context, List<BluetoothDevice> data) {
this.data = data;
this.context = context;
}
//public static List<BluetoothDevice> getAllData() {
// return data;
//}
public synchronized void addDevice(BluetoothDevice device, int rssi) {
if(!data.contains(device) ){
data.add(device);
}
rssiMap.put(device, rssi);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
LayoutInflater mInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.leaf_devices_list_item, null);
convertView.setTag(new DeviceView(convertView));
}
DeviceView view = (DeviceView) convertView.getTag();
view.init((BluetoothDevice) getItem(position));
return convertView;
}
public class DeviceView {
private TextView title;
private TextView status;
private TextView type;
private TextView address;
private TextView rssivalue;
public DeviceView(View view) {
title = (TextView) view.findViewById(R.id.device_name);
status = (TextView) view.findViewById(R.id.device_status_txt);
type = (TextView) view.findViewById(R.id.device_type_txt);
address = (TextView) view.findViewById(R.id.device_address_txt);
rssivalue = (TextView) view.findViewById(id.signal_intensity_txt);
}
public void init(BluetoothDevice device) {
title.setText(device.getName());
address.setText(device.getAddress());
setType(device.getType());
setStatus(device.getBondState());
rssivalue.setText(""+rssiMap.get(device)+" dBm");
}
public void setType(int status) {
switch(status) {
case BluetoothDevice.DEVICE_TYPE_CLASSIC:
type.setText("Bluetooth Signal");
break;
case BluetoothDevice.DEVICE_TYPE_LE:
type.setText("BLE Signal");
break;
case BluetoothDevice.DEVICE_TYPE_DUAL:
type.setText("Dual Mode - BR/EDR/LE");
break;
case BluetoothDevice.DEVICE_TYPE_UNKNOWN:
type.setText("Device Unknown");
break;
}
}
public void setStatus(int s) {
switch(s) {
case BluetoothDevice.BOND_NONE:
status.setText("Not Bonded");
break;
case BluetoothDevice.BOND_BONDED:
status.setText("Bonded");
break;
case BluetoothDevice.BOND_BONDING:
status.setText("Bonding");
break;
}
}
}
I want to save the title, address, type, status and rssivalue (shown in the code above) of each BLE signal that has been found during the scan to be saved in a xml file. I have provided only a part of the project but if it is necessarly I will edit and put the code that is missing.
Does anyone know how to do it?? Please help!!!!!
New code: This corresponds to the class ScanBaseActivity:
abstract public class ScanBaseActivity extends ListActivity {
protected LeDeviceListAdapter mLeDeviceListAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_devices_scan);
mLeDeviceListAdapter = new LeDeviceListAdapter(this, new ArrayList<BluetoothDevice>());
this.setListAdapter(mLeDeviceListAdapter);
initScanBluetooth();
}
/**
* Start Scan Bluetooth
*
*/
abstract protected void initScanBluetooth();
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
BluetoothDevice device = (BluetoothDevice) mLeDeviceListAdapter.getItem(position);
ParcelUuid[] uuids = device.getUuids();
String uuidString = "Getting UUID's from " + device.getName() + ";UUID:";
if (null != uuids && uuids.length > 0) {
uuidString += uuids[0].getUuid().toString();
} else {
uuidString += "empty";
}
Toast.makeText(this, uuidString, Toast.LENGTH_LONG).show();
}
/**
* #param device
*/
protected synchronized void addDevice(final BluetoothDevice device, final int rssi) {
runOnUiThread(new Runnable() {
#Override
public void run() {
mLeDeviceListAdapter.addDevice(device, rssi);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
protected void savedata(boolean enable) throws FileNotFoundException{
String filename = "file.txt";
FileOutputStream fos;
Bundle extras = getIntent().getExtras();
long timestamp = extras.getLong("currentTime");
try {
fos= openFileOutput(filename, Context.MODE_PRIVATE);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.write((int) timestamp);
out.writeObject(mLeDeviceListAdapter);
out.write(null);
out.close();
Toast.makeText(this, R.string.list_saved, Toast.LENGTH_SHORT).show();
savedata(false);
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
}
New!!: I have edited ScanBaseActivity and ScanBleActivity to introduce the xml saving but when I run the app, when the scan stops it causes an error (moment when the list has to be saved in the sml file). Does anyone know how to solve it or correct it??!!!
OK, first, you need to re-shape how you are dealing with your adapter, after that, everything should fall into place.
so for that, I'm going to outsource to vogella, the cardinal of good android design patterns
http://www.vogella.com/tutorials/AndroidListView/article.html
you could read up through section 3, eat some copypasta and come back here, but every extra line you grok is a good thing =]
Now you have an activity that has a list of data, and an adapter that takes that list and--somewhat dumbly in comparison to your code--applies it to a view of some sort. When you want to update that data, you do so by modifying the List object in the activity by your method somewhere that gets a list of Bluetoothdevices--and I'd take that process off thread with an AsyncTask.
Since you are passing a List to the adapter, you can wait until you've populated the data in your activity, then do an adapter.notifyDataSetChanged. You do not want to adapter.add
then you have a nice List of your data in the activity; you don't need to worry if it's the right list, because--given the update pattern--it's the only list!
then follow the link Merleverde posted to serialize that data into an xml, maybe in a utility class, but in your activity is fine.
edit:
here is a perfectly good adapter, this is part of the larger pattern that displays dynamically changing data:
public class AnchorListAdapter extends ArrayAdapter<String>
{
private final List<String> anchorNames;
public AnchorListAdapter(Context context, int textviewId, List<String> anchors){
super(context, textviewId, anchors);
anchorNames = anchors;
}
#Override
public String getItem( int i ) {
return anchorNames.get(i).toString();
}
}
Well, it's not that you are going to be saving it from the adapter, it's that the adater is going to be 'adapting' a data set that you will put into preferences.
as a feild:
private SharedPreferences saveHash;
in onCreate:
saveHash = getSharedPreferences( getString( R.string.save_hash ), MODE_PRIVATE );
then later:
public void onFinishedLoading(){
super.onPause();
SharedPreferences.Editor editor = saveHash.edit();
editor.clear();
for( String s: myData){
editor.putString(x, s);
}
editor.commit();
}
edit: realized you want to make a hash from a list; what do you want as the key?

Call method of interface implements with child fragment From container activity Android

I'm stuck with communication between activity and fragment using interface. I have created activity with child fragment. I wanna do some stuff with continuous thread defined in activity and during that thread when I'm getting some result at that time I wanna trigger to child fragment to do something.
My Container Activity
public class MySpaceActivity extends BaseDrawerActivity {
private OnSetLastSeenListener mListner;
public static Thread mThread = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setHeaders(Const.MY_SPACE);
super.setSubmenus(Const.MY_SPACE,
Utils.getSubmenuList(Const.MY_SPACE, MySpaceActivity.this),
submenuBean);
// super.attachFragment(submenuBean);
}
#Override
public void setHeaderSubMenu(SubmenuBean subMenuBean) {
// txt_submenu.setText(subMenuBean.getSubmenu_name());
this.submenuBean = subMenuBean;
Log.print("::::: setHeaderSubMenu ::::");
super.attachFragment(submenuBean);
}
public void setsubFragment(SubmenuBean subMenuBean) {
this.submenuBean = subMenuBean;
super.attachSubFragment(submenuBean);
}
#Override
public void onBackPressed() {
super.onBackPressed();
popLastFragment();
}
private void popLastFragment() {
if (super.getNumberOfChilds() > 1) {
super.popSubFragment();
} else {
finish();
}
}
#Override
protected Fragment getFragement() {
StudentsFragment fragment = new StudentsFragment(Const.MY_SPACE,
getSubmenubean());
return fragment;
}
public SubmenuBean getSubmenubean() {
return submenuBean;
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mThread = new Thread(new CountDownTimer(MySpaceActivity.this));
mThread.start();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (mThread.isAlive()) {
mThread.interrupt();
mThread = null;
}
}
public void updateLastSeen(){
Log.print("::::::Call Interface::::::");
mListner.updateLastSeen();
}
class CountDownTimer implements Runnable {
private Context mContext;
private JSONObject mJsonObject;
private JSONArray mJsonArray;
public CountDownTimer(Context mContext) {
this.mContext = mContext;
}
// #Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
HttpChatLastSeen mChat = new HttpChatLastSeen();
mJsonObject = mChat.Http_ChatLastSeen(mContext);
String mResult = mJsonObject.getString("Result");
if (mResult.equalsIgnoreCase(String
.valueOf(Const.RESULT_OK))) {
mJsonArray = mJsonObject.getJSONArray("UserData");
for (int i = 0; i < mJsonArray.length(); i++) {
mJsonObject = mJsonArray.getJSONObject(i);
new DbStudentMasterBll(mContext).update(
"last_seen", mJsonObject
.getString("LastSeen"), Integer
.parseInt(mJsonObject
.getString("UserId")));
}
} else {
Log.print("MY LAST SEEN Response : "
+ mJsonObject.toString());
}
updateLastSeen();
Thread.sleep(15000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
Log.print("ChatLastSeenThread : ", e.getMessage());
}
}
}
}
}
My Child Fragment With Interface :
public class StudentsFragment extends Fragment implements OnSetLastSeenListener{
TextView txt_submenu;
ListView list_students;
SubmenuBean submenuBean;
int Mainmenu;
MySpaceActivity mMySpaceActivity;
ArrayList<DbStudentMasterBean> studentsList;
StudentsAdapter mAdapter = null;
OnSetLastSeenListener mListner;
public StudentsFragment() {
super();
}
public StudentsFragment(int Mainmenu, SubmenuBean submenuBean) {
this.submenuBean = submenuBean;
this.Mainmenu = Mainmenu;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_students, container,
false);
mMySpaceActivity = (MySpaceActivity) getActivity();
txt_submenu = (TextView) view.findViewById(R.id.txt_submenu);
txt_submenu.setText(submenuBean.getSubmenu_name());
txt_submenu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mMySpaceActivity.openDrawer();
}
});
list_students = (ListView) view.findViewById(R.id.list_colleagues);
studentsList = new DbStudentMasterBll(getActivity()).getAllRecords();
mAdapter = new StudentsAdapter(getActivity(), studentsList, handler);
list_students.setAdapter(mAdapter);
list_students.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
DbStudentMasterBean bean = (DbStudentMasterBean) parent
.getAdapter().getItem(position);
Message msg = new Message();
msg.what = CHAT;
msg.obj = bean;
handler.sendMessage(msg);
}
});
return view;
}
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case CHAT:
submenuBean.setTag(VIEWCHATSTUDENT);
DbStudentMasterBean bean = (DbStudentMasterBean) msg.obj;
mMySpaceActivity.setsubFragment(submenuBean);
break;
}
};
};
#Override
public void updateLastSeen() {
// TODO Auto-generated method stub
Log.print("!!!!!!!!!Refresh Adapter!!!!!!!!!!!");
mAdapter.notifyDataSetChanged();
}
}
My Interface :
public interface OnSetLastSeenListener {
public void updateLastSeen();
}
So I have implemented interface OnSetLastSeenListener with my child fragment StudentsFragment . Now I'm calling method of tht interface updateLastSeen() from my container activity with thread. But it is not getting trigger to child fragment where I have implemented interface. So I don't know whether it is good way to communicate or not? Let me take your help to suggest on this solution or best way to communicate from child fragment to parent activity.
Thanks,
It is better to use interface when you want to communicate something from Fragment to Activity and not vice versa.
In your case, you can directly call the method in Fragment from Activity through fragment object. No need to use interface.
Something like this (For static fragments)
StudentsFragment fragment = (StudentsFragment) getFragmentManager()
.findFragmentById(R.id.fragmentid);
if (fragment != null && fragment.isInLayout()) {
fragment.updateLastSeen();
}
For dynamic fragment you can use the fragment object directly.

AsynkTask within Fragment within a ViewPager

First of all, I am relatively new to android programming.
I am creating a ViewPager application with two Fragments. One of the Fragments requests data from a server and return a result to the main FragmentActivity. My problem is that this request to the server can take sometime, and I have been trying to get a ProgressDialog to appear with AsyncTask while the user waits for the data to be retrieved. Once I create the background thread to retrieve the data, I successfully execute some code in the onPostExecute() method and set some variables. However, the return statement that sends information back to the FragmentActivity is being executed before the background thread actually ends. I can't seem to figure out a way for the main thread to wait on the background thread. Using Asyctask's get() method results in the ProgressDialog from appearing. I have looked through a lot of posts in here, but can't seem to find an answer.
Anything helps.
Code below:
SplashScreen.java
public class SplashScreen extends FragmentActivity {
MainMenu mainMenu;
MapScreen mapScreen;
PagerAdapter pagerAdapter;
ViewPager viewPager;
List<LatLng> geoPoints;
private Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash_screen);
context = this;
initializePaging();
}
private void initializePaging()
{
mainMenu = new MainMenu();
mapScreen = new MapScreen();
pagerAdapter = new PagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(mainMenu);
pagerAdapter.addFragment(mapScreen);
viewPager = (ViewPager) super.findViewById(R.id.viewPager);
viewPager.setAdapter(pagerAdapter);
viewPager.setOffscreenPageLimit(2);
viewPager.setCurrentItem(0);
viewPager.setOnPageChangeListener(new OnPageChangeListener()
{
#Override
public void onPageScrollStateChanged(int postion){}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2){}
#Override
public void onPageSelected(int position)
{
switch(position){
case 0: findViewById(R.id.first_tab).setVisibility(View.VISIBLE);
findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
break;
case 1: findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
findViewById(R.id.second_tab).setVisibility(View.VISIBLE);
break;
}
}
});
}
//Called from onClick in main_mainu.xml
public void getDirections(View view)
{
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
try
{
geoPoints = mainMenu.getDirections(context);
mapScreen.plotPoints(geoPoints);
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Error! Invalid address entered.", Toast.LENGTH_LONG).show();
mainMenu.clear();
}
}
}
MainMenu.java
public class MainMenu extends Fragment {
String testString;
int testInt;
TextView testTV;
private TextView tvDisplay;
private EditText departure;
private EditText destination;
private Geocoder geocoder;
private List<Address> departAddress;
private List<Address> destinationAddress;
private List<LatLng> geoPoints;
private String departString;
private String destinationString;
private Address departLocation;
private Address destinationLocation;
private LatLng departurePoint;
private LatLng destinationPoint;
private Context contextMain;
private GetData task;
public MainMenu()
{
super();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View root = (View) inflater.inflate(R.layout.main_menu, null);
geoPoints = new ArrayList<LatLng>(2);
return root;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
departure = (EditText) getView().findViewById(R.id.depart_field);
destination = (EditText) getView().findViewById(R.id.destination_field);
tvDisplay = (TextView) getView().findViewById(R.id.textView1);
}
public List<LatLng> getDirections(Context context)
{
contextMain = context;
geocoder = new Geocoder(getActivity());
departString = departure.getText().toString();
destinationString = destination.getText().toString();
try
{
task = new GetData(new Callback(){
public void run(Object result)
{
//return geoPoints;
}
});
task.execute((Void[])null);
}catch(Exception e)
{
e.printStackTrace();
}
return geoPoints;
}
public void clear()
{
departure.setText("");
destination.setText("");
tvDisplay.setText("Enter departure point, and destination point");
}
private class GetData extends AsyncTask<Void, Void, List<List<Address>>>
{
Callback callback;
private ProgressDialog processing;
public GetData(Callback callback)
{
this.callback = callback;
}
#Override
protected void onPreExecute()
{
processing = new ProgressDialog(contextMain);
processing.setTitle("Processing...");
processing.setMessage("Please wait.");
processing.setCancelable(false);
processing.setIndeterminate(true);
processing.show();
}
#Override
protected List<List<Address>> doInBackground(Void...arg0)
{
List<List<Address>> list = new ArrayList<List<Address>>(2);
try
{
departAddress = geocoder.getFromLocationName(departString, 5, 37.357059, -123.035889, 38.414862, -121.723022);
destinationAddress = geocoder.getFromLocationName(destinationString, 5, 37.357059, -123.035889, 38.414862, -121.723022);
list.add(departAddress);
list.add(destinationAddress);
}catch(IOException e)
{
e.printStackTrace();
}
return list;
}
#Override
protected void onPostExecute(List<List<Address>> list)
{
departLocation = list.get(0).get(0);
destinationLocation = list.get(1).get(0);
departurePoint = new LatLng(departLocation.getLatitude(), departLocation.getLongitude());
destinationPoint = new LatLng(destinationLocation.getLatitude(), destinationLocation.getLongitude());
if(geoPoints.size() >= 2)
{
geoPoints.clear();
}
geoPoints.add(departurePoint);
geoPoints.add(destinationPoint);
callback.run(list);
processing.dismiss();
}
}
}
#Override
protected Object doInBackground(Void...arg0)
{
Object result = null;
try
{
departAddress = geocoder.getFromLocationName(departString, 5, 37.357059, -123.035889, 38.414862, -121.723022);
destinationAddress = geocoder.getFromLocationName(destinationString, 5, 37.357059, -123.035889, 38.414862, -121.723022);
}catch(IOException e)
{
e.printStackTrace();
}
return result;
}
You never set the value of result...

Categories

Resources