【发布时间】:2020-05-12 23:06:03
【问题描述】:
我正在尝试遍历包含数字的字符串数组并将这些值解析为整数集合,将返回值显示为广播,例如“来自 --- 的作品,返回值”。
我已经用.split(',') 将字符串拆分为一个字符串数组,并尝试在将字符串值解析为整数时遍历该数组。
如果我可以直接输入索引 parseInt(FilterDataValues[1]) 或任何索引的索引,我可以从 for 循环中获取返回值,但是一旦我添加 i parseInt(FilterDataValues[i+1]) 或 parseInt(FilterDataValues[i]) 我无法获得返回值。该循环在一个更简单的循环中工作正常,所以我不太确定发生了什么。请帮助我,任何帮助都可以
public String sortingList(Context context)
{
String filterData = "";
StimulusDatabase td = new StimulusDatabase(context);
Cursor result = td.getAllData();
while (result.moveToNext())
{
filterData = (result.getString(23));
}
if(filterData.isEmpty())
{
return "";
}
else
{
String[] FilterDataValues = filterData.split(",");
for(int i = 0; i <= FilterDataValues.length; i++)
{
FilterOptions.add(parseInt(FilterDataValues[i+1]));
}
// for(int i = 0; i <= FilterDataValues.length; i++)
// {
// FilterOptions.add(i+1));
// }
return filterData;
}
}
整个代码的输出即使在 20/30 分钟后也不会导致出现通知
使用注释代码时的输出(0,2,6是列索引)
A word from Martin Luther King Jr
0,2,6
整个班级都在下面
package com.example.MentalHealthStateManagement.Application.Notifications;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Build;
import android.util.Log;
import com.example.MentalHealthStateManagement.Application.Database.StimulusDatabase;
import com.example.MentalHealthStateManagement.R;
import com.example.MentalHealthStateManagement.StateHandler.CheckIn;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import androidx.core.app.NotificationManagerCompat;
import static java.lang.Integer.parseInt;
public class Trigger_Receiver extends BroadcastReceiver
{
private static final String CHANNEL_ID = "M.H.S.M daily check-in registry";
private static final String TAG = "Trigger Receiver: ";
private String[] authors;
private String sortStatement;
private List<Integer> FilterOptions = new ArrayList<>();
private int notificationID = 135;
@Override
public void onReceive(Context context, Intent intent)
{
sortStatement = sortingList(context);
authors = getAuthors(context).split("#");
String[] quotes = getQuotes(context).split("#");
int randomNum = randomPosition();
// Create an exclusive intent for the user's daily check-in
intent = new Intent(context, CheckIn.class);
// Create the TaskStack Builder and add the Main Activity, which inflates the back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntentWithParentStack(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Checks if the API Level is 26 or greater
Notification.Builder notification = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notification = new Notification.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentTitle("A word from " + authors[randomNum])
.setContentText(sortingList(context))//quotes[randomNum]
.setStyle(new Notification.BigTextStyle().bigText(sortingList(context)))
.setContentIntent(pendingIntent)
.setAutoCancel(true);
}
NotificationManagerCompat notificationManger = NotificationManagerCompat.from(context);
assert notification != null;
notificationManger.notify(notificationID, notification.build());
notificationID++;
Log.i("Trigger_Receiver", "Trigger successful");
}
//Gets the authors for the specified trigger
public String getAuthors(Context context)
{
StringBuilder authors = new StringBuilder();
StimulusDatabase td = new StimulusDatabase(context);
// List<Integer> AuthorsToBeFiltered = NumberFilterValue;
Cursor result = td.getAllData();
while (result.moveToNext())
{
authors = new StringBuilder(result.getString(2) + " " + result.getString(4) + " "
+ result.getString(6) + " " + result.getString(8) + " " +
result.getString(10) + " " + result.getString(12) + " "
+ result.getString(14) + " " + result.getString(16) + " " +
result.getString(18) + " " + result.getString(20));
}
// while (result.moveToNext())
// {
// if(!AuthorsToBeFiltered.isEmpty())
// {
// for(int i = 0; i<AuthorsToBeFiltered.size(); i++)
// {
// if(AuthorsToBeFiltered.get(i) % 2 == 0)
// {
// authors.append(result.getString(i));
// }
// }
// }
// else
// {
// authors = new StringBuilder(result.getString(2) + " " + result.getString(4) + " " + result.getString(6) + " " + result.getString(8) + " " +
// result.getString(10) + " " + result.getString(12) + " " + result.getString(14) + " " + result.getString(16) + " " +
// result.getString(18) + " " + result.getString(20));
// }
// }
return authors.toString();
}
//Gets the quotes for the specified trigger
public String getQuotes(Context context)
{
StringBuilder quotes = new StringBuilder();
StimulusDatabase td = new StimulusDatabase(context);
//List<Integer> AuthorsToBeFiltered = NumberFilterValue;
Cursor result = td.getAllData();
while (result.moveToNext())
{
quotes.append(result.getString(1)).append(" ").append(result.getString(3))
.append(" ").append(result.getString(5)).append(" ").append(result.getString(7))
.append(" ").append(result.getString(9)).append(" ").append(result.getString(11))
.append(" ").append(result.getString(13)).append(" ").append(result.getString(15))
.append(" ").append(result.getString(17)).append(" ").append(result.getString(19));
}
// while (result.moveToNext())
// {
// if(!AuthorsToBeFiltered.isEmpty())
// {
// for(int i = 0; i<AuthorsToBeFiltered.size(); i++)
// {
// if(AuthorsToBeFiltered.get(i) % 2 == 0)
// {
// quotes.append(result.getString(i-1));
// }
// }
// }
// else
// {
// quotes.append(result.getString(1)).append(" ").append(result.getString(3))
// .append(" ").append(result.getString(5)).append(" ").append(result.getString(7))
// .append(" ").append(result.getString(9)).append(" ").append(result.getString(11))
// .append(" ").append(result.getString(13)).append(" ").append(result.getString(15))
// .append(" ").append(result.getString(17)).append(" ").append(result.getString(19));
// }
// }
return quotes.toString();
}
//A random number used to gather different quotes and authors
//P.S. each quote belongs to appropriate author due to the delimiter settings preformed in the StimulusSetup class
public Integer randomPosition()
{
int randomNum = 0;
// Ensures the API level 26 or greater
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
randomNum = ThreadLocalRandom.current().nextInt(0, authors.length + 1);
}
else
{
Log.d(TAG,"Incompatible device, API lower than lollipop");
}
return randomNum;
}
public String sortingList(Context context)
{
String filterData = "";
StimulusDatabase td = new StimulusDatabase(context);
Cursor result = td.getAllData();
while (result.moveToNext())
{
filterData = (result.getString(23));
}
if(filterData.isEmpty())
{
return "";
}
else
{
String[] FilterDataValues = filterData.split(",");
for(int i = 0; i <= FilterDataValues.length; i++)
{
FilterOptions.add(parseInt(FilterDataValues[i+1]));
}
// ` Working loop
// for(int i = 0; i <= FilterDataValues.length; i++)
// {
// FilterOptions.add(i+1));
// }
return filterData;
}
}
}
【问题讨论】:
标签: java android arrays parsing broadcastreceiver