0

I am trying to figure out how to add another string to my month array and display that in a list view dynamically via pressing the button. Currently I have no problem display the array I have specified, but I cant figure out a way to add my count variable which should be added to the array list after the button is clicked. Any ideas ? Thank's for the help!!!!

public class Test extends Activity implements OnClickListener,
        OnItemClickListener {

    Button test;
        ListView list;
    String month[] = { "January", "February", "March", "April", "May", "June",
            "July", "August", "September", "October", "November", "December" };

    public static int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        test = (Button) findViewById(R.id.button1);
        test.setOnClickListener(this);
        list = (ListView) findViewById(R.id.tlist);
        list.setAdapter(new  ArrayAdapter<String> (this,
                android.R.layout.simple_list_item_1, month));
    }


    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.button1:
            count++;

            //Add count value to end of array
            break;
        }
    }

}

1 Answer 1

2

when u click on the button, update the contents of your array { in your case month[], say add month to it } then call "ListAdapter.this.notifyDataSetChanged();" whenre ListAdapter is your custom adapter. Basically notifyDataSetChanged will call getview of your adapter and it will update the list

Sign up to request clarification or add additional context in comments.

1 Comment

myArr.add("" + count); ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myArr); list.setAdapter(myArrayAdapter); myArrayAdapter.notifyDataSetChanged();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.