我已尝试使用以下方法执行此任务但它无法正常工作,但根本没有显示任何错误。 这是比较部分:
Cursor res = myDb.showKharcha();
final String selected= parent.getItemAtPosition(position).toString();
while(res.moveToNext()) {
if(selected == res.getString(1)) {
int num = Integer.parseInt(editText.getText().toString());
num = num + Integer.parseInt(res.getString(2));
boolean isUpdated = myDb.updateData(res.getString(0),selected,num);
if (isUpdated == true) {
Toast.makeText(add_trans.this,"Kharcha updated to "+ selected,Toast.LENGTH_LONG).show();
} else {
Toast.makeText(add_trans.this, "Kharcha not added", Toast.LENGTH_LONG).show();
}
这是DatabaseHelper类中的showKharcha
函数:
public Cursor showKharcha(){
SQLiteDatabase db = this.getWritableDatabase();
Cursor tyt = db.rawQuery("select * from " + TABLE_NAME,null);
return tyt;
}
数据库更新代码为:
public boolean updateData(String ID, String KharchaType, Integer Kharcha){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1,ID);
contentValues.put(COL_2,KharchaType);
contentValues.put(COL_3,Kharcha);
db.update(TABLE_NAME,contentValues, "ID = ?",new String[] { ID });
return true;
}
答案 0 :(得分:0)
您已经讨论过比较Spinner中选定值的值。
使用此using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApp1
{
class Program
{
static object lockObject = new object();
static HashSet<TaskCompletionSource<bool>> completionSources = new HashSet<TaskCompletionSource<bool>>();
static void Main(string[] args)
{
MainAsync().Wait();
}
static async Task MainAsync()
{
Task t1 = T1Async();
Task t2 = T2Async();
await t1;
await t2;
}
static async Task T1Async()
{
Console.WriteLine("T1 start.");
if (await WaitAsync()) Console.WriteLine("Wait succeeded.");
else Console.WriteLine("Wait failed.");
Console.WriteLine("T1 end.");
}
static async Task T2Async()
{
Console.WriteLine("T2 start.");
await Task.Delay(500);
CompleteAndClear();
Console.WriteLine("T2 end.");
}
static async Task<bool> WaitAsync()
{
Console.WriteLine("Wait start.");
bool res = false;
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(3000))
{
using (CancellationTokenRegistration cancellationTokenRegistration = cancellationTokenSource.Token.Register(() => { tcs.TrySetResult(false); }))
{
Add(tcs);
res = await tcs.Task;
Remove(tcs);
}
}
Console.WriteLine("Wait end.");
return res;
}
static void Add(TaskCompletionSource<bool> TaskCompletionSource)
{
Console.WriteLine("Add start.");
lock (lockObject)
{
completionSources.Add(TaskCompletionSource);
}
Console.WriteLine("Add end.");
}
static void Remove(TaskCompletionSource<bool> TaskCompletionSource)
{
Console.WriteLine("Remove start.");
lock (lockObject)
{
if (!completionSources.Remove(TaskCompletionSource)) Console.WriteLine("TCS not found.");
}
Console.WriteLine("Remove end.");
}
static void CompleteAndClear()
{
Console.WriteLine("CompleteAndClear start.");
lock (lockObject)
{
if (completionSources.Count > 0)
{
Console.WriteLine("Completing {0} TCSs.", completionSources.Count);
foreach (TaskCompletionSource<bool> tcs in completionSources)
tcs.TrySetResult(true);
Console.WriteLine("Clearing TCS list.");
completionSources.Clear();
}
}
Console.WriteLine("CompleteAndClear end.");
}
}
}
而不是
final String text = spinner.getSelectedItem().toString();
。
如果你设置OnItemSelectedListener会更好,这样当用户选择时,比较会自动发生:
final String selected= parent.getItemAtPosition(position).toString();
答案 1 :(得分:0)
final String text = spinner.getSelectedItem().toString();
从Spinner
和
if(selected.equals(res.getString(1)))
进行比较有关==
和equals