0

I have made a custom View which contains an ImageView, a TextView and a delete Button.
I want to reset the particular View when I click the delete Button associated with it.
Please tell how to implement this.

Here is my MainActivity.java

package com.khurana.nikhil.list1;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
ListView lv;
TextView tv1, tv2;
View v1;
public String[] s = {"nikhil", "mukul", "kunal"};
public int[] img = {R.drawable.rty, R.drawable.sf, R.drawable.rty};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView lv = (ListView) findViewById(R.id.listView);


    CustomAdapter cad = new CustomAdapter(MainActivity.this, s, img);
    lv.setAdapter(cad);


    lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {

        public void onItemClick(AdapterView<?> parent,View v,int position,long id)
        {
            Toast.makeText(MainActivity.this,"You clicked "+s[position],Toast.LENGTH_SHORT).show();

        }

    });


}
} 

Here is CustomAdapter.java

package com.khurana.nikhil.list1;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

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);
    ImageView im=(ImageView)v.findViewById(R.id.imageview);
    tv.setText(s1[position]);
    im.setImageResource(s2[position]);
    Button bt=(Button)v.findViewById(R.id.button);
    return v;
}
}
Nikhil Khurana
  • 422
  • 1
  • 5
  • 12

5 Answers5

2
((LinearLayout)yourView.getParent()).removeView(yourView);

or you can call from the layout directly

yourRelativeLayout.removeView(yourView)
Jason Portnoy
  • 757
  • 8
  • 23
1

I would recommend to use an ArrayList instead of a String[] in your Adapter class. It makes it a lot easier to delete or edit a View and the associated data behind it. I used this post to convert your String[] to an ArrayList, delete the item and convert back to String[].

This should work on button click:

In your Adapter class:

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);
    ImageView im=(ImageView)v.findViewById(R.id.imageview);
    tv.setText(s1[position]);
    im.setImageResource(s2[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 = v.getTag(); //get the position of the view to delete stored in the tag
        removeItem(positionToRemove); //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(i);
  strings = new String[a.size()];
  s1= a.toArray(strings);
  notifyDataSetChanged(); //refresh your listview based on new data
}

 @Override
 public int getCount() {
   return s1.length;
 }

 @Override
 public String getItem(int position) {
   return s1[position];
 }
Community
  • 1
  • 1
Frank D.
  • 1,306
  • 1
  • 13
  • 23
0

View visibility to GONE will not delete your view from memory as well. Please be specific, do you want to retain your view for future?

Ashutoshg
  • 157
  • 1
  • 7
0

If you are working in a listview you should call notifyDataSetChanged() the adapter. your getView() method can be like: action on button can be like this

Button bt=(Button)v.findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

              s1 = ArrayUtils.remove(s1, position);
              s2 = ArrayUtils.remove(s2, position);
              notifyDataSetChanged();
            }
        });
Sajidkhan
  • 608
  • 7
  • 16
-2

If you want the view to disappear:

yourView.setVisibility(View.GONE);

EDIT: To get the parent view of the button: Android: Making changes to a button's parent view

Community
  • 1
  • 1
MD1948
  • 292
  • 2
  • 13