Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6227349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:11:52+00:00 2026-05-24T09:11:52+00:00

I have an activity which extends ListActivity. It has many things but amongst them

  • 0

I have an activity which extends ListActivity. It has many things but amongst them it shows the articles the user has purchased with an adapter. Well I have a method that the user can delete the items from the list. The problem is when there is only one item. If I try to delete the last one the app crashes. Here is a it of my code:

public class Ventas extends ListActivity {

……

lv = getListView();

……

 protected void confirmRemoval(final int arg2) {
    // TODO Auto-generated method stub
    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle(getResources().getString(R.string.ventas));
    alertDialog.setMessage(getResources().getString(R.string.confirmacion2));
    alertDialog.setButton("Si",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    if(adapter2.mEvents.size()>=1){
                    adapter2.mEvents.remove(arg2);
                    } else {

                        //doesn't work
                        /*adapter2=null;
                        adapter2.notifyDataSetInvalidated();
                        lv.setVisibility(View.GONE);*/
                    }

                }
            });
    alertDialog.setButton2("No", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            dialog.dismiss();
        }
    });

    alertDialog.show();


}

here is the adapter and wrapper:

private class EventAdapter2 extends BaseAdapter {

    public ArrayList<Articulo> mEvents = null;

    public EventAdapter2(Context c, ArrayList<Articulo> clientes) {
        mContext = c;
        mEvents = clientes;
    }

    public int getCount() {
        return mEvents.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        EventEntryView2 btv;
        if (convertView == null) {
            btv = new EventEntryView2(mContext, mEvents.get(position));

        } else {
            btv = (EventEntryView2) convertView;
            String title1 = mEvents.get(position).getCantidad() + "";

            if (title1 != null) {
                btv.setText1Title(title1);
            }

            String title2 = mEvents.get(position).getDescripcion();

            if (title2 != null) {
                btv.setText2Title(title2);
            }

            String title3 = mEvents.get(position).getpVenta() + "0";

            if (title3 != null) {
                btv.setText3Title(title3);
            }

            String title4 = (mEvents.get(position).getCantidad() * mEvents
                    .get(position).getpVenta()) + "0";

            if (title4 != null) {
                btv.setText4Title(title4);
            }

        }

        return btv;

    }

    private Context mContext;

}

private class EventEntryView2 extends LinearLayout {

    private TextView text1;
    private TextView text2;
    private TextView text3;
    private TextView text4;
    private View inflatedView;

    public EventEntryView2(Context context, Articulo resp) {
        super(context);
        this.setOrientation(VERTICAL);

        inflatedView = View.inflate(context, R.layout.results, null);
        text1 = (TextView) inflatedView.findViewById(R.id.textView1);
        text2 = (TextView) inflatedView.findViewById(R.id.textView2);
        text3 = (TextView) inflatedView.findViewById(R.id.textView3);
        text4 = (TextView) inflatedView.findViewById(R.id.textView4);

        String t = resp.getCantidad() + "";
        text1.setText(t);

        String t1 = resp.getDescripcion();
        text2.setText(t1);

        String t2 = resp.getpVenta() + "0";
        text3.setText(t2);

        String t3 = (resp.getCantidad() * resp.getpVenta()) + "0";
        text4.setText(t3);

        addView(inflatedView, new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

    }

    public void setText4Title(String title4) {
        text4.setText(title4);
    }

    public void setText3Title(String title3) {
        text3.setText(title3);
    }

    public void setText2Title(String title2) {
        text2.setText(title2);
    }

    public void setText1Title(String title1) {
        text1.setText(title1);

    }
}

as you can see when I have only one item left I have tried to set adapter to null or adapter.notifyDataSetInvaliadted or even making the listview invisible, nothing works. What happens is when I click ok nothing changes then when I click a second time it all crashes

What I would like is the listView to disappear when the adapter is empty but I am now out of ideas, is it even possible?
Any ideas?

EDIT:
Thank you all for the answers but the problem was I was modifying the list from inside an inner anonymous class. It is actually pretty simple, create a method and call it from inside the dialog, once the array is empty the list disappears automatically:

 protected void removeFromList(int arg2) {  
    adapter2.mEvents.remove(arg2);
    adapter2.notifyDataSetChanged();
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:11:54+00:00Added an answer on May 24, 2026 at 9:11 am

    remove item from the arraylist which you add into the adapter and then call this method.

    youradapter.notifyDataSetChanged();
    

    and whatever you do for single item that was

    adapter2 = null;
    adapter2.notifyDataSetInavlidated();
    

    this will obviously crash it because adapter2 object was null so how null object notify its data

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a main class which extends Activity which should spawn a ListActivity and
My current application has one Activity, the main one which extends ListActivity (listview of
I have an activity which extends MapActivity. But when I tap the map, the
I have a couple of tables which are used to log user activity for
I have an activity which shows some List entries. When I click on a
I have an activity which extends PreferenceActivity. I'm loading preferences from the xml file.
I have an android activity in which I'm using tabs. public class UnitActivity extends
I have created two classes: Example1 and Example2 , which extends activity. Example1 contains
I have a TV Guide activity which extends TabActivity. The tabs are labelled (example)...
I have an activity which extends MapActivity. After running the activity, the Google Map

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.