Andrea Bandiera
Andrea Bandiera

Reputation: 109

Customizing Android ListView Items with Custom ArrayAdapter how remove item

with few examples i have build this application, I would like to remove the item when I click on the imagebutton( declared on the row item xml) I have tried to implement the callback invoked when the imagebuttom are pressed, but after how can delete/remove the item? How can do that? thx for all!

This is my activity:

public class MainActivity extends Activity {

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


        Ordine ordine_data[] = new Ordine[]
                {
                    new Ordine("Cloudy1"),
                    new Ordine("Cloudy2"),
                    new Ordine("Cloudy3"),
                    new Ordine("Cloudy4"),
                    new Ordine("Cloudy5")
                };

        final ArrayList<Ordine> list = new ArrayList<Ordine>();
        for (int i = 0; i < ordine_data.length; ++i) {
          list.add(ordine_data[i]);
        }
        final OrdineAdapter adapter = new OrdineAdapter(this,R.layout.order_list_row, ordine_data);
         ListView listview = (ListView) findViewById(R.id.listview);

        View header = (View)getLayoutInflater().inflate(R.layout.listview_header, null);
        listview.addHeaderView(header);        
        listview.setAdapter(adapter);


        //final StableArrayAdapter adapter = new StableArrayAdapter(this,android.R.layout.simple_list_item_1, list);
        //listview.setAdapter(adapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {

            final String item = (String) parent.getItemAtPosition(position);
            view.animate().setDuration(1000).alpha(0)
                .withEndAction(new Runnable() {
                  @Override
                  public void run() {
                    list.remove(item);
                    adapter.notifyDataSetChanged();
                    view.setAlpha(1);
                  }
                });
          }

        });
    }




    private class StableArrayAdapter extends ArrayAdapter<String> {

        HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

        public StableArrayAdapter(Context context, int textViewResourceId,
            List<String> objects) {
          super(context, textViewResourceId, objects);
          for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
          }
        }

        @Override
        public long getItemId(int position) {
          String item = getItem(position);
          return mIdMap.get(item);
        }

        @Override
        public boolean hasStableIds() {
          return true;
        }

      }

    @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;
    }

}

this is my adapter:

public class OrdineAdapter extends ArrayAdapter<Ordine>{

    Context context; 
    int layoutResourceId;    
    Ordine data[] = null;

    public OrdineAdapter(Context context, int layoutResourceId, Ordine[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        OrdineHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new OrdineHolder();

            holder.txtTitle = (TextView)row.findViewById(R.id.txtUserName);        
            row.setTag(holder);

            ImageButton btnRemove = (ImageButton)row.findViewById(R.id.remove_product);
            btnRemove.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    int i;
                    i=0; //this is for testing only!!!

                }

            });
        }
        else
        {
            holder = (OrdineHolder)row.getTag();
        }

        Ordine ordine = data[position];
        holder.txtTitle.setText(ordine.title);


        return row;
    }

    static class OrdineHolder
    {

        TextView txtTitle;
    }
}

Upvotes: 1

Views: 10839

Answers (1)

Emmanuel
Emmanuel

Reputation: 13223

You are passing ordine_data to your OridneAdapter; there is no connection between your list variable and ordine_data. You are adding your Ordines to your ArrayListusing ordine_data, but this doesn't mean that there is a link between the data you pass to the OridneAdapter and the list of Ordines. You need to use remove() in ViewAdapter(the parent variable in your case) to pass the Ordine to be removed , and then call notifyDataSetChanged(). You can try this inside run():

ArrayAdapter<Ordines> adapter = parent.getAdapter();
 adapter.remove(adapter.getItem(position));
adapter.notifyDataSetChanged();

Also, I think you need to call start() for your Animation to run.

Upvotes: 1

Related Questions