Android Loader – Elenco vuoto

Nessun errore o crash in esecuzione, solo una output vuota per ListView. Re-leggere il codice più volte non ha idea della causa per cui il ListView non popolerà.

MSKFragment.java

  • Esempio di codice per implementare un lettore PDF
  • mettere le tabs nella parte inferiore dello schermo
  • Come hide la barra d'azione prima che l'attività sia creata e quindi visualizzarla di nuovo?
  • Aggiunta di "divisori di sezione" al layout?
  • Come utilizzare la visualizzazione del calendario personalizzato in Android e come impostare l'avviso per il promemory?
  • Problemi con joystick sullo schermo di Android
  • public class MSKFragment extends android.support.v4.app.Fragment implements android.support.v4.app.LoaderManager.LoaderCallbacks<Cursor> { private ServiceAdapter mServiceAdapter; public static final int SERVICE_LOADER = 0; private static final String[] SERVICE_COLUMNS = { ServiceContract.ServiceEntry.TABLE_NAME, ServiceContract.ServiceEntry._ID, ServiceContract.ServiceEntry.COLUMN_NAME, ServiceContract.ServiceEntry.COLUMN_ORGANIZATION_NAME, ServiceContract.ServiceEntry.COLUMN_PRICE }; /*public static final int COL_TABLE_NAME = 0;*/ public static final int COL_ID = 0; public static final int COL_NAME = 1; public static final int COL_ORG_NAME = 2; public static final int COL_PRICE = 3; public MSKFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); getLoaderManager().initLoader(SERVICE_LOADER, null, this); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_fragment_msk, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_refresh) { //Moving functions to a helper class, so that when Activity starts the data can be displayed updateServices(); return true; } return super.onOptionsItemSelected(item); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mServiceAdapter = new ServiceAdapter(getActivity(), null, 0); View rootView = inflater.inflate(R.layout.fragment_msk, container, false); ListView listView = (ListView) rootView.findViewById(R.id.listViewMainService); listView.setAdapter(mServiceAdapter); return rootView; } @Override public void onStart() { super.onStart(); //Refresh called and service updated when activity starts updateServices(); } //Calling FetchServiceTask and executing it. private void updateServices() { FetchServicesTask serviceTask = new FetchServicesTask(getActivity()); serviceTask.execute(); } @Override public android.support.v4.content.Loader<Cursor> onCreateLoader(int i, Bundle args) { Uri serviceUri = ServiceContract.ServiceEntry.buildServiceUri(i); return new android.support.v4.content.CursorLoader(getActivity(), serviceUri, null, null, null, null); } @Override public void onLoadFinished(android.support.v4.content.Loader<Cursor> loader, Cursor data) { mServiceAdapter.swapCursor(data); } @Override public void onLoaderReset(android.support.v4.content.Loader<Cursor> loader) { mServiceAdapter.swapCursor(null); } } 

    FetchServicesTask.java (operazioni di Async)

     public class FetchServicesTask extends AsyncTask<String, Void, Void> { private final String LOG_TAG = FetchServicesTask.class.getSimpleName(); private final Context mContext; private boolean DEBUG = true; public FetchServicesTask(Context context ) { mContext = context; } private String[] getServicesDatafromJson(String serviceJsonStr) throws JSONException { final String MSK_NAME = "name"; final String MSK_PRICE = "price"; final String MSK_ORG_NAME = "organizationName"; final String MSK_POSTED_USER = "postedUser"; final String MSK_PROFILE = "profile"; int totalResults = 25; String resultStrs[] = new String[totalResults]; try { JSONArray serviceArray = new JSONArray(serviceJsonStr); Vector<ContentValues> cVVector = new Vector<ContentValues>(serviceArray.length()); for(int i=0;i<serviceArray.length();i++) { String name, organizationName; int price; JSONObject serviceObject = serviceArray.getJSONObject(i); name = serviceObject.getString(MSK_NAME); price = serviceObject.getInt(MSK_PRICE); JSONObject postedUserObject = serviceObject.getJSONObject(MSK_POSTED_USER); JSONObject profileObject = postedUserObject.getJSONObject(MSK_PROFILE); organizationName = profileObject.getString(MSK_ORG_NAME); ContentValues serviceValues = new ContentValues(); serviceValues.put(ServiceContract.ServiceEntry.COLUMN_NAME, name); serviceValues.put(ServiceContract.ServiceEntry.COLUMN_ORGANIZATION_NAME, organizationName); serviceValues.put(ServiceContract.ServiceEntry.COLUMN_PRICE, price); cVVector.add(serviceValues); } int inserted = 0; if(cVVector.size()>0) { ContentValues[] cvArray = new ContentValues[cVVector.size()]; cVVector.toArray(cvArray); inserted = mContext.getContentResolver().bulkInsert(ServiceContract.ServiceEntry.CONTENT_URI, cvArray); } Log.d(LOG_TAG, "FetchServicesTask Complete. " + inserted + " Inserted"); } catch(JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); e.printStackTrace(); } return null; } /////////////////////////////// NETWORKING BOILER PLATE /////////////////////////// @Override protected Void doInBackground(String... params) { if (params.length == 0) { return null; } HttpURLConnection urlConnection = null; //Streaming data using HTTP String serviceJsonStr = null; //raw JSON response BufferedReader reader = null; //read text from character i/p stream int pageNo = 1; int numResults = 5; try { //-------------------------CONNECTION--------------------// final String SERVICE_BASE_URL = "http://myservicekart.com/public/"; final String SEARCH_BASE_URL = "http://myservicekart.com/public/search?"; final String SEARCH_PARAM = "search"; final String PAGE_PARAM = "pageno"; /*Using a helper class UriBuilder for building and manipulating URI references*/ Uri builtServiceUri = Uri.parse(SERVICE_BASE_URL); Uri builtSearchUri = Uri.parse(SEARCH_BASE_URL).buildUpon(). appendQueryParameter(SEARCH_PARAM, ""). appendQueryParameter(PAGE_PARAM, Integer.toString(pageNo)). build(); URL searchUrl = new URL(builtSearchUri.toString()); Log.v(LOG_TAG, "Built SearchUri" +builtSearchUri.toString()); urlConnection = (HttpURLConnection) searchUrl.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); //------------------------BUFFERING---------------------// InputStream inputStream = urlConnection.getInputStream(); StringBuffer buffer = new StringBuffer(); if(inputStream==null) { return null; } reader = new BufferedReader(new InputStreamReader(inputStream)); String line = null; while((line = reader.readLine()) != null) { buffer.append(line + "\n"); } if(buffer.length()==0) { return null; } serviceJsonStr = buffer.toString(); getServicesDatafromJson(serviceJsonStr); Log.v(LOG_TAG, "Services JSON String: " +serviceJsonStr); } catch(IOException e) { Log.e(LOG_TAG, "Error cannot connect to URL", e); return null; } catch (JSONException e) { e.printStackTrace(); } finally { if(urlConnection!=null) { urlConnection.disconnect(); } if(reader!=null) { try { reader.close(); } catch (final IOException e) { Log.e(LOG_TAG,"Error closing stream",e); } } } return null; } } 

    ServiceAdapter.java

     public class ServiceAdapter extends CursorAdapter { public ServiceAdapter(Context context, Cursor c, int flags) { super(context, c, flags); } private String convertCursorRowToUXFormat(Cursor cursor) { return cursor.getString(MSKFragment.COL_NAME) + "-" + cursor.getString(MSKFragment.COL_ORG_NAME) + " - " + cursor.getInt(MSKFragment.COL_PRICE); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { View view = LayoutInflater.from(context).inflate(R.layout.list_item_main, parent, false); return view; } @Override public void bindView(View view, Context context, Cursor cursor) { TextView tv = (TextView)view; tv.setText(convertCursorRowToUXFormat(cursor)); } } 

  • Imansible rilevare quando risposta in risposta in risposta a Android
  • Estrarre il text dalle pagine web solo dà l'URL
  • java.lang.illegalargumentexception carattere illegale in url
  • Come get il nome del contatto, il numero e l'ID emai da un elenco di contatti?
  • Nessuna intenzione da UserRecoverableAuthIOException che utilizza Drive SDK per Android
  • Elencare i dispositivi connessi al telefono Android che funziona come punto di accesso WiFi
  • L'Android è un fan Android di Google, tutto su telefoni Android, Android Wear, Android Dev e applicazioni Android Games e così via.