Advertisement
Guest User

moimeme

a guest
Sep 5th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.52 KB | None | 0 0
  1.      In this activity I have 3 buttons and 1 radiogroup.
  2.      
  3.      The radiogroup is to set the order. ASC or DESC
  4.      Button one is to show an alertdialog where the user picks one option
  5.      Button two is to show an alertdialog where the user picks another option
  6.      Button three is to set the search on the database with the choices made above
  7.      
  8.      
  9.      This is the code on the manifest (removed all that wasn't important)
  10.      
  11.     <?xml version="1.0" encoding="utf-8"?>
  12.     <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  13.         package="com.stack"
  14.         android:installLocation="auto"
  15.         android:versionCode="1"
  16.         android:versionName="1.0.1" >
  17.  
  18.         <uses-sdk
  19.             android:minSdkVersion="8"
  20.             android:targetSdkVersion="15" />
  21.  
  22.         <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  23.         <supports-screens android:anyDensity="true" android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" />
  24.  
  25.         <application
  26.             android:icon="@drawable/ic_launcher"
  27.             android:label="@string/app_name" >
  28.            
  29.             ........
  30.            
  31.             <activity
  32.                 android:name=".MyClass"
  33.                 android:exported="false"
  34.                 android:label="@string/app_name"
  35.                 android:theme="@style/AppTheme" >
  36.                 <intent-filter>
  37.                     <action android:name="android.intent.action.MYCLASS" />
  38.  
  39.                     <category android:name="android.intent.category.DEFAULT" />
  40.                 </intent-filter>
  41.             </activity>
  42.             .......
  43.         </application>
  44.     </manifest>
  45.    
  46.    
  47.    
  48.     After all the modifications here is the code of MyClass
  49.    
  50.     public class MyClass extends ListActivity {
  51.  
  52.     // Cria a base de dados
  53.     Database db = new Database(this);
  54.    
  55.     ListView listContent;
  56.     Cursor query;
  57.     MyAdapt cursorAdapter;
  58.  
  59.     @Override
  60.     protected void onCreate(Bundle savedInstanceState) {
  61.         super.onCreate(savedInstanceState);
  62.         setContentView(R.layout.mylayout);
  63.        
  64.  
  65.         // Sets my buttons and everithyng else
  66.        
  67.         // Button 1 - alertdialog that showns items so that the user picks one
  68.         pickButOne.setOnClickListener(new OnClickListener() {
  69.            
  70.             @Override
  71.             public void onClick(View v) {
  72.  
  73.                 AlertDialog.Builder pickButOne = new AlertDialog.Builder(this);
  74.                 pickButOne.setTitle(...);
  75.                 pickButOne.setMultiChoiceItems(..., null, new DialogInterface.OnMultiChoiceClickListener() {
  76.  
  77.                     @Override
  78.                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
  79.                         // Some code
  80.                     }
  81.                 });
  82.                 pickButOne.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  83.  
  84.                     @Override
  85.                     public void onClick(DialogInterface dialog, int which) {
  86.                         // Some code
  87.                     }
  88.                 });
  89.                 pickGroup.show();
  90.             }
  91.         });
  92.  
  93.         // Button 2 - alertdialog that showns items so that the user picks one
  94.         pickButTwo.setOnClickListener(new OnClickListener() {
  95.            
  96.             @Override
  97.             public void onClick(View v) {
  98.    
  99.                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
  100.                 builder.setTitle(...));
  101.    
  102.                 builder.setSingleChoiceItems(..., ..., new DialogInterface.OnClickListener() {
  103.                     public void onClick(DialogInterface dialog, int item) {
  104.                         // Some code
  105.                     }
  106.                 });
  107.    
  108.                 AlertDialog alert = builder.create();
  109.                 alert.show();
  110.                
  111.             }
  112.         });
  113.    
  114.         // RadioGroup choice
  115.         pickOrder.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  116.            
  117.             @Override
  118.             public void onCheckedChanged(RadioGroup group, int checkedId) {
  119.                 switch (checkedId) {
  120.                     case R.id.procOrderASC:
  121.                         order = 0;
  122.                         queryData[2] = "ASC";
  123.                         break;
  124.                     case R.id.procOrderDSC:
  125.                         order = 1;
  126.                         queryData[2] = "DESC";
  127.                         break;
  128.                 }
  129.             }
  130.         });
  131.        
  132.         // Button 3 - performs the search on the database with the user choices
  133.         search.setOnClickListener(new OnClickListener() {
  134.            
  135.             @Override
  136.             public void onClick(View v) {
  137.                 // The result from this query is shown on the listview. After the search made if I rotate the phone, the layout kicks in (have two layouts, one for each orientation)
  138.                 db.open();
  139.                 query = db.getData(.....);
  140.                 cursorAdapter = new MyAdapt(this, query);
  141.                 listContent.setAdapter(cursorAdapter);
  142.                 db.close();    
  143.             }
  144.         });
  145.     }
  146.    
  147.  
  148.     @Override
  149.     protected void onListItemClick(ListView l, View v, int position, long id) {
  150.         super.onListItemClick(l, v, position, id);
  151.            
  152.             // After the listview is shown, item picks one item and the data in this item is passed to another activity
  153.             Cursor somequery = (Cursor) getListView().getItemAtPosition(position);
  154.            
  155.             Intent i = new Intent(this, AnotherClass.class);
  156.             i.putExtra("Test", somequery.getFloat(0));
  157.             startActivity(i);
  158.         }
  159.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement