Reputation: 21
I have 2 activities, When I enter something in the first activity and press "save button" it saves the text via SQLite Database. In the second activity, I have a list of saved items, and when I click one of them, the onItemClick method needs to trigger and open up a third activity where I can edit or delete the selected item. Now, everything works fine to that point, when I click on an item. When I tap it, the third activity loads, but doesn't show anything, and after few seconds it closes and drops me back to the previous activity. Here's the code snippets:
Code snippet of second activity, where the list is:
DatabaseHelper mDatabaseHelper;
private ListView mListView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
mListView=(ListView) findViewById(R.id.listView);
mDatabaseHelper=new DatabaseHelper(this);
populateListView();
}
private void populateListView() {
Log.d(TAG,"populateListView: Displaying data in ListView.");
Cursor data=mDatabaseHelper.getData();
ArrayList<String> listData=new ArrayList<>();
while (data.moveToNext()){
listData.add(data.getString(1));
}
ListAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, listData);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//string name=the item saved
String name = adapterView.getItemAtPosition(i).toString();
Log.d(TAG,"onItemClick: You Clicked on "+ name);
Cursor data=mDatabaseHelper.getItemID(name);
int itemID= -1;
while (data.moveToNext()){
itemID = data.getInt(0);
}
if (itemID > -1){
Log.d(TAG,"onItemClick: The ID is: "+itemID);
Intent editScreenIntent = new Intent(MoneyTracking.this, EditDataActivity.class);
editScreenIntent.putExtra("id",itemID);
editScreenIntent.putExtra("name",name);
startActivity(editScreenIntent);
}
else{
toastMessage("No ID associated with that list item.");
}
}
});
}
/**
* Customizable toast
* @param message
*/
private void toastMessage(String message){
Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}}
Then the "third activity" which won't load up:
private Button btnSave2, btnDelete1;
private EditText editText00;
DatabaseHelper mDatabaseHelper;
private String selectedName;
private int selectedID;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_data_layout);
btnSave2=(Button)findViewById(R.id.btnSaveTwo);
btnDelete1=(Button)findViewById(R.id.btnDelete);
editText00=(EditText) findViewById(R.id.editText00);
mDatabaseHelper=new DatabaseHelper(this);
Intent recievedIntent=getIntent();
selectedID=recievedIntent.getIntExtra("id",-1);
selectedName=recievedIntent.getStringExtra("name");
editText00.setText(selectedName);
btnSave2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String item=editText00.getText().toString();
if (!item.equals("")){
mDatabaseHelper.updateName(item,selectedID,selectedName);
}else{
toastMessage("Morate uneti nesto da bi ste sacuvali!");
}
}
});
btnDelete1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDatabaseHelper.deleteName(selectedID,selectedName);
editText00.setText("");
toastMessage("Izbrisano!");
}
});
}
/**
* Customizable toast
* @param message
*/
private void toastMessage(String message){
Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}
}
Database Helper:
private static final String TAG="DatabaseHelper";
private static final String TABLE_NAME="mt_lists";
private static final String COL1= "ID";
private static final String COL2= "listndprice";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable="CREATE TABLE "+ TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT, "+
COL2+" TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean appData(String item)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
Log.d(TAG, "appData: Adding "+item+" to "+TABLE_NAME);
long result=db.insert(TABLE_NAME, null, contentValues);
if (result == -1){
return false;
}
else {
return true;
}
}
/**
* Returns all the data from database
* @return
*/
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query="SELECT * FROM " + TABLE_NAME;
Cursor data=db.rawQuery(query,null);
return data;
}
/**
*
* @param name
* @return
*/
public Cursor getItemID(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query="SELECT"+ COL1 +" FROM "+TABLE_NAME+" WHERE "+COL2+ " = '" + name + " ' ";
Cursor data = db.rawQuery(query,null);
return data;
}
/**
* Update database
* @param newName
* @param id
* @param oldName
*/
public void updateName(String newName,int id,String oldName){
SQLiteDatabase db=this.getWritableDatabase();
String query="UPDATE"+TABLE_NAME+"SET"+COL2+"='"+newName+"' WHERE"+COL1+"= '"+id+"'"+"AND"+COL2+"= '"+oldName+"'";
Log.d(TAG,"updateName: query: "+query);
Log.d(TAG,"updateName: Setting name to "+newName);
db.execSQL(query);
}
/**
* Delete from database
* @param id
* @param name
*/
public void deleteName(int id,String name){
SQLiteDatabase db=this.getWritableDatabase();
String query="DELETE FROM" + TABLE_NAME + "WHERE" + COL1 + "= '" + id + "'" + " AND " + COL2 + "= '" +name +"'";
Log.d(TAG,"deleteName: query: "+query);
Log.d(TAG,"deleteName: Deleting "+name+"from database.");
db.execSQL(query);
}
EDIT LogCat: enter image description here
Upvotes: 1
Views: 91
Reputation: 826
Ok I think I found what your problem is. You need to put a space in between your SQL keywords and your column names/values. In your code here your query is being written as "SELECTID FROM...". But it needs to read "SELECT ID FROM...". Notice the space between SELECT and ID!:
public Cursor getItemID(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query="SELECT"+ COL1 +" FROM "+TABLE_NAME+" WHERE "+COL2+ " = '" + name + " ' ";
Cursor data = db.rawQuery(query,null);
return data;
}
Change the above to
public Cursor getItemID(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query="SELECT "+ COL1 +" FROM "+TABLE_NAME+" WHERE "+COL2+ " = '" + name + " ' ";
Cursor data = db.rawQuery(query,null);
return data;
}
Also make sure your other SQL methods don't have the same problem!
Upvotes: 1