1

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>

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

}

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

1
  • set onClickListener inside of your adapter's getView method on delete button. Commented Jun 10, 2015 at 4:11

2 Answers 2

2

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

follow this link

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;  
}  
Sign up to request clarification or add additional context in comments.

2 Comments

This confuses me because I don't see at getView method in the code i was given and it doesn't work when I try to add one.
U have the base adapter class for your listview...? if yes, add the setOnClickListener code in that class.
0

Add Longclicklistner in Your Listview

try this , it may help you

Link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.