0

I'm writing a simple android app that has a dynamic ListView associated with an ArrayList. The user can type in a text box, press a button, and the text will be added to the ListView via the ArrayList. The text box where the user can type is on a separate activity, so whenever the user presses "submit" in order to add the item to the ListView, the main activity is called and the text is passed via an intent. I need to check to see if the text was empty, however, because otherwise the way my code is setup an empty entry to the ListView will be added. This is what I currently have:

    protected void onResume() {
    super.onResume();
    ArrayAdapter<Item> itemsAdapter = new itemsListAdapter(this, R.layout.adapter_view_layout, itemsArrayList);
    ListView itemsListView = findViewById(R.id.mainActivity_itemsListView1);
    itemsListView.setAdapter(itemsAdapter);
    String incomingItemName = getIntent().getStringExtra("ITEM_NAME");
    if (!incomingItemName.equals("")) { itemsArrayList.add(new Item(incomingItemName)); } //this line causes app to crash on start
    }

The if statement I have written causes the app to crash on startup, if I comment that line out the app starts fine. If I move itemsArrayList.add(new Item(incomingItemName)); out of an if statement, a blank list entry is created when the user goes into the activity to add an item then simply backs out back into the main screen.

I am relatively new to android programming, so any help would be much appreciated. Thank you.

1 Answers1

1

You can use Textutils.isEmpty() to check your string from intent. It will return true if your input string is null or empty.

karan
  • 8,637
  • 3
  • 41
  • 78
  • Thank you. That worked beautifully, I will accept this as the answer as soon as it will let me. –  Oct 19 '18 at 07:40