Advertisement
Guest User

Missing update for checkbox in CheckedTextView

a guest
Feb 3rd, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.44 KB | None | 0 0
  1. package org.raboss.gamification.scavengerhunt;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. import android.net.Uri;
  9. import android.os.Bundle;
  10. import android.provider.ContactsContract.Contacts;
  11. import android.widget.Button;
  12. import android.widget.CheckedTextView;
  13. import android.widget.SimpleAdapter;
  14. import android.widget.SimpleCursorAdapter;
  15. import android.app.Activity;
  16. import android.content.AsyncQueryHandler;
  17. import android.content.ContentResolver;
  18. import android.content.ContentValues;
  19. import android.content.Context;
  20. import android.content.Intent;
  21. import android.content.SharedPreferences;
  22. import android.content.res.Resources;
  23. import android.database.Cursor;
  24. import android.view.Menu;
  25. import android.view.View;
  26. import android.view.View.OnClickListener;
  27. import android.view.ViewGroup;
  28. import android.widget.ListView;
  29. import android.util.Log;
  30. import java.util.regex.Matcher;
  31. import java.util.regex.Pattern;
  32.  
  33. public class GatherListActivity extends Activity {
  34.  
  35.     protected ListView gather_list;
  36.     protected AsyncQueryHandler asyncGatherlistQueryHandler;
  37.     private SharedPreferences prefs;
  38.     protected ArrayList<HashMap<String,Object>> gather_data;
  39.    
  40.     final private int INTENT_REQUESTCODE_QRSCAN = 1;
  41.    
  42.     private class CheckedListAdapter extends SimpleAdapter {
  43.  
  44.         public CheckedListAdapter(Context context,
  45.                 List<? extends Map<String, ?>> data, int resource,
  46.                 String[] from, int[] to) {
  47.             super(context, data, resource, from, to);
  48.             // TODO Auto-generated constructor stub
  49.         }
  50.        
  51.         @Override
  52.         public View getView(int position, View convertView, ViewGroup parent) {
  53.             View v = super.getView(position, convertView, parent);
  54.             if (parent != null) {
  55.                 CheckedTextView ctv = (CheckedTextView)parent.findViewById(android.R.id.text1);
  56.                 if (ctv != null) {
  57.                     try {
  58.                         if (position < gather_data.size()) {
  59.                             HashMap<String,Object> hm = gather_data.get(position);
  60.                             if (ctv.isChecked() != (Boolean)hm.get("mark")) {
  61.                                 Log.v(this.getClass().getName(), String.format("Draw %s->%s %s", ctv.isChecked() ? "set" : "unset", (Boolean)hm.get("mark") ? "set" : "unset", hm.get("context")));
  62.                                 //ctv.setChecked((Boolean)hm.get("mark"));
  63.                                 //parent.invalidate();
  64.                                 ctv.toggle();
  65.                             }
  66.                         }
  67.                     }
  68.                     catch (Exception ex) {
  69.                         Log.v(this.getClass().getName(), ex.toString());
  70.                     }
  71.                 }
  72.             }
  73.             return v;
  74.         }
  75.     }
  76.    
  77.     @Override
  78.     protected void onCreate(Bundle savedInstanceState) {
  79.         super.onCreate(savedInstanceState);
  80.         setContentView(R.layout.activity_gatherlist);
  81.         Log.v(this.getClass().getName(), "onCreate()");
  82.  
  83.         internalRestoreInstanceState(savedInstanceState);
  84.        
  85.         String[] from = new String[] {"title", "context"};
  86.         int[] to = new int[] {android.R.id.text1, android.R.id.text2};
  87.         try {
  88.             gather_list = (ListView)findViewById(R.id.gather_list);
  89.             CheckedListAdapter adapter = new CheckedListAdapter(gather_list.getContext(), this.gather_data, R.layout.gather_list_item, from, to);
  90.             ListView lv = (ListView)this.findViewById(R.id.gather_list);
  91.             lv.setAdapter(adapter);
  92.         }
  93.         catch (IllegalArgumentException ex) {
  94.             Log.e(this.getClass().getName(), ex.toString());   
  95.             throw(ex);
  96.         }
  97.        
  98.         Button qrscanner_button = (Button)findViewById(R.id.qrscanner_button);
  99.         qrscanner_button.setOnClickListener(new OnClickListener() {
  100.             public void onClick(View v) {
  101.                 Log.v(this.getClass().getName(), "onClick()");
  102.                 Intent intent = new Intent("org.raboss.gamification.scavengerhunt.SCAN");
  103.                 intent.putExtra("EMBEDDED_INTENT", true);
  104.                 intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
  105.                 startActivityForResult(intent, INTENT_REQUESTCODE_QRSCAN);
  106.             }
  107.         });
  108.     }
  109.    
  110.     @Override
  111.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  112.         super.onActivityResult(requestCode, resultCode, data);
  113.         if (resultCode != RESULT_OK)
  114.             return;
  115.        
  116.         switch (requestCode) {
  117.         case INTENT_REQUESTCODE_QRSCAN:
  118.             onActivityResultQRScan(resultCode, data);
  119.             break;
  120.         }
  121.     }
  122.    
  123.     private void onActivityResultQRScan(int resultCode, Intent data) {
  124.         // Handle successful scan
  125.         String contents = data.getStringExtra("SCAN_RESULT");
  126.         Log.i(this.getClass().getName(), "Scanned QR-Code: "+contents);
  127.        
  128.         try {
  129.             Uri link = Uri.parse(contents);
  130.             String link_where = link.getQueryParameter("where");
  131.             Log.i(this.getClass().getName(), String.format("Stand: %s", link_where));
  132.             for(HashMap<String,Object> hm : this.gather_data) {
  133.                 String context = (String)hm.get("context");
  134.                 if (context.contains(link_where)) {
  135.                     hm.put("mark", Boolean.TRUE);
  136.                 }
  137.                 else if (link_where.contains("restart")) {
  138.                     hm.put("mark", Boolean.FALSE);
  139.                 }
  140.                 Log.i(this.getClass().getName(), String.format("%s ~ %s: %s", context, link_where, (Boolean)hm.get("mark") ? "set" : "unset"));
  141.             }
  142.             this.gather_list.invalidate();
  143.         }
  144.         catch (Exception ex) {
  145.             Log.i(this.getClass().getName(), ex.toString());
  146.         }
  147.     }
  148.    
  149.     @Override
  150.     protected void onSaveInstanceState(Bundle outState) {
  151.         super.onSaveInstanceState(outState);
  152.         Log.v(this.getClass().getName(), "onSaveInstanceState()");
  153.         this.internalSaveInstanceState(outState);
  154.     }
  155.  
  156.     protected void internalSaveInstanceState(Bundle outState) {
  157.         Log.v(this.getClass().getName(), "internalSaveInstanceState()");
  158.         prefs = getSharedPreferences(this.getClass().getName(), MODE_PRIVATE);
  159.         SharedPreferences.Editor editor = prefs.edit();
  160.         try {
  161.             for(HashMap<String,Object> hm : this.gather_data) {
  162.                 editor.putBoolean((String)hm.get("context"), (Boolean)hm.get("mark"));         
  163.             }
  164.         }
  165.         catch (Exception ex) {
  166.             Log.v(this.getClass().getName(), ex.toString());
  167.         }
  168.         editor.commit();
  169.     }
  170.  
  171.    
  172.     @Override
  173.     protected void onRestoreInstanceState(Bundle savedInstanceState) {
  174.         super.onRestoreInstanceState(savedInstanceState);
  175.         Log.v(this.getClass().getName(), "onRestoreInstanceState()");
  176.         this.internalRestoreInstanceState(savedInstanceState);
  177.     }
  178.    
  179.     protected void internalRestoreInstanceState(Bundle savedInstanceState) {
  180.         Log.v(this.getClass().getName(), "internalRestoreInstanceState()");
  181.         if (this.gather_data != null) {
  182.             Log.v(this.getClass().getName(), String.format("this.gather_data already initialised: %d", gather_data.size()));
  183.         }
  184.         else {
  185.             Resources res = getResources();
  186.             String[] cebit_stands = res.getStringArray(R.array.cebit_stands);
  187.             String[] go_slogen = res.getStringArray(R.array.go_slogan);
  188.             this.gather_data = new ArrayList<HashMap<String,Object>>();
  189.             for (int i = 0; i < cebit_stands.length; i++) {
  190.                 HashMap<String, Object> hm = new HashMap<String, Object>();
  191.                 hm.put("context", cebit_stands[i]);
  192.                 hm.put("title", go_slogen[i % go_slogen.length]);
  193.                 this.gather_data.add(hm);
  194.             }
  195.         }
  196.         prefs = getSharedPreferences(this.getClass().getName(), MODE_PRIVATE);
  197.         try {
  198.             for(HashMap<String,Object> hm : this.gather_data) {
  199.                 hm.put("mark", prefs.getBoolean((String)hm.get("context"), Boolean.FALSE));
  200.                 Log.v(this.getClass().getName(), String.format("Restore %s = %s", (String)hm.get("context"), (Boolean)hm.get("mark") ? "set" : "unset"));
  201.             }
  202.         }
  203.         catch (Exception ex) {
  204.             Log.v(this.getClass().getName(), ex.toString());
  205.         }
  206.     }
  207.    
  208.     @Override
  209.     public void finish() {
  210.         Log.v(this.getClass().getName(), "finish()");
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement