Bryant
Bryant

Reputation: 59

How to remove a custom view from list view using a button in the custom view

I created a custom view with a edit text field and a button. I want to delete an item from listview by clicking on a button. The code for the custom view:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete"
        android:id="@+id/delete_btn"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/player_name_txt"
        android:textColor="#ffffff"
        android:editable="false"
        android:background="#5eb8ed"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignBottom="@+id/delete_btn"
        android:layout_toLeftOf="@+id/delete_btn"
        android:layout_toStartOf="@+id/delete_btn" />
    </RelativeLayout>

The code for the adapter and list view:

inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    playerListView = (ListView)this.findViewById(android.R.id.content).getRootView().findViewById(R.id.player_list);
    playerListAdapter = new ArrayAdapter(this,R.layout.player_item,R.id.player_name_txt, new ArrayList<String>(){});
playerListView.setAdapter(playerListAdapter);

The Code I tried:

 public void removePlayer(View v)
    {
        EditText pairedEdit = (EditText)findViewById(R.id.player_name_txt);
        String name = pairedEdit.getText().toString();
        playerListAdapter.remove(name);
        playerListAdapter.notifyDataSetChanged();
    }

Right now it removes the first item in the list view.

AlertDialog.Builder addPlayerBuilder = new AlertDialog.Builder(this);
        final View customView = inflater.inflate(R.layout.add_player,null);
        final EditText usernameEdit = (EditText)customView.findViewById(R.id.username_edit);
        addPlayerBuilder.setView(customView);
        addPlayerBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {

                String name = usernameEdit.getText().toString();
                playerListAdapter.add(name);
                playerListAdapter.notifyDataSetChanged();

                dialog.dismiss();

            }
        });
        addPlayerBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });

        AlertDialog dialog = addPlayerBuilder.create();
        dialog.show();

Upvotes: 1

Views: 1171

Answers (3)

Bhumit
Bhumit

Reputation: 338

In adapter class on delete_btn click you have to call remove method :

 public class yourAdapter extends BaseAdapter {

    private static LayoutInflater inflater = null;
    private Activity context;
    private ArrayList<yourBean> arrList;

    public yourAdapter(Activity context,
            ArrayList<yourBean> arrList) {
        this.context = context;
        this.arrList = arrList;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return arrList.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {



        if (convertView == null) {

            convertView = inflater.inflate(R.layout.list_row,
                    null);


            viewHolder.delete_btn= (Button) convertView
                    .findViewById(R.id.delete_btn);


            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.delete_btn.setTag(position);


        viewHolder.delete_btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

                    arrayList.remove(position);
                    notifyDataSetChanged();

        }
    });

        return convertView;
    }

    static class ViewHolder {
        Button delete_btn;
    }

}

Upvotes: 0

user5713256
user5713256

Reputation:

Use this:

Button b2 = (Button) row.findViewById(R.id.button1);
        b2.setTag(arg0); 
        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                int pos = (int)arg0.getTag();
                  lista.remove(pos);
                  SunetePreferateAdaptor.this.notifyDataSetChanged();            }
        });

Upvotes: 0

abbas piplodawala
abbas piplodawala

Reputation: 302

In your button of custom layout place a onclicklistener which would remove the element from the arraylist by remove() function on the basis of its position and then call notifyDataSetInvalidated()

Upvotes: 1

Related Questions