1

I´m a beginner in JAVA and android. I have manage to add a setOnItemClickListener on each listView item and if you click a item (text) it deletes the item from the sqlite database. I will later on change this to go to a new activity, so I would like my button deleteFavorite on EACH ROW beside the item text and if you click the button it will delete the row from sqlite database.

How do I get the delete button (deleteFavorite) to delete item in 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);

}

My favorite.xml

<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>

My 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>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
David
  • 247
  • 2
  • 3
  • 9
  • 1
    What exactly is the problem? You say, " if you click a item it deletes the item from the sqlite database". But then you say, "But I would like my button deleteFavorite on each row and if you click the button it will delete the row from sqlite database". So it does or doesn't delete it? – codeMagic Jul 31 '14 at 01:00
  • It deletes the item if you click on the item(the text) right now, but I do not want that. I would like that the button do this instead. Sorry if I´m not clear enough, kind of tired now. – David Jul 31 '14 at 01:07
  • No problem. just needed some clarification. – codeMagic Jul 31 '14 at 01:09
  • Oh, you have `android:focusable="false"` on the Button. What if you delete that? I didn't realize the Button was in each row. Also, you will want to use a Custom Adapter then set the `onClickListener` for the button in `getView()` – codeMagic Jul 31 '14 at 01:12
  • On each row there will be a button. All rows has the same button. I thought if it works now with the item (the text) click, I could use something similar to the button. Do you think that works or do I have to try something total different? – David Jul 31 '14 at 01:16
  • That will definitely work. Just read up on using a custom adapter class. It's actually really simple once you understand it. [Try Vogella's tutorials. They are pretty decent for beginneres](https://www.google.com/url?q=http://www.vogella.com/tutorials/AndroidListView/article.html&sa=U&ei=UZrZU-3-HpKSyATmq4GgAQ&ved=0CAUQFjAA&client=internal-uds-cse&usg=AFQjCNFiZ4f3IQhSHCkSs1KQmTnRG4frYw) – codeMagic Jul 31 '14 at 01:23

1 Answers1

0

it's seems you are setting click listener to the item not for the button... you have to set click listener for the button in list adapter..

please refer these links..

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

ListView with Add and Delete Buttons in each Row in android

it will hep you a lot for achieving this delete functionality

Community
  • 1
  • 1
Sam
  • 4,046
  • 8
  • 31
  • 47
  • My question have become "put on hold as unclear what you're asking by...", that is odd, because Sam understands it. Is he a super hero? Thanks Sam for your answer! – David Jul 31 '14 at 08:21