custom imageadapter to add and remove images with text
<p>How can I add and remove images from a image adapter? I have a custom image adapter to which I would like to add and remove. I am basically trying to add an image with some text and remove it and update the adapter itself. I just need to know how I can add, remove and update the imageAdapter. I have a picture of folder as drawable and I want to add that picture with a different names and remove the picture from the grid as desired. I tried but failed implementing this below is my implementation. The adapter overwrites current images with the text just added to the list.</p>

<pre><code>import java.util.ArrayList    
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AlbumAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList albNames;

    public AlbumAdapter(Context c, ArrayList albNames) {

        mContext = c;
        this.albNames = albNames;
    }

    public int getCount() {

        return albNames.size();
    }

    public Object getItem(int position) {

        return albNames.get(position);
    }

    public long getItemId(int position) {

        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
/*  public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView;

        if (convertView == null) { // if it's not recycled, initialize some attributes

            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(10, 10, 10, 3);
        } 

        else {

            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    */

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

        View view;

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.album_image, null);
        } 

        else {

            view = convertView;
        }

        ImageView imgView = (ImageView) view.findViewById(R.id.icon_image);
        imgView.setLayoutParams(new LinearLayout.LayoutParams(50, 50));
        imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imgView.setPadding(10, 10, 10, 3);
        TextView albNameView = (TextView) view.findViewById(R.id.icon_text);


        if (albNames.size() &gt; 0) {          

            for (int i = 0; i &lt; albNames.size(); i++) {

                imgView.setBackgroundResource(R.drawable.folder);           
                albNameView.setText((String)albNames.get(i));

            }           
        }       

        return view;
    }
}
</code></pre>

<p>Main Acitivity</p>

<pre><code>import java.util.ArrayList;

import cs213.photoAlbum.control.*;
import cs213.photoAlbum.model.userNode;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;


public class MainActivity extends Activity {

    private userNode user = new userNode("user1", "", null, null);
    private UserControl userFunc = new UserControl();
    private ArrayList albNames = new ArrayList();
    private AlbumAdapter albAdapter = new AlbumAdapter(this, albNames);

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

        getWindow().getDecorView().setBackgroundColor(Color.WHITE);

        GridView gridview = (GridView) findViewById(R.id.gridview);     
        gridview.setAdapter(albAdapter);

        gridview.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long id) {

                Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();

            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection

        switch (item.getItemId()) {

        case R.id.action_addAlb:

            final AlertDialog addAlbdialog = new AlertDialog.Builder(this).create(); 
            addAlbdialog.setTitle("Add Album");

            View addAlb_layout = getLayoutInflater().inflate(R.layout.addalb_dialog, null);

            // Create the text field in the alert dialog...
            final EditText dFieldA = (EditText) addAlb_layout.findViewById(R.id.dField1);  
            final Button actionBtnA = (Button) addAlb_layout.findViewById(R.id.button1);  
            final Button resetBtnA = (Button) addAlb_layout.findViewById(R.id.button2);  

            addAlbdialog.setView(addAlb_layout);
            addAlbdialog.show();            

            View.OnClickListener addAlbClick = new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    if (actionBtnA.getId() == ((Button)v).getId()) {

                        String albName = dFieldA.getText().toString().trim();
                        System.out.println(albName);

                        if (userFunc.addAlbum(user, albName)) {

                            Toast.makeText(MainActivity.this, "Created album with name: " + albName, Toast.LENGTH_SHORT).show();
                            albNames.add(albName);

                            System.out.println("list" + albNames);

                            albAdapter.notifyDataSetChanged();

                        }

                        else {

                            Toast.makeText(MainActivity.this, "Album already extis with name: " + albName, Toast.LENGTH_SHORT).show();

                        }

                    }

                    else if (resetBtnA.getId() == ((Button)v).getId()) {

                        dFieldA.setText("");

                    }                   
                }

            };

            actionBtnA.setOnClickListener(addAlbClick);
            resetBtnA.setOnClickListener(addAlbClick);

            return true;

        case R.id.action_deleteAlb:

            final AlertDialog delAlbdialog = new AlertDialog.Builder(this).create(); 
            delAlbdialog.setTitle("Delete Album");

            View delAlb_layout = getLayoutInflater().inflate(R.layout.deletealb_dialog, null);

            delAlbdialog.setView(delAlb_layout);
            delAlbdialog.show();     

            final EditText dFieldD = (EditText) delAlb_layout.findViewById(R.id.dField1);  
            final Button actionBtnD = (Button) delAlb_layout.findViewById(R.id.button1);  
            final Button resetBtnD = (Button) delAlb_layout.findViewById(R.id.button2);

            View.OnClickListener delAlbClick = new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    if (actionBtnD.getId() == ((Button)v).getId()) {

                        String albName = dFieldD.getText().toString().trim();
                        System.out.println("delete " + albName);

                        delAlbdialog.dismiss();

                    }

                    else if (resetBtnD.getId() == ((Button)v).getId()) {

                        dFieldD.setText("");

                    }                   
                }

            };

            actionBtnD.setOnClickListener(delAlbClick);
            resetBtnD.setOnClickListener(delAlbClick);          

            return true;

        case R.id.action_renameAlb:

            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }
}
</code></pre>
