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

  • Home
  • SEARCH
  • 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 7515595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:51:26+00:00 2026-05-30T00:51:26+00:00

I am making an android application that needs to add items to a dynamic

  • 0

I am making an android application that needs to add items to a dynamic ListView. How can I do this? I wish to add the item once the "AddItemToListViewButton" is pressed. I am trying to use the code below, but however, it’s not working. How can I fix/make this happen? My code:

public class NotesActivity extends ListActivity implements OnClickListener {

    List<String> myList = new ArrayList<String>();
    EditText AddItemToListViewEditText;
    Button AddItemToListView, AddItemToListViewButton;
    LinearLayout AddItemToListViewLinearLayout;
    static final String[] COUNTRIES = new String[] {
      "Matte på A1 med Ole", "Engelsk på klasserommet", "Film på A1 etter friminuttet"
      };

   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notes);
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
        // When clicked, show a toast with the TextView text
        Toast.makeText(getApplicationContext(), "Note: " + ((TextView) view).getText(),
            Toast.LENGTH_SHORT).show();
  }
    });
  }
    @Override
   public boolean onCreateOptionsMenu(Menu meny) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.listviewmenubuttons, meny);
      return true;
   }
    @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      switch(item.getItemId()) {
      case R.id.AddItemToListView:
        AddItemToListViewEditText = (EditText)findViewById(R.id.AddItemToListViewEditText);
        AddItemToListViewButton = (Button)findViewById(R.id.AddItemToListViewButton);
        AddItemToListViewLinearLayout = (LinearLayout)findViewById(R.id.AddItemToListViewLinearLayout);
        AddItemToListViewButton.setOnClickListener(this);
        AddItemToListViewLinearLayout.setVisibility(View.VISIBLE);
        break;
        }
    return true;
}
   public void onClick(View src) {
      switch(src.getId()) {
      case R.id.AddItemToListViewButton:
        myList.add("Hello");
        myList.add("world");
        System.out.println("The list contains " +
           myList.size() + "elements, and " +
           "the first is " + myList.get(0));
        AddItemToListViewLinearLayout.setVisibility(View.GONE);
        break;
    }
}
}
  • 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-30T00:51:28+00:00Added an answer on May 30, 2026 at 12:51 am

    Your ListActivity has a ListView element that is based on an ArrayAdapter<String> containing a string array named COUNTRIES. In your onClick() method you add strings to an ArrayList<String>(myList) but your ListView adapter doesn’t know about it and that you try to add it to your list(well this is what i think you are trying to do). So :

    -in your myList add the current content of your COUNTRIES string array:

    myList.add("Matte på A1 med Ole");//and the other strings from COUNTRIES
    

    -set the adapter to point to the myList:

    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, myList));
    

    -in your onClick() method write:

    public void onClick(View src) {
        switch(src.getId()) {
        case R.id.AddItemToListViewButton:
            myList.add("Hello");
            myList.add("world");
            ((ArrayAdapter)getListView().getAdapter()).notifyDataSetChanged(); 
            AddItemToListViewLinearLayout.setVisibility(View.GONE);
            break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making an android application that needs to display all of the items
I am making an android application that needs to use a ListView . I
So I'm making this android application, that needs to read data from user-provided CSV
I am making an android application that will play an mp3 file once a
I am making an android application that uses a listview. I want to get
I'm making an application for Android that needs to react to when the device
i am making one application in Android 2.2 in that application i want to
I am making a email sending application in android phone. For that I included
I am making an application in Android. This is a small application of an
I am making an android application that shows a list with users, I would

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.