Android Open Source - android.app.niuz.io News Companies Selection Activity






From Project

Back to project page android.app.niuz.io.

License

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.

Java Source Code

package io.niuz;
//from ww w.  j  av  a 2  s  . c o  m
import io.niuz.model.APIRetrievalStatus;
import io.niuz.model.CompanyInformation;
import io.niuz.services.CompaniesService;
import io.niuz.services.NewsService;
import io.niuz.services.PhoneService;

import java.util.Map;

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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class NewsCompaniesSelectionActivity extends ListActivity {
  private CompanyInformation[] objects;
  private CompanyListAdapter companyListAdapter;
  
  private Button submitCompanies;
  
  private Context context;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news_companies_selection);
    this.context = this;
    
    this.submitCompanies = (Button)findViewById(R.id.submitCompanies);
  }  
  
  @Override
  protected void onResume() {
    super.onResume();
    
    initObjects();
    companyListAdapter = new CompanyListAdapter(this, R.layout.view_companies_selection, objects);
    this.getListView().setAdapter(companyListAdapter);
    
    final ListActivity that = this;
    this.submitCompanies.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        that.getListView().setClickable(false);
        v.setClickable(false);
        toast("Adapting news companies");
        sendChoicesToServerSide();
      }
    });
  }



  private void continueToNewsReading() {
    startActivity(new Intent(this, NewsHeadersActivity.class));
  }
  
  private void sendChoicesToServerSide() {
    new InitRelevantCompaniesTask().execute(objects);
  }
  
  private class InitRelevantCompaniesTask extends AsyncTask<CompanyInformation, Integer, APIRetrievalStatus> {
    @Override
    protected APIRetrievalStatus doInBackground(CompanyInformation... params) {
      try {
        CompaniesService.initRelevantCompaniesSelections(params, context);
        return APIRetrievalStatus.SUCCESS;

      } catch (Exception e) {
        Log.w("Fatal", e);
        return APIRetrievalStatus.ERROR;
      }
    }
    
    @Override
    protected void onPostExecute(APIRetrievalStatus result) {
      switch (result) {
      case SUCCESS:
        toast("Initializing primary user news...");
        new InitPrimaryUserNews().execute();
        break;

      case ERROR:
        toast("Failed to initialize favourite companies!");
      }
    }
  }
  
  private class InitPrimaryUserNews extends AsyncTask<Object, Object, APIRetrievalStatus> {

    @Override
    protected APIRetrievalStatus doInBackground(Object... arg0) {
      try {
        NewsService.initUserNewsToRead();
        return APIRetrievalStatus.SUCCESS;

      } catch (Exception e) {
        Log.w("Fatal", e);
        return APIRetrievalStatus.ERROR;
      }
    }
    
    @Override
    protected void onPostExecute(APIRetrievalStatus result) {
      switch (result) {
      case SUCCESS:
        continueToNewsReading();
        break;

      case ERROR:
        toast("Failed to initialize primary news!");
      }
    }
  }
  
  private void initObjects() {
    Map<Integer, String> relevantCompanies = CompaniesService.getRelevantCompanies();
    this.objects = new CompanyInformation[relevantCompanies.size()];
    
    int i=0;
    for (Map.Entry<Integer, String> company : relevantCompanies.entrySet()) {
      CompanyInformation info = new CompanyInformation();
      info.setId(company.getKey());
      info.setName(company.getValue());
      info.setMarkedAsFavourite(false);
      objects[i++] = info;
    }
  }
  
  private void toast(String message) {
    PhoneService.toast(message, this);
  }
  
  private static class CompanyRowHolder {
    CheckBox checkBox;
    TextView companyName;
  }
  
  private class CompanyListAdapter extends ArrayAdapter<CompanyInformation> {
    private Context context;
    private CompanyInformation[] objects;
    private int resource;    
    
    public CompanyListAdapter(Context context, int resource, CompanyInformation[] objects) {
      super(context, resource, objects);
      this.context = context;
      this.objects = objects;
      this.resource = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View rowView = convertView;
      CompanyRowHolder holder;
      
      // Init data object
      final CompanyInformation 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 CompanyRowHolder();
        holder.checkBox = (CheckBox)rowView.findViewById(R.id.checkBoxCompany);
        holder.companyName = (TextView)rowView.findViewById(R.id.companyName);
        rowView.setTag(holder);
      } else {
        holder = (CompanyRowHolder)rowView.getTag();
      }
            
      // Populating data
      final ArrayAdapter<CompanyInformation> aa = this;
      holder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          dataObject.setMarkedAsFavourite(((CheckBox)v).isChecked());
          aa.notifyDataSetChanged();
        }
      });
      holder.checkBox.setChecked(dataObject.isMarkedAsFavourite());
      holder.companyName.setText(dataObject.getName());
      return rowView;
    }    
  }
  
  @Override
  public void onBackPressed() {
    startActivity(new Intent(context, PreMainActivity.class));
  }
}




Java Source Code List

io.niuz.CategoriesSelectionActivity.java
io.niuz.CompanyInformationLoadingActivity.java
io.niuz.MainActivity.java
io.niuz.NewsCompaniesSelectionActivity.java
io.niuz.NewsHeadersActivity.java
io.niuz.NewsReaderActivity.java
io.niuz.PreMainActivity.java
io.niuz.model.APIRetrievalStatus.java
io.niuz.model.CategoryInformation.java
io.niuz.model.CompanyInformation.java
io.niuz.model.InitUserStatus.java
io.niuz.model.News.java
io.niuz.services.BaseService.java
io.niuz.services.CategoriesSubcategoriesService.java
io.niuz.services.CompaniesService.java
io.niuz.services.NewsService.java
io.niuz.services.PhoneService.java
io.niuz.services.UserService.java