我想弄清楚当我从左向右滑动时如何从列表视图中删除项目。我将在下面附上我的代码。截至目前,我正在使用SQLite DB并创建了我自己的自定义列表视图适配器。我正在创建一个简单的列表来删除和添加项目。我有添加项目,现在我只需要弄清楚如何从列表视图中从左到右滑动删除项目。我的想法是当你从左向右滑动时,会出现一个显示删除的按钮。并且用户可以单击它以从列表中删除项目。
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>