简体   繁体   中英

Delete a listview item with a button in it self in android studio

As I am very new to java pls help me on this. I have a custom list view in my main activity and a Custom adapter with it. In my every list item there is a delete button that should delete that item when it clicked. I can not remove data from my arraylist when i am inside my custom adapter. Pls helm me in coding this delete button.

MainActivity.java


public class MainActivity extends AppCompatActivity {

    EditText getItem;
    Button AddButton;
    Button DellButton;

    public static ArrayList<String> myData = new ArrayList<String>();

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

        ListView list = (ListView)
findViewById(R.id.listView);
        getItem = (EditText) findViewById(R.id.newItem);
        AddButton = (Button) findViewById(R.id.AddButton);
       
        MyAdapter adapter = new MyAdapter(this, myData);

        list.setAdapter(adapter);

        AddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String result = getItem.getText().toString();
                myData.add(result);
                adapter.notifyDataSetChanged();
            }
        });       
    }

MyAdapter.java

public class MyAdapter extends ArrayAdapter<String> {

       public MyAdapter(Context context, ArrayList<String> records) {

        super(context, 0, records);
    }

    @Override

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

        String item = getItem(position);

        if (convertView == null) {

            convertView = LayoutInflater.from(getContext()).inflate(R.layout.listview_custom, parent, false);
        }

        final TextView lst_txt = (TextView) convertView.findViewById(R.id.list_Txt2);

        Button plusbut = (Button) convertView.findViewById(R.id.plusbut);

        Button minusbut = (Button) convertView.findViewById(R.id.minusbut);

        final TextView sum = (TextView) convertView.findViewById(R.id.sum);

        Button cal = (Button) convertView.findViewById(R.id.calButton);

        Button delete = (Button) convertView.findViewById(R.id.btnDel);

        lst_txt.setText(item);

        minusbut.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                int sumAll = Integer.parseInt(sum.getText().toString());
                int sum1 = sumAll - 1;

                sum.setText(String.valueOf(sum1));

            }
        });

        plusbut.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                int sumAll = Integer.parseInt(sum.getText().toString());
                int sum1 = sumAll + 1;
                sum.setText(String.valueOf(sum1));

           }
        });

        cal.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                String s = sum.getText().toString();

                Intent intent = new Intent(getContext(), calll.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                intent.putExtra("sumFL", s);
                getContext().startActivity(intent);

            }
        });             
        return convertView;
    }
}

Please first try to remove object from list by using the position of item with check the validation with list size and then call notifyItemadapter to update the list view.

Use ViewHolder class for all view like textview, button etc. And initialize them inside the condition

if(convert view==null){
     Initialize holder object here and
     Inflate your layout and
    
     Initialize button like
     holder.deletebutton = convert view.findviewbyid from xml
     
     
     settag(holder)
} 
Again get the holdet using the gettag in 
else{
   //Here
}

Put All click event and text update etc. Outside of above condition

holder.deletbutton.setonclicklistener{
    
     int pos = view.getag
     list.remove(pos)
     Notifyadapter here
 
}

holder.deletebutton.settag(position)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM