반응형

출처: https://stackoverflow.com/questions/32136029/how-can-i-select-only-one-checkbox-in-a-dynamic-view


Firstly, declare an ArrayList in the class:

ArrayList<CheckBox> mCheckBoxes = new ArrayList<CheckBox>();

Then in addGreetingToListView add every new checkbox to mCheckBoxes and modify the click listener of the checkbox:

checkBox.setTag(greetings);

mCheckBoxes.add(checkBox);
checkBox.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick (View view) {

            if (((CheckBox) view).isChecked())
            {
                for (int i = 0; i < mCheckBoxes.size(); i++) {
                     if (mCheckBoxes.get(i) == view)
                         selected_position = i;
                     else 
                         mCheckBoxes.get(i).setChecked(false);
                }

            }
            else
            {
                selected_position=-1;
            }           
        }

    });


반응형
반응형

 출처: http://soulduse.tistory.com/34 [프로그래밍좀비]


SQLite 관련 작업중 테이블에 현재 시간을 기본설정 값으로 넣으면 


우리나라 현재시간과 비교했을 때 9시간이 빠르게 나오는 현상이 발생했다.


아래는 SQLite 테이블을 만들고 기본값을 설정한 상태이다.

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " ("
+ IDX + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ REG_DATE1 + " DATETIME DEFAULT (datetime('now','localtime'))"
+ REG_DATE2 + " DATETIME DEFAULT (datetime('now'))"
+ REG_DATE3 + " DATETIME DEFAULT CURRENT_TIMESTAMP"
+ ");");
}



EX) 현재 우리나라 시간이 2016-04-26 17:46:49라고 했을때,


     위의 내용에대한 결과는 아래와 같이 출력된다.

REG_DATE1 : 2016-04-26 17:46:49
REG_DATE2 : 2016-04-26 10:46:49
REG_DATE3 : 2016-04-26 10:46:49


결과를 보면 


datetime('now','localtime')    // => 현재 우리나라 시간과 동일한 시간을 얻는다.

datetime('now')                // => 국제표준시간(UTC) 기준의 값을 출력

CURRENT_TIMESTAMP              // => 국제표준시간(UTC) 기준의 값을 출력


임을 알 수 있다.


SQLite 작성과 시간에 있어 삽질시간 단축에 도움이 되었으면 한다. ^^



++ 추가


SQLite에서 날짜를 표시하기 위한 함수는 아래의 5가지가 있다.

date(timestring, modifier, modifier, ...) : 날짜
time(timestring, modifier, modifier, ...) : 시간
datetime(timestring, modifier, modifier, ...) : 날짜+시간
julianday(timestring, modifier, modifier, ...) : 율리우스력 날짜
strftime(format, timestring, modifier, modifier, ...) : 포맷 지정

흔히 현재 날짜를 얻을 때 date, time, datetime을 자주 사용하게 된다.
이 함수에 'now' 문자열을 넣으면 현재 날짜와 시간을 얻을 수 있고, 이는 xCurrentTime 메소드가 제공해 주는 것이라고 SQLite 공식 페이지가 말하고 있다.(-_-;;)

즉, SELECT date('now'); 를 하면 현재 날짜가 '2011-12-25' 일 형식 으로 나오고,
SELECT datetime('now'); 를 하면 현재 날짜+시간(시:분:초)이 '2011-12-25 23:30:11' 형식으로 나온다.
단, 국제표준시간(UTC) 기준의 값을 출력한다.

우리나라의 표준 시간은 국제표준시+9 이기 때문에 시간이 틀리게 출력 된다.

해결 방법은 대략 두가지.

1. localtime 값을 이용한 현재시간 구하기 

SELECT datetime('now','localtime'); 


위와 같이 하면 국제표준시+9 가 된 우리나라 시간이 된다.

2. 시간 값 더하기 

SELECT datetime('now','+9 hours'); 


국제 표준시에 +9를 해 주면 우리나라 시간이 된다.


편한게 좋기 때문에 1번 추천.

더 자세한 정보는 SQLite 공식 페이지의 'Date And Time Functions' 페이지를 참조 하자.

'보고픈'님이 정리하신 SQLitet 날짜 관련 샘플

--UTC 기준의 현재 날짜/시간 
select datetime('now'); 
2010-08-25 04:01:46 

-- 로컬 기준의 현재 날짜/시간
select datetime('now','localtime'); 
2010-08-25 13:02:30

--현재 로컬 기준 시간에서 10분 3.5초를 더한 시간.
select datetime('now','localtime','+3.5 seconds','+10 minutes');
2010-08-25 13:14:15

--현재 로컬 시간에 3.5초를 더하고 날짜는 돌아오는 화요일 (weekday == 0 이 일요일입니다.)
select datetime('now','localtime','+3.5 seconds','weekday 2');
2010-08-31 13:05:39

--현재 달의 마지막 날짜
SELECT date('now','start of month','+1 month','-1 day','localtime');
2010-08-31

--2004-01-01 02:34:56초부터 현재까지의 총 초
SELECT strftime('%s','now') - strftime('%s','2004-01-01 02:34:56');
209785028

--현재날짜/시간 기준에서 올해 9번째달의 첫번째 화요일
SELECT date('now','start of year','+9 months','weekday 2');
2010-10-05

-- 날짜 포맷 스타일 변경 
select strftime("%Y/%m/%d %H:%M:%S",'now','localtime');
2010/08/27 09:17:22



출처: http://marga.tistory.com/578 [margalog]



참고 사이트

http://stackoverflow.com/questions/381371/sqlite-current-timestamp-is-in-gmt-not-the-timezone-of-the-machine

http://wiki.simplism.kr/programming:database:sqlite

http://marga.tistory.com/578



반응형
반응형

출처: https://stackoverflow.com/questions/20261181/nullpointerexception-on-getactivity-fragment


  1. First declare context variable: 
    private Context context;
  2. In onCreateView():
    context = container.getContext();
  3. Use it for Toast:
    Toast.makeText(context, "Your vote was sent", Toast.LENGTH_LONG).show();


반응형
반응형

출처: https://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity



Try using interfaces.

Any fragment that should pass data back to its containing activity should declare an interface to handle and pass the data. Then make sure your containing activity implements those interfaces. For example:

In your fragment, declare the interface...

public interface OnDataPass {
    public void onDataPass(String data);
}

Then, connect the containing class' implementation of the interface to the fragment in the onAttach method, like so:

OnDataPass dataPasser;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    dataPasser = (OnDataPass) context;
}

Within your fragment, when you need to handle the passing of data, just call it on the dataPasser object:

public void passData(String data) {
    dataPasser.onDataPass(data);
}

Finally, in your containing activity which implements OnDataPass...

@Override
public void onDataPass(String data) {
    Log.d("LOG","hello " + data);
}


반응형

+ Recent posts