我如何能实现在ListView删除按钮,并从数据库中删除? [英] How can I implement a delete button in a ListView and delete from database?

查看:215
本文介绍了我如何能实现在ListView删除按钮,并从数据库中删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是很新的Andr​​oid和我得到了prewritten应用程序,我必须改进。有一件事我要做的就是在ListView添加一个删除按钮的每个项目。
这里是我的ListView元素的XML:

I'm very new to android and I was given a prewritten app that I must improve. One thing I have to do is add a delete button to each item in a ListView. Here is the XML for my ListView element:

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>

基本上,我希望用户点击删除图标,如果他们想在ListView删除行。此外,这应该从数据库中删除的项目的数据。我对如何实现这个非常困惑,因为我不知道我怎么会知道删除按钮它们被点击。此外,当我加入的ImageButton到ListView code,它告诉我,使主onClick的方法(它应该是主?);但我怎么就能从数据库中删除数据?此外,主要活动有其获得的ListView code片段。这是片段类:

Basically, I want the user to click the delete icon if they want to delete a row in the 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?); but how will I be able to delete data from the database? Also, Main Activity has a Fragment which obtains the ListView code. This is the Fragment class:

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);
    }

}

我很失落,如何实现这个删除功能。任何帮助将大大AP preciated:)

I'm very lost as how to implement this delete feature. Any help will be much appreciated :)

推荐答案

下面是关于如何把一个clicklistener上的一个按钮列表视图里面一个非常好的教程。

here is a very good tutorial on how to put a clicklistener on a button inside listview.

按照<一href=\"http://www.c-sharpcorner.com/UploadFile/9e8439/create-custom-listener-on-button-in-listitem-listview-in-a/\"相对=nofollow>此链接

您的适配器getView方法中,你需要把点击这样的按钮侦听

inside your adapter getView method, you need to put click listener on button like this

@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;  
}  

这篇关于我如何能实现在ListView删除按钮,并从数据库中删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆