[GIF OF ACTIVITY][1] <--------------- WATCH ACTIVITY
I have been having issues trying to delete the item on the row from SQLite in a ListView So far I have been able to successfully delete always first item on list. whenever I click my Delete Button on my ListView.
If you have any input or feedback don't hesitate to indulge me please!
Here is the relevant code
DBHelper
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "ListappDB";
private static final int DB_VERSION = 1;
public static final String DB_TABLE = "List";
public static final String DB_COLUMN = "Item";
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = String.format("CREATE TABLE %s(ID INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT NOT NULL)", DB_TABLE, DB_COLUMN);
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = String.format("DELETE TABLE IF EXISTS %s", DB_TABLE);
db.execSQL(query);
onCreate(db);
}
public void insertNewItem(String item){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DB_COLUMN, item);
db.insertWithOnConflict(DB_TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
db.close();
}
public void deleteItem(String item){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(DB_TABLE, DB_COLUMN + "=?", new String[]{ item });
db.close();
}
public ArrayList<String> getItemList(){
ArrayList<String> itemList = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(DB_TABLE, new String[]{DB_COLUMN},null,null,null,null,null);
while (cursor.moveToNext()){
int index = cursor.getColumnIndex(DB_COLUMN);
itemList.add(cursor.getString(index));
}
cursor.close();
db.close();
return itemList;
}
}
Main Activity
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DBHelper mDBHelper;
private ArrayAdapter<String> mAdapter;
private ListView mListView;
private Button mButton;
private Button mDeleteButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_main);
mDBHelper = new DBHelper(this);
mAuth = FirebaseAuth.getInstance();
mListView = (ListView) findViewById(R.id.list);
mButton = (Button) findViewById(R.id.buttonQR);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<String> itemList = mDBHelper.getItemList();
MultiFormatWriter multiFW = new MultiFormatWriter();
try {
BitMatrix bitMatrix = multiFW.encode(String.valueOf(itemList), BarcodeFormat.QR_CODE, 200, 200);
BarcodeEncoder enconder = new BarcodeEncoder();
Bitmap bitmap = enconder.createBitmap(bitMatrix);
Intent intent = new Intent(getApplicationContext(), QR.class);
intent.putExtra("qrcode", bitmap);
startActivity(intent);
} catch (WriterException e) {
e.printStackTrace();
}
}
});
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null){
goAuthenticate();
}
}
};
loadItemList();
}
private void loadItemList() {
ArrayList<String> itemList = mDBHelper.getItemList();
if (mAdapter==null){
mAdapter = new ArrayAdapter<String>(this, R.layout.row, R.id.item_name, itemList);
mListView.setAdapter(mAdapter);
} else {
mAdapter.clear();
mAdapter.addAll(itemList);
mAdapter.notifyDataSetChanged();
}
}
public void goAuthenticate(){
Intent mLogin = new Intent(MainActivity.this, Authentication.class);
mLogin.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mLogin);
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.logout:
mAuth.signOut();
LoginManager.getInstance().logOut();
break;
case R.id.addNewItem:
final EditText itemEditText = new EditText(this);
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle("Add New Item")
.setView(itemEditText)
.setPositiveButton("Add", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
String item = String.valueOf(itemEditText.getText());
if(item.length() <= 0|| item.equals("")){
Toast.makeText(MainActivity.this, "Item Cant Be Blank",
Toast.LENGTH_LONG).show();
} else {
mDBHelper.insertNewItem(item);
loadItemList();
}
}
})
.setNegativeButton("Cancel", null)
.create();
alertDialog.show();
break;
}
return super.onOptionsItemSelected(item);
}
public void deleteItemH(View view){
TextView itemTextView = (TextView) view.findViewById(R.id.item_name);
String item = String.valueOf(itemTextView.getText());
mDBHelper.deleteItem(item);
loadItemList();
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null){
mAuth.removeAuthStateListener(mAuthListener);
}
}
}
Row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:id="@+id/item_name"
android:text="Example"
android:textSize="20dp"
android:layout_alignParentStart="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnDelete"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="DELETE"
android:onClick="deleteItemH"
android:layout_centerVertical="true"/>
Activity Main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="tech.destinum.listapp.MainActivity"
android:layout_centerVertical="true">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list"
android:clipChildren="false"
android:layout_weight="1"/>
<RelativeLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonQR"
android:text="Generate"/>
</RelativeLayout>