我的数据库表包含task,task_time,task_completed等字段。在我的应用程序中,我正在获取task_time字符串并将其与每次在计时器内循环的android系统时间进行比较。如果时间匹配,那么我将根据列表视图中指定的时间显示该任务字符串。我在这里遇到的问题是,每当添加新列表时,之前的列表就会被删除,即它们会被覆盖。
这是代码。
MainAcivity.java
public class MainAcivity extends Activity
{
private int Current_level = 0;
private int tcount = 0;
private Timer t;
private String[] ids;
private String Current_Date = "";
private String Current_Time = "";
private String DB_Date = "";
private ListView listView;
private TaskAdapter adapter;
private TextView timercount;
private ArrayList<String> LoadTaskFromDb = new ArrayList<String>();
private ArrayList<String> LoadTimeFromDb = new ArrayList<String>();
private DatabaseHandler db = new DatabaseHandler(TaskScheduler.this);
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.task_scheduler);
listView = (ListView) findViewById(R.id.listview1);
timercount = (TextView)findViewById(R.id.levelStartedTime);
Current_level = 1;
Initialization(Current_level);
}
//Check level and extract task
private void Initialization(int curr_level)
{
LoadTaskFromDb = db.GetTaskBasedOnLevel(curr_level);
LoadTimeFromDb = db.getTime(curr_level);
t = new Timer();
t.scheduleAtFixedRate(new TimerTask()
{
@Override
public void run()
{
runOnUiThread(new Runnable()
{
@Override
public void run()
{
Load(LoadTaskFromDb, LoadTimeFromDb);
Log.d("TimerCount", String.valueOf(tcount));
tcount++;
}
});
}
},0, 30000);
}
//HERE IS THE PROBLEM
//CHECKING SYSTEM TIME WITH DB_TIME
private void Load(ArrayList<String> task, ArrayList<String> time)
{
for(int j = 0; j < time.size(); j++)
{
if(CompareTime(time.get(j)))
{
LoadTask.LoadTaskFromDB(task, j); //If matches loading task
ids = new String[LoadTask.Items.size()];
for (int i = 0; i < ids.length; i++)
{
int k = i + 1;
ids[i] = Integer.toString(k);
}
adapter = new TaskAdapter(this,R.layout.task_llist, ids);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> listView, View view, int position, long id)
{
Log.d("LIST_POS", String.valueOf(position));
TextView task = (TextView)view.findViewById(R.id.taskString);
Log.d("LIST_POS_TASK", task.getText().toString());
}
});
}
else
{
Log.d("Nomatch", "NO MATCH FOUND");
}
}
}
//compare system time with db fetched time
private boolean CompareTime(String db_time)
{
boolean time_match = true;
//TODO check time format in database
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm aa");
Date systemDate = Calendar.getInstance().getTime();
String myDate = sdf.format(systemDate);
Date Date1 = null;
Date Date2 = null;
try
{
Date1 = sdf.parse(myDate);
}
catch (ParseException e)
{
e.printStackTrace();
}
try
{
Date2 = sdf.parse(db_time);
}
catch (ParseException e)
{
e.printStackTrace();
}
if(Date1.compareTo(Date2) == 0)
{
time_match = true;
}
else
{
time_match = false;
}
return time_match;
}
}
Task.java
public class Task
{
public static int Id;
public static String IconFile;
public static String Task;
public static String TimeLeft;
public Task(int id, String iconFile, String task, String timeleft)
{
Id = id;
IconFile = iconFile;
Task = task;
TimeLeft = timeleft;
}
}
TaskAdapter.java
public class TaskAdapter extends ArrayAdapter<String>
{
public MyCounter timer;
private final Context context;
private final String[] Ids;
private final int rowResourceId;
public TaskAdapter(Context context, int textViewResourceId, String[] objects)
{
super(context, textViewResourceId, objects);
this.context = context;
this.Ids = objects;
this.rowResourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(rowResourceId, parent, false);
ImageView imageView = (ImageView) rowView.findViewById(R.id.taskImage);
TextView textView = (TextView) rowView.findViewById(R.id.taskString);
TextView timeleft = (TextView) rowView.findViewById(R.id.timeShow);
int id = Integer.parseInt(Ids[position]);
String imageFile = LoadTask.GetId(id).IconFile;
textView.setText(LoadTask.GetId(id).Task);
timeleft.setText("2 hr left");
InputStream ims = null;
try
{
ims = context.getAssets().open(imageFile);
}
catch (IOException e)
{
e.printStackTrace();
}
Drawable d = Drawable.createFromStream(ims, null);
imageView.setImageDrawable(d);
return rowView;
}
}
LoadTask.java
public class LoadTask
{
public static ArrayList<Task> Items = null;
public static void LoadTaskFromDB(ArrayList<String> task, int getTimeid)
{
Items = new ArrayList<Task>();
for(int i = 0; i < task.size(); i++)
{
int j = i + 1;
Items.add(new Task(j, "news.png", task.get(getTimeid).toString(), ""));
}
}
public static Task GetId(int id)
{
for(Task task : task)
{
if (task.Id == id)
{
return task;
}
}
return null;
}
}