title: How do I properly remove an item from ListView in Android?
body: <p>I am having problem while removing item from ListView,</p>
  
  <p>This is my Activity :</p>
  
  <pre><code>public class TestListItemsView1Activity extends ListActivity {
      /** Called when the activity is first created. */
  
      private EfficientAdapter efficientAdapter;
      private String[] data = new String[] { "BBC", "Yahoo", "CNN", "Eenadu", "Hindu" };
      private List&lt;String&gt; dataValues = Arrays.asList(data);
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          requestWindowFeature(Window.FEATURE_NO_TITLE);
          setContentView(R.layout.main);
          efficientAdapter = new EfficientAdapter(this,dataValues);
  
          setListAdapter(efficientAdapter);
      }
  
      @Override
      protected void onListItemClick(ListView l, View v, int position, long id) {
      // TODO Auto-generated method stub
      super.onListItemClick(l, v, position, id);
          //dataValues.remove(position);
          //efficientAdapter.notifyDataSetChanged();
          Toast.makeText(this, "Click-" + String.valueOf(position), Toast.LENGTH_SHORT).show();
      }
  }
  </code></pre>
  
  <p>and the Adapter class:</p>
  
  <pre><code>public class EfficientAdapter extends BaseAdapter implements Filterable{
  
      private LayoutInflater mInflater;
      private static String TAG=EfficientAdapter.class.getSimpleName();
      private Context context;
  
      private HashMap&lt;String,EfficientAdapter.ViewHolder&gt; holders= new HashMap&lt;String, EfficientAdapter.ViewHolder&gt;();
      //private String[] dataArray = new String[] { "BBC", "Yahoo", "CNN", "Eenadu", "Hindu" };
      private List&lt;String&gt; data= new ArrayList&lt;String&gt;();//Arrays.asList(dataArray);
  
      public EfficientAdapter(Context context,List&lt;String&gt; data) {
          //super(context, R.layout.main, values);
          mInflater = LayoutInflater.from(context);
          this.context=context;
          this.data.addAll(data);
      }
  
      public EfficientAdapter(Context context) {
          //super(context, R.layout.main, values);
          mInflater = LayoutInflater.from(context);
          this.context=context;
          //data.addAll(data);
      }
      public View getView(final int position, View convertView, ViewGroup parent) {
          ViewHolder holder;
  
          if(convertView == null){
              convertView = mInflater.inflate(R.layout.adaptor_content, null);
  
              holder = new ViewHolder();
              holder.textLine = (TextView) convertView.findViewById(R.id.textLine);
              holder.buttonLine =(Button) convertView.findViewById(R.id.buttonLine);
  
              holder.textLine.setText(this.data.get(position));
              convertView.setOnClickListener(new OnClickListener() {
                  private int pos= position;
                  public void onClick(View v) {
                      Toast.makeText(context, "Click-" + String.valueOf(pos), Toast.LENGTH_SHORT).show(); 
  
                  }
              });
  
              holder.buttonLine.setOnClickListener(new OnClickListener() {
                  private int pos= position;
  
                  public void onClick(View v) {
                      /*ViewHolder deleteHolder = (ViewHolder)v.getTag();
                      int delPosition=deleteHolder.position;
                      holders.remove(delPosition);
                      ViewHolder currentHolder;
                      for(int i=pos+1;i&lt;getCount();i++){
                          currentHolder = holders.get(delPosition);
                          currentHolder.position=i-1;
                      }
                      data.remove(delPosition);
                      notifyDataSetChanged();*/
                      //View convertView1 = mInflater.inflate(R.layout.adaptor_content, null);
  //                  ViewHolder deleteHolder = (ViewHolder)v.getTag();
  
                      data.remove(getItem(pos));
                      notifyDataSetChanged();
  
  
                      //final ArrayAdapter adapter = ((ArrayAdapter)getListAdapter());
  
  
                      //ListView lv = v.findViewById(R.layout.)
  
  
                      //v.invalidate();
  
                      Log.i(TAG, "Delete button pressed at position" + pos);
                      Toast.makeText(context, "Delete-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();
  
                  }
              });
              //holder.position=position;
              //holders.put(Integer.toString(position), holder);
              convertView.setTag(holder);
  
          } else {
              holder =(ViewHolder) convertView.getTag();
          }
  
          return convertView;
      }
  
  
      public int getCount() {
          // TODO Auto-generated method stub
          return data.size();
      }
  
      public Object getItem(int position) {
          // TODO Auto-generated method stub
          return data.get(position);
      }
  
      public long getItemId(int position) {
          // TODO Auto-generated method stub
          return 0;
      }
  
  
      public Filter getFilter() {
          // TODO Auto-generated method stub
          return null;
      }
  
  
      static class ViewHolder {
          TextView textLine;
          ImageView iconLine;
          Button buttonLine;
          //int position;
          }
  
  }
  </code></pre>
  
  <p>Every time its removing last item not the one which clicked.</p>