【问题标题】:exception thrown after trying to add string to ListView尝试将字符串添加到 ListView 后引发的异常
【发布时间】:2011-05-25 18:41:44
【问题描述】:

我是android新手,由于某种原因在使用ArrayAdapter的add函数时遇到了异常。

我知道这个过程应该是微不足道的,但我找不到我的错误。

我正在尝试将日期和时间记录添加到 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);
              }
         });     
     }
 }

【问题讨论】:

  • 感谢@Haphazard 花费的时间,感谢您的努力。 Bruce 帮我解决了这个问题。

标签: android android-widget android-listview


【解决方案1】:

ArrayAdapter 有一个不可修改的列表。您将无法向其中添加内容,否则您将收到 UnsupportedOperationException(据我所知)。

不要使用 String[],而是使用 ArrayList

查看Populate Spinner dynamically in android from edit text 以获得解决方案。

【讨论】:

  • 谢谢布鲁斯!你帮我省去了很多挫折、时间和精力。
  • 布鲁斯,来自我来自(以色列)的地方,习惯上祝福有帮助的人。我对你的祝福是,你的一个伟大愿望(为善)将在今年年底前实现。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多