Advertisement
Guest User

CheckTextView change state with adapter

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