Swipe from left to right to delete items from the ListView… here is a solution to the problem.
Swipe from left to right to delete items from the ListView
I’m trying to figure out how to remove an item from the ListView when I swipe from left to right. I will attach my code below. As of now, I’m using SQLite DB and created my own custom ListView adapter. I’m creating a simple list to remove and add items. I have work on adding items, now I just need to figure out how to remove an item from the ListView when swiping from left to right. My idea is that when you swipe from left to right, a button that says “Delete” appears. Users can tap it to remove an item from the list.
main_menu_activity.java
public class main_menu_activity extends Activity
{
DatabaseHandler db;
ArrayList<Account> account_details;
ListView accountList;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu_activity);
accountList = (ListView)findViewById(R.id.accountListView);
Initiates SQLite Database
db = new DatabaseHandler(getApplicationContext());
Displays ListView
displayListView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu_activity, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
take appropriate action for each action item clicked
switch(item.getItemId())
{
case R.id.action_add_new:
{
perform add new item action
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("Enter account details:");
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
Set an EditText view
final EditText input = new EditText(this);
input.setHint("Account Name");
layout.addView(input);
final EditText input2 = new EditText(this);
input2.setHint("Account Balance");
layout.addView(input2);
alert.setView(layout);
alert.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
if (input.getText().toString() != null)
{
Create empty Account
Account temp_account;
temp_account = new Account();
Save information to SQLiteDatabase
temp_account.setAccountName(input.getText().toString());
temp_account.setAccountBalance(Double.parseDouble(input2.getText().toString()));
Add temp account
db.addAccount(temp_account);
displayListView();
}
else
{
dialogInterface.cancel();
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
cancel
dialogInterface.cancel();
}
});
alert.show();
return true;
}
default:
{
return super.onOptionsItemSelected(item);
}
}
}
DISPLAY ACCOUNT LISTVIEW //
public void displayListView()
{
account_details = new ArrayList<Account>();
account_details = db.getAllAccounts();
accountList.setAdapter(new ListViewBaseAdapter(account_details,this));
}
}
ListView .xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<TextView
android:id="@+id/account_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textColor="#00628B"
android:textSize="22dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/account_balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#00628B"
android:textSize="22dp"/>
</RelativeLayout>
activity_main_menu_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E6E6DC">
<!---
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="0.2dp"
andrivider="#000"/>
-->
<ListView
android:id="@+id/accountListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="0.1dp"
android:divider="#81A594"/>
</LinearLayout>