Skip to content

Instantly share code, notes, and snippets.

@Motoharujap
Created September 28, 2014 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Motoharujap/dd199422d039acb6be24 to your computer and use it in GitHub Desktop.
Save Motoharujap/dd199422d039acb6be24 to your computer and use it in GitHub Desktop.
items
package com.example.motoharu.shoplist;
import android.app.Activity;
import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class ItemsActivity extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor> {
public ListView lw;
public TextView productText;
public EditText et;
public SimpleCursorAdapter adapter2;
public static DBhelper mDBhelper;
public SimpleCursorAdapter adapter;
public Long goodsId;
public Cursor cursor;
public Long mRowId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_items);
mDBhelper = new DBhelper(this);
Bundle extras = getIntent().getExtras();
lw = (ListView) findViewById(android.R.id.list);
et = (EditText) findViewById(R.id.editText2);
mRowId = null;
mRowId = (savedInstanceState == null) ? null
: (Long) savedInstanceState
.getSerializable(DBhelper.MAIN_ID);
if (extras != null) {
mRowId = extras.getLong(DBhelper.MAIN_ID);
}
fillData(populateFields());
//getLoaderManager().initLoader(0, null, this);
lw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
makeToast();
}
});
et.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 = et.getText().toString();
goodsId = mDBhelper.createGoodsValue(name);
mDBhelper.createTags(goodsId, mRowId);
et.setText("");
getLoaderManager().getLoader(0).forceLoad();
adapter.notifyDataSetChanged();
return true;
}
}
return false;
}
});
}
public String populateFields()
{
cursor = mDBhelper.getTitles(mRowId);
String name = cursor.getString(1);
Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
return name;
}
public void makeToast()
{
String idshow = mRowId.toString();
Toast.makeText(getBaseContext(), idshow, Toast.LENGTH_SHORT).show();
}
private void fillData(String titles_name) {
SQLiteDatabase db = mDBhelper.getReadableDatabase();
String selectQuery = "SELECT *FROM " + mDBhelper.TABLE_GOODS + " tg, "
+ mDBhelper.TABLE_TITLES + " tt, " + mDBhelper.TABLE_TAGS + " tgs WHERE tt."
+ mDBhelper.TITLES_NAME + " = '" + titles_name + "'" + " AND tt." + mDBhelper.MAIN_ID
+ " = " + "tgs." + mDBhelper.TITLES_ID + " AND tg." + mDBhelper.MAIN_ID + " = "
+ "tgs." + mDBhelper.GOODS_ID;
Cursor c = db.rawQuery(selectQuery, null);
//cursor = mDBhelper.getGoods(mRowId);
String[] from = new String[] { DBhelper.GOODS_NAME};
int[] to = new int[] { R.id.goods_name};
adapter = new SimpleCursorAdapter(this,
R.layout.test, c, from, to, 0);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
getLoaderManager().initLoader(0, null, this);
cursor.close();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new MyCursorLoader2(this, mDBhelper);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
static class MyCursorLoader2 extends CursorLoader {
DBhelper dBhelper;
public MyCursorLoader2(Context context, DBhelper dbcon) {
super(context);
this.dBhelper = dbcon;
}
public Cursor loadInBackground() {
Cursor cursor = mDBhelper.getAllDataGoods();
return cursor;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment