我想計算出如何在 month 數組中添加另一個字符串,然後通過點擊按鈕在一個list view中動態的顯示。現在我可以顯示指定的數組,但是不知道如何添加計數變量,就是應該在點擊按鈕後再添加到 array list中的。請問如何實現?
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;
}
}
}
顯然String數組是指定長度的了,無法進行添加
你得用List或者ArrayList
然後在click中寫:
list.add(""+count);
adapter.notifyDataSetChanged();