如何获取删除按钮以删除listView中的项目?

时间:2014-07-31 00:57:27

标签: java android listview button

我是JAVA和android的初学者。我已设法在每个setOnItemClickListener项目上添加listView,如果单击某个项目(文本),则会从sqlite数据库中删除该项目。我稍后会更改此内容以转到新活动,因此我想在项目文本旁边的EACH ROW上按下deleteFavorite,如果单击该按钮,它将从sqlite数据库中删除该行。

如何删除按钮(deleteFavorite)以删除listView中的项目?

My Favorite.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class Favorite extends ActionBarActivity implements OnItemClickListener {

TextView favoriteTitle;

ListView list;
MySQLiteHelper db;
ArrayAdapter<DBLite> mArrayAdapter;
DBLite item;

Button deleteFavorite;

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {

    db = new MySQLiteHelper(this);

    item = db.getAllXXX().get(position);

    db.deleteXXX(item);

    mArrayAdapter = new ArrayAdapter<DBLite>(this,
            R.layout.list_view, R.id.txt, db.getAllXXX());
    list.setAdapter(mArrayAdapter);

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.favorite);

    favoriteTitle = (TextView) findViewById(R.id.favorite_title);

    db = new MySQLiteHelper(this);

    list = (ListView) findViewById(R.id.favorite_list);

    mArrayAdapter = new ArrayAdapter<DBLite>(this,
            R.layout.list_view, R.id.txt, db.getAllXXX());
    list.setAdapter(mArrayAdapter);
    list.setOnItemClickListener(this);

}

我最喜欢的文件夹

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/xxx"
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="com.xxx.xxx.Favorite">

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/text_background"
    android:padding="10sp"
    android:orientation="vertical">

    <ListView android:id="@+id/favorite_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#666666"
        android:dividerHeight="1sp"
        android:layout_below="@+id/favorite_title" />

   <TextView
        android:id="@+id/favorite_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/favorite_title"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        android:layout_marginBottom="10sp" />

</RelativeLayout>

</RelativeLayout>

我的list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical"
android:orientation="vertical" >

<Button
    android:id="@+id/delete_favorite_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/xxx"
    android:focusable="false"
    android:focusableInTouchMode="false"

    android:paddingTop="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp" />

<TextView
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:textColor="#FFFFFF" />
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

它似乎是你设置点击监听器而不是按钮... 你必须为列表适配器中的按钮设置点击监听器..

请参考这些链接..

https://looksok.wordpress.com/tag/listview-item-with-button/

ListView with Add and Delete Buttons in each Row in android

它将帮助您实现此删除功能