程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-程序開始運行時強制關閉,Adapter方法出錯(用來實現鬧鐘的時間列表)

android-程序開始運行時強制關閉,Adapter方法出錯(用來實現鬧鐘的時間列表)

編輯:編程綜合問答
程序開始運行時強制關閉,Adapter方法出錯(用來實現鬧鐘的時間列表)

package pcAlarm.pack;

import java.util.Calendar;
import java.util.Currency;

import pcAlarm.pack.PCAlarmActivity;

import android.app.Activity;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Contacts;
import android.view.View;
import android.widget.ArrayAdapter;
import pcAlarm.pack.AlarmView;
import pcAlarm.pack.AlarmView.AlarmData;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TimePicker;

public class PCAlarmActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();

    tabHost.addTab(tabHost.newTabSpec("tabAlarm").setIndicator("鬧鐘").setContent(R.id.tabAlarm));


    btnAddAlarm=(Button)findViewById(R.id.btnAddAlarm);
    lvListView=(ListView)findViewById(R.id.lvListView);

    adapter=new ArrayAdapter<PCAlarmActivity.AlarmData>(getContext(), android.R.layout.simple_list_item_1);
    lvListView.setAdapter(adapter);

    adapter.add(new AlarmData(System.currentTimeMillis()));





    btnAddAlarm.setOnClickListener(new OnClickListener() {

        private Calendar mcalender;

        @Override
        public void onClick(View v) {
            Calendar.getInstance().setTimeInMillis(System.currentTimeMillis());
            mcalender = Calendar.getInstance();
            int mHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
            int mMinute = Calendar.getInstance().get(Calendar.MINUTE);
            new TimePickerDialog(PCAlarmActivity.this, new TimePickerDialog.OnTimeSetListener() {

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    // TODO Auto-generated method stub
                    Calendar.getInstance().setTimeInMillis(System.currentTimeMillis());
                    Calendar.getInstance().set(Calendar.HOUR_OF_DAY, hourOfDay);
                    Calendar.getInstance().set(Calendar.MINUTE, minute);
                    Calendar.getInstance().set(Calendar.SECOND, 0);
                    Calendar.getInstance().set(Calendar.MILLISECOND, 0);

                    AlarmData add = new AlarmData(Calendar.getInstance().getTimeInMillis());




                }
            }, mHour, mMinute, true).show();
        }
    });

}
private Context getContext() {
    // TODO Auto-generated method stub
    return null;
}
private Button btnAddAlarm;
private ListView lvListView;
private ArrayAdapter<AlarmData> adapter;

private static class AlarmData{
    public AlarmData(long time){
        this.time=time;
        date= Calendar.getInstance();
        timeLabel=String.format("%d月%d日 %d:%d", 
                date.get(Calendar.MONTH)+1,
                date.get(Calendar.DAY_OF_MONTH),
                date.get(Calendar.HOUR_OF_DAY),
                date.get(Calendar.MINUTE));

    }

    public long getTime() {
        return time;
    }

    public String getTimeLabel() {
        return timeLabel;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return getTimeLabel();
    }

    private String timeLabel="";
    private long time=0;
    private Calendar date;
}

private TabHost tabHost;

}

最佳回答:


 adapter=new ArrayAdapter<PCAlarmActivity.AlarmData>(getContext(), android.R.layout.simple_list_item_1);
    lvListView.setAdapter(adapter);

    adapter.add(new AlarmData(System.currentTimeMillis()));//這種方式添加,會直接notify更新界面,但在oncreate中,界面沒初始化完成。
        改成
        List<PCAlarmActivity.AlarmData> dataList = new ArrayList();
    dataList.add(new AlarmData(System.currentTimeMillis()));
    adapter=new ArrayAdapter<PCAlarmActivity.AlarmData>(getContext(), android.R.layout.simple_list_item_1,dataList);
    lvListView.setAdapter(adapter);試試吧,
danielinbiti
qq_28818163
danielinbiti
qq_28818163
qq_28818163
qq_28818163
qq_28818163
danielinbiti
qq_28818163
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved