Rafie Gilang
Rafie Gilang

Reputation: 31

Add delete button in custom Listview

First of all, pardon the broken english.

I want to know how to delete an item when the data is from other class using a delete button inside the listview.

I use a button in MainActivity that will insert some data to array called nicknames.

This is my CustomAdapter class.

public class CustomAdapter extends ArrayAdapter<String>{

public CustomAdapter(Context context, String[] name) {
    super(context,R.layout.custom_row ,name);

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(getContext());
    final View customView = inflater.inflate(R.layout.custom_row, parent, false);


    final String singleName = getItem(position);
    TextView nickname = (TextView) customView.findViewById(R.id.nickname);

    nickname.setText(singleName);

    return customView;
}}

And this is my onClick in MainActivity class

  public void resultNickname(View view){
    nicknames[] = dbHandler.retrieveName();
    setContentView(R.layout.nickname_list);
    ListAdapter listAdapter = new CustomAdapter(this,nicknames);
    ListView nameListView = (ListView) findViewById(R.id.nickList);
    nameListView.setAdapter(listAdapter);


}

I think my problem can be resolved based on this question answer. android: how to delete a row from a ListView with a delete button in the row But i cant make it work because i didnt understand what is the item mean and how to implement it in my code.

Can someone help me at least explaining the items mean from the other question and how to implement it in my code ?

Upvotes: 0

Views: 2945

Answers (2)

Mrugesh
Mrugesh

Reputation: 4517

 public class CustomAdapter extends ArrayAdapter<String>
 {
 Context c1;
 String s1[];
 int s2[];
 CustomAdapter(Context c,String s[],int s3[])
 {
 super(c,R.layout.tcustom,s);
  this.c1=c;
  this.s1=s;
  this.s2=s3;
  }

  public View getView(int position,View v,ViewGroup parent)
  {
      LayoutInflater li=(LayoutInflater) c1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


v=li.inflate(R.layout.tcustom,null);
TextView tv=(TextView)v.findViewById(R.id.textView);
tv.setText(s1[position]);

Button bt = (Button) v.findViewById(R.id.button);
bt.setTag(position); //important so we know which item to delete on button click

bt.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v)
    {
        int positionToRemove = (int)v.getTag(); //get the position of the view to delete stored in the tag
        removeItem(positionToRemove);
    notifyDataSetChanged(); //remove the item
    }
});

return v;
}

 public void removeItem(int position){
 //convert array to ArrayList, delete item and convert back to array
 ArrayList<String> a = new ArrayList<>(Arrays.asList(s1));
 a.remove(position);
 String[] s = new String[a.size()];
 s=a.toArray(s);
  s1 = s;
  notifyDataSetChanged(); //refresh your listview based on new data

 }
 public int getCount() {
 return s1.length;
 }
 public String getItem(int position) {
 return s1[position];
 }}

Upvotes: 2

Riki Rikmen
Riki Rikmen

Reputation: 35

Trying to answer, first of all i think you should define onclick with your delete button, and then you remove your list/array with the position of row.

Upvotes: 0

Related Questions