Back to project page android.app.niuz.io.
The source code is released under:
GNU General Public License
If you think the Android project android.app.niuz.io listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package io.niuz; //from w w w . j av a 2 s . co m import io.niuz.model.APIRetrievalStatus; import io.niuz.model.News; import io.niuz.services.NewsService; import io.niuz.services.PhoneService; import java.text.ParseException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import android.app.Activity; import android.app.ListActivity; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; public class NewsHeadersActivity extends ListActivity { private NewsAdapter newsAdapter; private News[] objects; private Context context; private NewsHeadersRetrievalTask newsHeadersRetrievalTask; private void toast(String message) { PhoneService.toast(message, this); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_news_headers); this.context = this; } @Override protected void onPause() { super.onPause(); if (newsHeadersRetrievalTask != null) { newsHeadersRetrievalTask.cancel(true); } } @Override protected void onResume() { super.onResume(); try { if (mustRenewObjects()) { toast("Retrieving news headers"); if (newsHeadersRetrievalTask == null || newsHeadersRetrievalTask.getStatus() != AsyncTask.Status.RUNNING) { newsHeadersRetrievalTask = new NewsHeadersRetrievalTask(); newsHeadersRetrievalTask.execute(); } } } catch (ParseException e) { toast("Error occurred while parsing dates"); } } private boolean mustRenewObjects() throws ParseException { if (objects == null || objects.length == 0) { return true; } else { return NewsService.isNewsTooOld(objects[0]); } } private void sortAndShuffleNewsList(List<News> newsList) { if (newsList == null || newsList.size() <= 1) { return; } // Sorting descending Collections.sort(newsList, new Comparator<News>(){ @Override public int compare(News arg0, News arg1) { return (arg0.getSubcategoryWeight() == arg1.getSubcategoryWeight() ? 0 : arg0.getSubcategoryWeight() > arg1.getSubcategoryWeight() ? -1 : 1); }} ); // Shuffling those with the same weight List<News> finalListToReturn = new ArrayList<News>(newsList.size()); List<News> listToShuffle = new ArrayList<News>(); Long lastItemWeight = null; // The algorithm below functions only if list is sorted in descending order for (int i=0 ; i<newsList.size() ; i++) { if (lastItemWeight == null || newsList.get(i).getSubcategoryWeight() == lastItemWeight) { lastItemWeight = newsList.get(i).getSubcategoryWeight(); listToShuffle.add(newsList.get(i)); if (newsList.size() - 1 == i) { finalListToReturn.addAll(listToShuffle); } } else { Collections.shuffle(listToShuffle); finalListToReturn.addAll(listToShuffle); listToShuffle.clear(); lastItemWeight = null; } } // Reinitializing the parameter list newsList.clear(); newsList.addAll(finalListToReturn); } private void afterNewsRetrieval() { List<News> retrievedNewsFromTheServer = NewsService.getNewsList(); if (retrievedNewsFromTheServer.size() == 0) { toast("Failed retrieving news list"); } else { // Sorting for reading sortAndShuffleNewsList(retrievedNewsFromTheServer); // Initializing an array objects = new News[retrievedNewsFromTheServer.size()]; for (int i=0 ; i<objects.length ; i++) { objects[i] = retrievedNewsFromTheServer.get(i); } // Initializing an adapter if (newsAdapter == null) { newsAdapter = new NewsAdapter(this, R.layout.view_news_header, objects); this.getListView().setAdapter(newsAdapter); } newsAdapter.notifyDataSetChanged(); toast("News retrieval is complete"); } } private class NewsHeadersRetrievalTask extends AsyncTask<Object, Object, APIRetrievalStatus> { @Override protected APIRetrievalStatus doInBackground(Object... arg0) { try { NewsService.initNewsList(context); return APIRetrievalStatus.SUCCESS; } catch (Exception e) { return APIRetrievalStatus.ERROR; } } @Override protected void onPostExecute(APIRetrievalStatus result) { switch (result) { case SUCCESS: afterNewsRetrieval(); break; case ERROR: toast("Failed to retrieve news!"); } } } private static class NewsHeaderRowHolder { TextView newsTitle; TextView newsSubcategoryName; TextView newsCompanyName; } private class NewsAdapter extends ArrayAdapter<News> { Context context; int resource; News[] objects; public NewsAdapter(Context context, int resource, News[] objects) { super(context, resource, objects); this.context = context; this.resource = resource; this.objects = objects; } @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = convertView; NewsHeaderRowHolder holder; // Init data object News dataObject = objects[position]; // Creating row if it's not created if (rowView == null) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); rowView = inflater.inflate(resource, null); holder = new NewsHeaderRowHolder(); holder.newsTitle = (TextView)rowView.findViewById(R.id.news_header); holder.newsSubcategoryName = (TextView)rowView.findViewById(R.id.news_subcategory_name); holder.newsCompanyName = (TextView)rowView.findViewById(R.id.news_company); rowView.setTag(holder); } else { holder = (NewsHeaderRowHolder)rowView.getTag(); } // Populating data holder.newsTitle.setText(dataObject.getTitle()); holder.newsSubcategoryName.setText(dataObject.getSubcategoryName()); holder.newsCompanyName.setText(dataObject.getNewsCompanyName()); rowView.setOnClickListener(new OnNewsHeaderClickListener(dataObject)); return rowView; } private class OnNewsHeaderClickListener implements View.OnClickListener { private News dataObject; OnNewsHeaderClickListener(News dataObject) { this.dataObject = dataObject; } @Override public void onClick(View view) { NewsReaderActivity.setNewsToRead(dataObject); startActivity(new Intent(context, NewsReaderActivity.class)); } } } @Override public void onBackPressed() { startActivity(new Intent(context, PreMainActivity.class)); } }