@Override public void onClick(View v) { switch (v.getId()) { case R.id.ButResistBodyItem: String str = ((EditText) findViewById(R.id.ETbodyitem)).getText().toString(); if (!str.equals("")) { int num = MailBodyGenerator.getInstance().getbIMaster().getNextNum(_lineNum); ContentValues cv = new ContentValues(); cv.put(ReturnMailerDBDefinition.MailBodyItemColumns.COL_GID, _lineNum + 1); cv.put(ReturnMailerDBDefinition.MailBodyItemColumns.COL_NUM, num); cv.put(ReturnMailerDBDefinition.MailBodyItemColumns.COL_ITEM, str); Uri uri = DBOperator.insert(ReturnMailerDBDefinition.CONTENT_URI, cv); if (uri != null) { MailBodyItem mbi = new MailBodyItem(uri.getPathSegments().get(1), _lineNum, num, str); MailBodyGenerator.getInstance().getbIMaster().add(_lineNum, mbi); ((EditText) findViewById(R.id.ETbodyitem)).setText(""); _adapter.notifyDataSetChanged(); Toast.makeText(this, "New Item was added!!", Toast.LENGTH_SHORT).show(); } } break; case R.id.ButDeleteBodyItem: if (MailBodyGenerator.getInstance().getbIMaster().size(_lineNum) <= 1) { Toast.makeText(this, "Can't delete all Item!!", Toast.LENGTH_SHORT).show(); break; } String _id = ((MailBodyItem) _listView.getItemAtPosition(_listView.getCheckedItemPosition())) .get_ID(); if (DBOperator.delete( ReturnMailerDBDefinition.CONTENT_URI, MailBodyItemColumns._ID + " = " + _id, null) >= 1) { MailBodyGenerator.getInstance() .getbIMaster() .remove(_lineNum, _listView.getCheckedItemPosition()); _adapter.notifyDataSetChanged(); Toast.makeText(this, "One Item was deleted!!", Toast.LENGTH_SHORT).show(); } break; } }
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.registbodyitem); ((Button) findViewById(R.id.ButResistBodyItem)).setOnClickListener(this); ((Button) findViewById(R.id.ButDeleteBodyItem)).setOnClickListener(this); _lineNum = getIntent().getIntExtra(MailBodySetting.LINE_NUM, 0); _adapter = new ListViewAdapter<MailBodyItem>( this, R.layout.list_item, MailBodyGenerator.getInstance().getbIMaster().get(_lineNum)); _listView = (ListView) findViewById(R.id.LVBodyItem); _listView.setAdapter(_adapter); _listView.setItemsCanFocus(false); _listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); }