0

I've got a ListView which I made a custom adapter for it called Myadp:

public class Myadp extends ArrayAdapter {
    private final String[] web;
    private final String[] t;
    public Myadp(Context context, int resource, int textViewResourceId, final String[] objects, String[] total) {
        super(context, resource, textViewResourceId, objects);
        this.web = objects;
        this.t = total;
    }

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

        final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.list_temp, parent, false);

        TextView tv2 = (TextView) row.findViewById(R.id.textView2);
        TextView tv4 = (TextView) row.findViewById(R.id.textView4);


        tv2.setText(web[position]);
        tv4.setText(String.valueOf(t[position]));
        ImageButton del = (ImageButton) row.findViewById(R.id.imageButton);

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

        });


        return row;
    }
}

and when user click on del ImageButton that item will be deleted. I tried a lot, for example I tried to delete that item from the array(both web & t array) and re-call the ListView adapter but I wasn't successful, I've searched google and Stackoverflow but all of the codes are just for simple listview adapter. So now I need your helps.

A. Hajeb
  • 37
  • 1
  • 11
  • You're not actually doing anything in the `OnClickListener` of your button in your ListView, so yes, nothing will happen. Is this the code you are using ? It may make more sense to just extend BaseAdapter instead of ArrayAdapter if you are doing something like this. Also [see this](http://stackoverflow.com/questions/1709166/android-listview-elements-with-multiple-clickable-buttons), it is the same if you only have one button. – Jonas Czech Jan 24 '16 at 15:59
  • no I've just deleted the codes of del button – A. Hajeb Jan 24 '16 at 16:00
  • i wanted to show the general view form of my listview adapter – A. Hajeb Jan 24 '16 at 16:01
  • Ok, It would probably be a good idea to post the actual code you're using, otherwise we can't really figure out what's wrong in your specific case. Als check the link I posted for how to have a clickeable button in a ListView item. – Jonas Czech Jan 24 '16 at 16:04

2 Answers2

0

On click of delete button:

  1. Remove the element from the List or String[] object which you used to populate the ListView
  2. Then call notifyDataSetChanged()
Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
Arshad
  • 1,262
  • 16
  • 25
0

In your custom adapter call this.notifyDataSetChanged(); where you are performing delete functionality and deleting that element from arrayList which is set to that adapter. You have to remove that record from the ArrayList too that you have set to your adapter if you know which record it is you can use ArrayList.remove(index); and then use notifyDataSetChanged();

EDIT: Go to your adapter and add a method delete();

public void delete(int position){
data.remove(position);
notifyItemRemoved(position);
}

In your onClick(); method add this:

delete(getPosition());
Stanojkovic
  • 1,612
  • 1
  • 17
  • 24
  • As you see I use arrays not ArrayLists and deleting an element from array is different than ArrayList – A. Hajeb Jan 24 '16 at 16:26