dataは、「4/29:22:34」のように表示されます。nameは、「ito」などの名前でcommentは、「おはよう」などです。
このコードのようにする事で、解決しました。
java
1 String checkdata="初期値"; 2 3 for (int i = 1; i <= c.getCount(); i++) { 4 5 String date = c.getString(0); 6 String name = c.getString(1); 7 String comment = c.getString(2); 8 9 10 if(i==1) { 11 12 textLog +="(あ)"+date + " " + name + ": " + comment + "\n"; 13 14 //そして、dateの日付をcheckdateにいれる 15 checkdata=date.substring(0, date.indexOf(":")); 16 17 /////////////分が同じならifでとおってる、そして、時間全部を消してる 18 //dateの:以前をcheckdateにいれて、replaceするように 19 20 }else if(date.startsWith(checkdata)){ //(dateにcheckdataが含まれる場合) checkdateは前のコメントの日付 21 22 //前のコメントと日にちが同じ場合は月日を省く 23 date= date.replaceAll(checkdata, ""); 24 if(checkdata.length() == 0 ){ 25 textLog +="(い)"+checkdata+"checkdataを表示 checkdataがから文字"+ date +" "+ name + ": " + comment + "\n"; 26 }else{ 27 textLog +="(い)"+checkdata+"checkdataを表示 checkdataはから文字じゃない"+ date +" "+ name + ": " + comment + "\n"; } 28 29 //そして、dateをcheckdateにいれる 30 // checkdata=date.substring(0, date.indexOf(":")); 31 32 33 } else if(!(date.startsWith(checkdata)) && checkdata!="初期値"){ 34 // 前方一致ではありません、つまり前日とは違う日で、"チェックデータ"でもない場合 35 textLog += "(う)"+ date + " " + name + ": " + comment + "\n"; 36 //そして、dateをcheckdateにいれる 37 //dateは、前のコメントの日付のはず 38 checkdata=date.substring(0, date.indexOf(":")); 39 40 }else if(checkdata=="初期値"){ 41 textLog+="初期値です\n"; 42 }
掲示板で、日にちが変わった日の最初のコメントだけ、何月何日:何時何分と表示したいです。
私が書いた下記のコードですと、今日の最初のコメントにだけは、何月何日:何時何分と表示され、今日の2つめ以降のコメントでは、何時何分とだけ表示させられるのですが、
昨日から、前の日に対しては、全て、何月何日:が表示されてしまいます。
どう書けばよいのでしょうか?
アドバイスの方どうぞ宜しくお願い致します
java
1 for (int i = 1; i <= c.getCount(); i++) { 2 //SQL文の結果から、必要な値を取り出す 3 4 String date = c.getString(0); 5 String name = c.getString(1); 6 String comment = c.getString(2); 7 8 Calendar calendar = Calendar.getInstance(); 9 int month = calendar.get(Calendar.MONTH) + 1; 10 int day = calendar.get(Calendar.DATE); 11 String checkdata = month + "/ " + day ; 12 13 if (date.startsWith(checkdata)){ 14 // 前方一致の場合は、同じ日だからdateからcheckdataを削除して 15 //昨日は全部表示されてしまう 16 17 if(i==1) { 18 textLog +=date + ": " + name + ": " + comment + "\n"; 19 }else{ 20 date= date.replaceAll(checkdata, ""); 21 textLog += date + ": " + name + ": " + comment + "\n"; 22 } 23 } 24 else { 25 // 前方一致ではありません、つまり前日とは違う日だから4/29と表示 26 textLog += date + ": " + name + ": " + comment + "\n"; 27 28 } 29 textView.setText(textLog); 30 31 c.moveToNext(); 32}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/29 13:20
2017/04/29 13:58 編集
2017/04/29 16:05 編集
2017/04/29 15:13
2017/04/29 16:46