我是android的新手,当我因某种原因使用ArrayAdapter
的添加功能时,我遇到了异常。
我知道这个过程应该是微不足道的,但我找不到我的错误。
我正在尝试添加日期和时间记录时间到ListView
。
以下是我的表现:
文件名:ClockTest.java
/* package name here */
/* import section here */
public class ClockTest extends Activity {
/** Called when the activity is first created. */
public int logButtonState;
public int logEntriesCount;
ImageButton logButton;
ImageButton switchButton;
ListView entriesList;
public ArrayAdapter<String> listAdapter;
public String logEntries[]={
"Entry1",
"Entry2",
"Entry3",
"Entry4",
"Entry5"};
public String date_info;
public String time_info;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// State: 0=login, 1=logout
logButtonState = 0;
logButton = (ImageButton)findViewById(R.id.log_button);
switchButton = (ImageButton)findViewById(R.id.switch_button);
// Infrastructure for entries on-screen presentation
logEntriesCount = 0;
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
logEntries);
entriesList = (ListView)findViewById(R.id.entries_list);
entriesList.setAdapter(listAdapter);
// add a click listener to the exit button
logButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView timeCaptured = (TextView)findViewById(R.id.timeCaptured);
Calendar c = Calendar.getInstance();
c.setTime(new Date(System.currentTimeMillis()));
date_info = String.valueOf(c.get(Calendar.DAY_OF_MONTH))
+ "."
+ String.valueOf(c.get(Calendar.MONTH))
+ "."
+ String.valueOf(c.get(Calendar.YEAR));
time_info = String.valueOf(c.get(Calendar.HOUR_OF_DAY))
+ ":"
+ String.valueOf(c.get(Calendar.MINUTE))
+ ":"
+ String.valueOf(c.get(Calendar.SECOND));
final String currentTime = asignLogString()
+ " ["
+ date_info
+ " "
+ time_info
+ "]";
logEntriesCount++;
timeCaptured.setText(currentTime);
//listAdapter.clear();
// re-arrange on-screen list view
listAdapter.add(currentTime);
listAdapter.notifyDataSetChanged();
entriesList.setAdapter(listAdapter);
}
});
}
}
答案 0 :(得分:0)
ArrayAdapter有一个不可修改的列表。您将无法向其中添加内容,否则您将获得UnsupportedOperationException(根据我的记忆)。
使用ArrayList&lt; String&gt;。
,而不是使用String []请查看Populate Spinner dynamically in android from edit text以获得解决方案。