简体   繁体   English

如何在ListView中实现删除按钮并从数据库中删除?

[英]How can I implement a delete button in a ListView and delete from database?

I'm very new to android and I was given a prewritten app that I must improve. 我是android的新手,因此给了我一个必须改进的预编写应用程序。 One thing I have to do is add a delete button to each item in a ListView. 我要做的一件事是向ListView中的每个项目添加一个删除按钮。 Here is the XML for my ListView element: 这是我的ListView元素的XML:

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:descendantFocusability="blocksDescendants"
    android:orientation="horizontal" >

<ImageView
    android:id="@+id/li_map_image"
    android:layout_width="50dp"
    android:layout_height="match_parent"
    android:contentDescription="thumbnail" />

<TextView
    android:id="@+id/li_map_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:paddingLeft="8dp"
    android:textSize="16sp" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/delete"
    android:focusableInTouchMode="true"
    android:background="@drawable/red_x"
    android:layout_gravity="center|left"
    android:onClick="deleteMap"></ImageButton>

Basically, I want the user to click the delete icon if they want to delete a row in the ListView. 基本上,如果用户要删除ListView中的行,我希望用户单击“删除”图标。 Also, this should delete the item's data from the database. 同样,这应该从数据库中删除项目的数据。 I'm very confused about how to implement this because I don't know how I will know which delete button they are clicking. 我对如何实现此功能感到非常困惑,因为我不知道他们将单击哪个删除按钮。 Also, when I added the ImageButton to the ListView code, it tells me to make the onClick method in main (should it be in main?); 另外,当我将ImageButton添加到ListView代码时,它告诉我在main中创建onClick方法(应该在main中吗?); but how will I be able to delete data from the database? 但是我如何能够从数据库中删除数据? Also, Main Activity has a Fragment which obtains the ListView code. 另外,Main Activity具有一个获取ListView代码的Fragment。 This is the Fragment class: 这是Fragment类:

public class MapListFragment extends ListFragment implements
        LoaderManager.LoaderCallbacks<Cursor> {

    private static final int LOADER_ID = 1;
    private static final String[] FROM = { Database.Maps.DATA,
            Database.Maps.NAME };
    private static final String[] CURSOR_COLUMNS = { Database.Maps.ID,
            Database.Maps.DATA, Database.Maps.NAME };
    private static final int[] TO = { R.id.li_map_image, R.id.li_map_name };

    private SimpleCursorAdapter mAdapter;

    // FIXME isn't this unnecessary?
    public MapListFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // FIXME reverse the order so the newest sessions are at the top
        mAdapter = new SimpleCursorAdapter(getActivity(),
                R.layout.map_list_item, null, FROM, TO, 0);
        mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, Cursor cursor,
                    int columnIndex) {
                if (view.getId() == R.id.li_map_image) {
                    ((ImageView) view).setImageURI(Uri.parse(cursor
                            .getString(columnIndex)));
                    return true;
                }
                return false;
            }
        });
        setListAdapter(mAdapter);

        getLoaderManager().initLoader(LOADER_ID, null, this);
    }

    @Override
    public void onListItemClick(ListView list, View v, int position, long id) {
        final Intent nextIntent = new Intent(getActivity(),
                ViewMapActivity.class);
        nextIntent.putExtra(Utils.Constants.MAP_ID_EXTRA, id);
        startActivity(nextIntent);
    }


    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CursorLoader(getActivity(), DataProvider.MAPS_URI,
                CURSOR_COLUMNS, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        if (loader.getId() == LOADER_ID)
            mAdapter.swapCursor(cursor);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }

}

I'm very lost as how to implement this delete feature. 我对如何实现此删除功能非常迷失。 Any help will be much appreciated :) 任何帮助都感激不尽 :)

here is a very good tutorial on how to put a clicklistener on a button inside listview. 这是一个很好的教程,介绍了如何在列表视图内的按钮上放置clicklistener。

follow this link 跟随此链接

inside your adapter getView method, you need to put click listener on button like this 在适配器的getView方法中,您需要将Click Listener放在这样的按钮上

@Override  
public View getView(final int position, View convertView, ViewGroup parent) {  
    ViewHolder viewHolder;  
    if (convertView == null) {  
        LayoutInflater inflater = LayoutInflater.from(context);  
        convertView = inflater.inflate(R.layout.child_listview, null);  
        viewHolder = new ViewHolder();  
        viewHolder.text = (TextView) convertView  
                .findViewById(R.id.childTextView);  
        viewHolder.button = (Button) convertView  
                .findViewById(R.id.childButton);  
        convertView.setTag(viewHolder);  
    } else {  
        viewHolder = (ViewHolder) convertView.getTag();  
    }  
    final String temp = getItem(position);  
    viewHolder.text.setText(temp);  
    viewHolder.button.setOnClickListener(new OnClickListener() {  

        @Override  
        public void onClick(View v) {  
            if (customListner != null) {  
                customListner.onButtonClickListner(position,temp);  
            }  

        }  
    });  

    return convertView;  
}  

Add Longclicklistner in Your Listview 在您的列表视图中添加Longclicklistner

try this , it may help you 试试这个,可能对您有帮助

Link 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击删除按钮从数据库和列表视图中删除项目 - Delete item from both, database and listview on clicking Delete Button Android:如何从保存在SQLite数据库中的列表视图中删除项目? - Android: How can I delete an item from a listview that is saved in a SQLite database? 如何在Android中的按钮单击事件中从数据库中删除文本框中的选定数据? - How can i delete selected data in TextBox from Database on the button click event in Android? 如何永久删除列表视图项? - How can I delete a listview item permanently? 如何从jsp页面删除数据库记录? - How can I delete database records from a jsp page? 如何将删除按钮 (x) 附加到动态创建的 ListView 的每个成员? - How can I attach a delete button (x) to each member of a dynamically created ListView? 如何长按Android应用程序删除ListView和服务器端数据库中的内容(复选框)? - How can I delete content (CheckBox) in a ListView and on a serverside database with long press for android application? 我可以删除listView数组但不能更新listview吗? - can i delete array of listView but not update listview? 如何从数据库以及Android的ListView中删除项目? - How to delete an item from both database as well as listview Android? 在用户输入后,如何从ListView添加/删除项目? - How can I add/delete items from a ListView upon input from user?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM