Created
September 28, 2014 21:24
-
-
Save Motoharujap/a9c66089a7570754ad95 to your computer and use it in GitHub Desktop.
MainActivity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.motoharu.shoplist; | |
import android.app.*; | |
import android.content.Context; | |
import android.content.CursorLoader; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.content.Loader; | |
import android.database.Cursor; | |
import android.os.Bundle; | |
import android.view.ContextMenu; | |
import android.view.KeyEvent; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
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 MyActivity extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor> { | |
private ListView lw; | |
private EditText edtxt; | |
private static EditText alertEd; | |
private static DBhelper dbhelper; | |
private SimpleCursorAdapter adapter; | |
private static final int ACTIVITY_CREATE = 0; | |
private static final int ACTIVITY_EDIT = 1; | |
private Cursor cursor; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_my); | |
dbhelper = new DBhelper(this); | |
fillData(); | |
//layout elements | |
lw = (ListView) findViewById(android.R.id.list); | |
//edittext view for adding list items | |
edtxt = (EditText) findViewById(R.id.editText); | |
//edittext view for context menu edit dialog | |
alertEd = (EditText) findViewById(R.id.alertEdit); | |
registerForContextMenu(lw); | |
getLoaderManager().initLoader(0, null, this); | |
lw.setOnItemClickListener(new OnItemClickListener() { | |
public void onItemClick(AdapterView<?> adapter, View v, | |
int position, long id) { | |
addToGoodsList(id); | |
} | |
}); | |
edtxt.setOnKeyListener(new View.OnKeyListener() { | |
@Override | |
public boolean onKey(View v, int keyCode, KeyEvent event) { | |
if (event.getAction()== KeyEvent.ACTION_DOWN) { | |
if (keyCode == KeyEvent.KEYCODE_ENTER) { | |
String name = edtxt.getText().toString(); | |
dbhelper.createTitlesValue(name); | |
getLoaderManager().getLoader(0).forceLoad(); | |
adapter.notifyDataSetChanged(); | |
if (edtxt.length() > 0) { | |
edtxt.getText().clear(); | |
} | |
return true; | |
} | |
} | |
return false; | |
} | |
}); | |
} | |
@Override | |
protected void onDestroy() | |
{ | |
super.onDestroy(); | |
dbhelper.close(); | |
} | |
private void fillData() { | |
cursor = dbhelper.getAllTitles(); | |
String[] from = new String[] { DBhelper.TITLES_NAME}; | |
int[] to = new int[] { R.id.title_name}; | |
// Теперь создадим адаптер массива и установим его для отображения наших | |
// данных | |
adapter = new SimpleCursorAdapter(this, | |
R.layout.list_item_view, cursor, from, to, 0); | |
setListAdapter(adapter); | |
adapter.notifyDataSetChanged(); | |
cursor.close(); | |
} | |
protected void onActivityResult(int requestCode, int resultCode, | |
Intent intent) { | |
super.onActivityResult(requestCode, resultCode, intent); | |
if (resultCode == RESULT_OK) { | |
return; | |
} | |
} | |
private void addToGoodsList(long id) { | |
Intent intent = new Intent(this, ItemsActivity.class); | |
intent.putExtra(DBhelper.MAIN_ID, id); | |
startActivityForResult(intent, ACTIVITY_CREATE); | |
String idshow = DBhelper.MAIN_ID; | |
Toast.makeText(this, idshow, Toast.LENGTH_SHORT).show(); | |
} | |
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo info){ | |
super.onCreateContextMenu(menu, v, info); | |
getMenuInflater().inflate(R.menu.actions, menu); | |
} | |
public boolean onContextItemSelected(MenuItem item) { | |
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); | |
final long titleID = info.id; | |
switch (item.getItemId()) { | |
case R.id.cnt_mnu_delete: | |
dbhelper.deleteData(info.id); | |
getLoaderManager().getLoader(0).forceLoad(); | |
adapter.notifyDataSetChanged(); | |
return true; | |
case R.id.cnt_mnu_edit: | |
AlertDialog.Builder alert = new AlertDialog.Builder(this); | |
// Set an EditText view to get user input | |
final EditText input = new EditText(this); | |
input.requestFocus(); | |
alert.setTitle("Edit"); | |
alert.setMessage("Change an item"); | |
alert.setView(input); | |
Cursor c = dbhelper.readData(); | |
input.setText(c.getString(c.getColumnIndex(dbhelper.TITLES_NAME))); | |
alert.setPositiveButton("Save", new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int whichButton) { | |
String name = input.getText().toString(); | |
dbhelper.updateTitlesValue(titleID, name); | |
getLoaderManager().getLoader(0).forceLoad(); | |
adapter.notifyDataSetChanged(); | |
} | |
}); | |
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int whichButton) { | |
dialog.cancel(); | |
} | |
}); | |
alert.show(); | |
} | |
return super.onContextItemSelected(item); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.my, menu); | |
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. | |
int id = item.getItemId(); | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
public Loader<Cursor> onCreateLoader(int id, Bundle args) { | |
return new MyCursorLoader(this, dbhelper); | |
} | |
@Override | |
public void onLoadFinished(Loader<Cursor> loader, Cursor data) { | |
adapter.swapCursor(data); | |
} | |
@Override | |
public void onLoaderReset(Loader<Cursor> loader) { | |
adapter.swapCursor(null); | |
} | |
static class MyCursorLoader extends CursorLoader { | |
DBhelper dBhelper; | |
public MyCursorLoader(Context context, DBhelper dbcon) { | |
super(context); | |
this.dBhelper = dbcon; | |
} | |
public Cursor loadInBackground() { | |
Cursor cursor = dbhelper.getAllData(); | |
return cursor; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment