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; //w w w. j a v a 2 s. com import io.niuz.model.APIRetrievalStatus; import io.niuz.model.CategoryInformation; import io.niuz.services.CategoriesSubcategoriesService; import io.niuz.services.PhoneService; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import android.app.Activity; import android.app.AlertDialog; import android.app.ListActivity; import android.content.Context; import android.content.DialogInterface; 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 CategoriesSelectionActivity extends ListActivity { private Button button; private CategoryInformation[] objects; private CategoryListAdapter categoryListAdapter; private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_init_categories); this.context = this; } @Override protected void onResume() { super.onResume(); initListAdapter(); final Context that = this; this.button = (Button)findViewById(R.id.submitCategories); this.button.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { if (!isAtLeastOneFilled()) { AlertDialog.Builder builder = new AlertDialog.Builder(that); builder.setMessage("No subcategories were selected!") .setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } } ); builder.create().show(); return; } else { proceedWithCategorySelections(); } } }); setListAdapter(categoryListAdapter); } private void proceedWithCategorySelections() { toast("Initializing category selections..."); this.getListView().setClickable(false); this.button.setClickable(false); new InitUserSubcategoriesTask().execute(objects); } private void toast(String message) { PhoneService.toast(message, this); } private boolean isAtLeastOneFilled() { for (CategoryInformation object : objects) { if (object.getSubcategoriesSelected().size() > 0) { return true; } } return false; } private void dataChanged() { categoryListAdapter.notifyDataSetChanged(); } private void initListAdapter() { Map<Integer, String> categories = CategoriesSubcategoriesService.getCategoriesMap(); int i = 0; objects = new CategoryInformation[categories.size()]; for (Map.Entry<Integer, String> entry : categories.entrySet()) { CategoryInformation object = new CategoryInformation(); object.setId(entry.getKey()); object.setName(entry.getValue()); object.setSubcategoriesSelected(new HashMap<Integer, String>()); objects[i++] = object; } categoryListAdapter = new CategoryListAdapter(this, R.layout.view_categories_selection, objects); } private void transferToCompanyInformationLoadingActivity() { startActivity(new Intent(this, CompanyInformationLoadingActivity.class)); } private class CategoryListAdapter extends ArrayAdapter<CategoryInformation> { private Context context; private CategoryInformation[] objects; private int resource; public CategoryListAdapter(Context context, int resource, CategoryInformation[] 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; CategoryRowHolder holder; // Init data object CategoryInformation 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 CategoryRowHolder(); holder.checkBox = (CheckBox)rowView.findViewById(R.id.checkBox); holder.categoryName = (TextView)rowView.findViewById(R.id.categoryName); holder.subcategoriesSelected = (TextView)rowView.findViewById(R.id.subcategoriesSelected); holder.subcategoriesNumber = (TextView)rowView.findViewById(R.id.subcategoriesNumber); rowView.setTag(holder); } else { holder = (CategoryRowHolder)rowView.getTag(); } rowView.setOnClickListener(new OnCategoryClickListener(dataObject, context)); // Populating data holder.checkBox.setChecked(dataObject.getSubcategoriesSelected().size() > 0); holder.checkBox.setClickable(false); holder.categoryName.setText(dataObject.getName()); holder.categoryName.setClickable(false); holder.subcategoriesSelected.setClickable(false); holder.subcategoriesNumber.setText(Integer.toString(dataObject.getSubcategoriesSelected().size())); holder.subcategoriesNumber.setClickable(false); return rowView; } private class OnCategoryClickListener implements View.OnClickListener { private CategoryInformation dataObject; private Context context; private OnCategoryClickListener(CategoryInformation dataObject, Context context) { this.dataObject = dataObject; this.context = context; } @Override public void onClick(View view) { // Subcategories final Map<Integer, String> subcategories = CategoriesSubcategoriesService.getSubcategoriesMap(dataObject.getId()); // Building items list final String[] itemsList = new String[subcategories.size()]; int i = 0; for (Map.Entry<Integer, String> subcategory : subcategories.entrySet()) { itemsList[i++] = subcategory.getValue(); } // Building selected list boolean[] selectionList = new boolean[subcategories.size()]; Arrays.fill(selectionList, false); for (Map.Entry<Integer, String> selectedSubcategory : dataObject.getSubcategoriesSelected().entrySet()) { for (int j=0 ; j<itemsList.length ; j++) { if (itemsList[j].equals(selectedSubcategory.getValue())) { selectionList[j] = true; } } } // Building dialog AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("What interests you in " + dataObject.getName() + "?"); builder.setMultiChoiceItems(itemsList, selectionList, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { String value = itemsList[which]; int key = getMapKeyByValue(subcategories, value); dataObject.getSubcategoriesSelected().put(key, value); } else { String value = itemsList[which]; int key = getMapKeyByValue(subcategories, value); dataObject.getSubcategoriesSelected().remove(key); } } }); builder.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); dataChanged(); } }); builder.create().show(); } } } private Integer getMapKeyByValue(Map<Integer, String> map, String value) { for (Map.Entry<Integer, String> entry : map.entrySet()) { if (entry.getValue().equals(value)) { return entry.getKey(); } } return null; } private static class CategoryRowHolder { CheckBox checkBox; TextView categoryName; TextView subcategoriesSelected; TextView subcategoriesNumber; } private class InitUserSubcategoriesTask extends AsyncTask<CategoryInformation, Integer, APIRetrievalStatus> { @Override protected APIRetrievalStatus doInBackground(CategoryInformation... params) { try { CategoriesSubcategoriesService.initUserSubcategories(params, context); return APIRetrievalStatus.SUCCESS; } catch (Exception e) { Log.w("Fatail", e); return APIRetrievalStatus.ERROR; } } @Override protected void onPostExecute(APIRetrievalStatus result) { switch (result) { case SUCCESS: transferToCompanyInformationLoadingActivity(); break; case ERROR: toast("Failed to initialize subcategories"); } } } @Override public void onBackPressed() { startActivity(new Intent(context, PreMainActivity.class)); } }