katjet0

second activity

Mar 23rd, 2013
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.database.Cursor;
  3. import android.os.Bundle;
  4. import android.view.Menu;
  5. import android.view.View;
  6. import android.widget.AdapterView;
  7. import android.widget.AdapterView.OnItemClickListener;
  8. import android.widget.EditText;
  9. import android.widget.ListView;
  10. import android.widget.SimpleCursorAdapter;
  11. import android.widget.Toast;
  12.  
  13. public class SecondActivity extends Activity {
  14.  
  15.     EditText et1;
  16.     EditText et2;
  17.     String et1text;
  18.     String et2text;
  19.     ListView lv;
  20.     Database database;
  21.     SimpleCursorAdapter adapter;
  22.     int pr;
  23.     String na;
  24.  
  25.     //@SuppressWarnings("deprecation")
  26.     @Override
  27.     public void onCreate(Bundle savedInstanceState) {
  28.         super.onCreate(savedInstanceState);
  29.         setContentView(R.layout.activity_second);
  30.        
  31.         et1 = (EditText)findViewById(R.id.editText1);
  32.         et2 = (EditText)findViewById(R.id.editText2);
  33.         lv = (ListView)findViewById(R.id.listView1);
  34.  
  35.         database = new Database(this);
  36.         Cursor cursor = database.getAllProducts();
  37.        
  38.         adapter = new Adapter(this, R.layout.peak_item, cursor, new String[] { "name", "price" }, new int[] { R.id.textView1, R.id.textView2 });
  39.            
  40.         lv.setAdapter(adapter);    
  41.         //lv.setClickable(true);
  42.         //lv.setFocusable(false);
  43.        
  44.         lv.setOnItemClickListener(new OnItemClickListener() {
  45.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  46.                 database.deleteProduct(id);//create removemethod in database class
  47.                 adapter.getCursor().requery();
  48.                 Toast.makeText(SecondActivity.this, "clicked", Toast.LENGTH_SHORT).show();
  49.             }
  50.         });
  51.  
  52.     }
  53.  
  54.     @Override
  55.     public boolean onCreateOptionsMenu(Menu menu) {
  56.         getMenuInflater().inflate(R.menu.activity_main, menu);
  57.         return true;
  58.     }
  59.    
  60. public void myClick(View v) {
  61.         //database.deleteProduct(2);
  62.         Toast.makeText(getApplicationContext(), v+"  Element: ", Toast.LENGTH_LONG).show();
  63.  
  64.     }
  65.    
  66.    
  67.    
  68.     @SuppressWarnings("deprecation")
  69.     public void onClick(View v) {
  70.        
  71.         na = et1.getText().toString();
  72.         et2text = et2.getText().toString();
  73.         try {
  74.             pr = Integer.parseInt(et2text);          
  75.         } catch(NumberFormatException nfe) {
  76.            System.out.println("Could not parse " + nfe);
  77.         }
  78.         database.addProduct(na, pr);
  79.         adapter.getCursor().requery();
  80.     }
  81.    
  82. }
  83.  
  84.  
  85.  
  86.  
  87. ////////////////////////////////////////////////////////////////
  88. LAYOUT
  89.  
  90.  
  91.  
  92. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  93.     xmlns:tools="http://schemas.android.com/tools"
  94.     android:layout_width="match_parent"
  95.     android:layout_height="match_parent"
  96.     android:orientation="vertical" >
  97.  
  98.     <ListView
  99.         android:id="@+id/listView1"
  100.         android:layout_width="match_parent"
  101.         android:layout_height="match_parent"
  102.         android:layout_weight="0.2" >
  103.     </ListView>
  104.  
  105.     <LinearLayout
  106.         android:layout_width="match_parent"
  107.         android:layout_height="match_parent"
  108.         android:layout_weight="0.9" >
  109.  
  110.         <EditText
  111.             android:id="@+id/editText1"
  112.             android:layout_width="match_parent"
  113.             android:layout_height="match_parent"
  114.             android:layout_weight="1"
  115.             android:ems="10"
  116.             android:hint="name1" />
  117.  
  118.         <EditText
  119.             android:id="@+id/editText2"
  120.             android:layout_width="match_parent"
  121.             android:layout_height="match_parent"
  122.             android:layout_weight="1"
  123.             android:ems="10"
  124.             android:hint="price"
  125.             android:inputType="number" />
  126.  
  127.     </LinearLayout>
  128.  
  129.     <Button
  130.         android:id="@+id/button1"
  131.         android:layout_width="match_parent"
  132.         android:layout_height="match_parent"
  133.         android:layout_weight="0.9"
  134.         android:onClick="onClick"
  135.         android:text="Button" />
  136.  
  137. </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment