Вы находитесь на странице: 1из 5

package com.example.

shoppinglist;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import

android.support.v7.app.ActionBar;
android.support.v7.app.ActionBarActivity;
android.content.Intent;
android.database.Cursor;
android.net.Uri;
android.os.Bundle;
android.os.Environment;
android.view.Menu;
android.view.MenuItem;
android.view.View;
android.view.View.OnClickListener;
android.view.WindowManager;
android.widget.AdapterView;
android.widget.ArrayAdapter;
android.widget.Button;
android.widget.EditText;
android.widget.Spinner;
android.widget.Toast;
android.widget.AdapterView.OnItemSelectedListener;

public class EditActivity extends ActionBarActivity {


EditText name, updateName;
Button update;
Spinner categorySpinner;
int selectedCategoryPosition;
protected String categorySelected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
selectedCategoryPosition=0;
categorySpinner = (Spinner) findViewById(R.id.sp_category);
name = (EditText) findViewById(R.id.et_item_added);
updateName = (EditText) findViewById(R.id.et_item_update_name);
update = (Button) findViewById(R.id.bt_update);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INP
UT_STATE_HIDDEN);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.category_array, android.R.layout.simple_
spinner_item);
// Set the layout to use for each dropdown item
adapter.setDropDownViewResource(android.R.layout.simple_spinner_
dropdown_item);
categorySpinner.setAdapter(adapter);
ActionBar actionBar = getSupportActionBar();
//
actionBar.hide();
actionBar.setHomeButtonEnabled(true);
update.setOnClickListener(new OnClickListener() {
@Override

public void onClick(View arg0) {


// TODO Auto-generated method stub
String strItemName = name.getText().toString().t
rim();
String strUpdatedName = updateName.getText().toS
tring().trim();
if (strItemName.equals(""))
name.setError("Please enter a valid name
");
else if (strUpdatedName.equals(""))
name.setError("Please enter a valid name
");
else if (selectedCategoryPosition == 0)
Toast.makeText(EditActivity.this, "Pleas
e Select Category",
Toast.LENGTH_SHORT).show
();
else {
DbAdapter dbAdapter= new DbAdapter(EditActivity.
this);
dbAdapter.open();
Boolean result=dbAdapter.editItem(strItemName, s
trUpdatedName, categorySelected);

if(result)
Toast.makeText(EditActivity.this, "Item Edited s
uccessfully", Toast.LENGTH_SHORT).show();
else
Toast.makeText(EditActivity.this, " Item
does not exist", Toast.LENGTH_SHORT).show();
dbAdapter.close();
}
}
});
categorySpinner.setOnItemSelectedListener(new OnItemSelectedList
ener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg
1,
int position, long arg3) {
// TODO Auto-generated method stub
selectedCategoryPosition = position;
categorySelected=arg0.getItemAtPosition(position
).toString();
// Toast.makeText(AddActivity.this,
// "selected Category="+selectedCategory+" posit
ion="+position,Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.add, menu);
menu.findItem(R.id.action_edit).setVisible(false);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
Intent intent;
int id = item.getItemId();
if(id== android.R.id.home )
{
intent= new Intent(EditActivity.this, ViewActivity.class
);
startActivity(intent);
return true;
}
if (id == R.id.action_export_all) {
// abc="Guwahati";
DbAdapter dbAdapter = new DbAdapter(EditActivity.this);
dbAdapter.open();
Cursor cursor = dbAdapter.getAllItemsWithCategory();
generateCsvFile("all_items.csv", cursor);
dbAdapter.close();
return true;
}
if (id == R.id.action_export_essential) {
DbAdapter dbAdapter = new DbAdapter(EditActivity.this);
dbAdapter.open();
Cursor cursor = dbAdapter.getEssentialItemsWithCategory(
);
generateCsvFile("essential_items.csv", cursor);
dbAdapter.close();
return true;
}
if (id == R.id.action_export_stocked) {

DbAdapter dbAdapter = new DbAdapter(EditActivity.this);


dbAdapter.open();
Cursor cursor = dbAdapter.getStockedItemsWithCategory();
generateCsvFile("stocked_items.csv", cursor);
dbAdapter.close();
return true;
}
if (id == R.id.action_add) {
intent= new Intent(EditActivity.this, AddActivity.class)
;
startActivity(intent);
return true;
}
if (id == R.id.action_edit) {
intent= new Intent(EditActivity.this, EditActivity.class
);
startActivity(intent);
return true;
}
if (id == R.id.action_delete) {
intent= new Intent(EditActivity.this, DeleteActivity.cla
ss);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
public void generateCsvFile(String sFileName, Cursor cursor) {
try {
Toast.makeText(this, "Generating csv writer", Toast.LENG
TH_SHORT)
.show();
File root = Environment.getExternalStorageDirectory();
File gpxfile = new File(root, sFileName);
FileWriter writer = new FileWriter(gpxfile);
if (cursor != null && cursor.moveToFirst()) {
writer.append("name");
writer.append(',');
writer.append("category");
writer.append('\n');
do {
writer.append(cursor.getString(0));
writer.append(',');
writer.append(cursor.getString(1));
writer.append('\n');

} while (cursor.moveToNext());
Toast.makeText(this, "completed writer", Toast.L
ENGTH_SHORT)
.show();
}
writer.flush();
writer.close();
String filelocation = root + "/" + sFileName;
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
// set the type to 'email'
emailIntent.setType("vnd.android.cursor.dir/email");
String to[] = { "" };
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
Uri uri = Uri.fromFile(gpxfile);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
// the mail subject
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent, "Send em
ail..."));
// startActivity(Intent.createChooser(emailIntent ,
// "Pick an Email provider"));
Toast.makeText(this, "email successful", Toast.LENGTH_SH
ORT);
} catch (IOException e) {
e.printStackTrace();
}
}
}

Вам также может понравиться