按钮onClick将值添加到ArrayAdapter数组

时间:2011-11-12 07:32:55

标签: android listview

我试图找出如何将另一个字符串添加到我的月份数组中,并通过按下按钮动态显示在列表视图中。目前我没有问题显示我指定的数组,但我无法找到一种方法来添加我的count变量,该变量应该在单击按钮后添加到数组列表中。有任何想法吗 ?谢谢你的帮助!!!!

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 个答案:

答案 0 :(得分:2)

当你点击按钮时,更新数组的内容{在你的案例月[]中,比如给它增加月份}然后调用“ListAdapter.this.notifyDataSetChanged();” whenre ListAdapter是您的自定义适配器。基本上,notifyDataSetChanged将调用适配器的getview,它将更新列表