Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.Activity;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.SimpleCursorAdapter;
- import android.widget.Toast;
- public class SecondActivity extends Activity {
- EditText et1;
- EditText et2;
- String et1text;
- String et2text;
- ListView lv;
- Database database;
- SimpleCursorAdapter adapter;
- int pr;
- String na;
- //@SuppressWarnings("deprecation")
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_second);
- et1 = (EditText)findViewById(R.id.editText1);
- et2 = (EditText)findViewById(R.id.editText2);
- lv = (ListView)findViewById(R.id.listView1);
- database = new Database(this);
- Cursor cursor = database.getAllProducts();
- adapter = new Adapter(this, R.layout.peak_item, cursor, new String[] { "name", "price" }, new int[] { R.id.textView1, R.id.textView2 });
- lv.setAdapter(adapter);
- //lv.setClickable(true);
- //lv.setFocusable(false);
- lv.setOnItemClickListener(new OnItemClickListener() {
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- database.deleteProduct(id);//create removemethod in database class
- adapter.getCursor().requery();
- Toast.makeText(SecondActivity.this, "clicked", Toast.LENGTH_SHORT).show();
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- public void myClick(View v) {
- //database.deleteProduct(2);
- Toast.makeText(getApplicationContext(), v+" Element: ", Toast.LENGTH_LONG).show();
- }
- @SuppressWarnings("deprecation")
- public void onClick(View v) {
- na = et1.getText().toString();
- et2text = et2.getText().toString();
- try {
- pr = Integer.parseInt(et2text);
- } catch(NumberFormatException nfe) {
- System.out.println("Could not parse " + nfe);
- }
- database.addProduct(na, pr);
- adapter.getCursor().requery();
- }
- }
- ////////////////////////////////////////////////////////////////
- LAYOUT
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ListView
- android:id="@+id/listView1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="0.2" >
- </ListView>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="0.9" >
- <EditText
- android:id="@+id/editText1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:ems="10"
- android:hint="name1" />
- <EditText
- android:id="@+id/editText2"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:ems="10"
- android:hint="price"
- android:inputType="number" />
- </LinearLayout>
- <Button
- android:id="@+id/button1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="0.9"
- android:onClick="onClick"
- android:text="Button" />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment