2011-06-01 53 views
1

我读从DB一些数据的线程的AsyncTask .... 这样:问题的AsyncTask螺纹

protected Void doInBackground(DBAdapter... db) { 

     try { 

      db[0].openDataBase(); 

      Cursor c = db[0].getCursor3(db[0].TABLE_3, user_id); 

      float[] viteza = new float[c.getCount()]; 

      String[] time = new String[c.getCount()]; 

      if (c.moveToFirst()) { 

       do { 

        viteza[i] = Float.parseFloat(c.getString(3)); 
        time[i] = c.getString(4); 


        publishProgress(????); 

        Thread.sleep(2500); 

        i++; 
       } while (c.moveToNext()); 

      } 
      c.close(); 
      db[0].close(); 

     } catch (Exception e) { 
      Log.d("Eroare", "doInBackground", e); 
     } 

     return null; 
    } 

我的问题是,每次我得到一个新的值viteza [ i]和时间[I]我要送他们到

protected void onProgressUpdate(....?) { 

} 

都在同一时间....但我不知道怎么做,原因publishProgress()可以只有一个参数! !

有人可以帮我吗?THX

回答

0

以下应工作假设你有在正确的顺序数据值(的getString(3)应该是一个数字和GetString(4)应该是一个时间值)。

protected Void doInBackground(DBAdapter... db) { 
    // ... some db work 
    if (c.moveToFirst()) { 
     publishProgress(c.getString(3), c.getString(4)); 
     // ... 
    } 
} 

protected void onProgressUpdate(String... values) { 
    float viteza = Float.parseFloat(values[0]); 
    String time = values[1]; 
    // do something with the data that shows in UI 
} 

一个完整的AsyncTask测试传递值是在这里:

private class SimpleTask extends AsyncTask<Void, String, Void> { 

    @Override 
    protected Void doInBackground(Void... params) { 
     String floatAsString = "3.14159265358979"; 
     String timeAsString = "06-01 07:32:02.579"; 
     publishProgress(floatAsString, timeAsString); 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(String... values) { 
     float pi = Float.parseFloat(values[0]); 
     Log.i("SimpleTask", String.format("Got values '%s' and '%s'. pi = %f", values[0], values[1], pi)); 
    } 

} 

它打印输出:

Got values '3.14159265358979' and '06-01 07:32:02.579'. pi = 3.141593 
+0

我这样做...... viteza [contor] = Float.parseFloat(values [0]);它会抛出一个异常,并且我得到强制关闭: java.lang.NumberFormatException: 06-01 07:32:02.579:ERROR/AndroidRuntime(483):at org.apache.harmony.luni.util.FloatingPointParser.parseFltImpl (Native Method) at org.apache.harmony.luni.util.FloatingPointParser.parseFloat(FloatingPointParser.java:321) – adrian 2011-06-01 19:33:01

+0

传递给'onProgressUpdate()'的字符串的确切值是什么?每次检查values数组的长度为2,并查看它们的值。异常是告诉你这个值不能作为浮点值进行分析。 – 2011-06-01 20:03:18

+0

Bu的值是214.721 ....为什么不能解析为float ????? – adrian 2011-06-01 20:46:51

0

你可以离开他们两个字符串,并通过字符串[]以publishProgress()数组,保持相同的顺序,然后转换viteza [I]的价值在publishProgress()中浮动。如果你不想担心数组的顺序,你也可以把它们放入一个超级简单的类中,然后传递这个对象,但是我会用数组来处理这么小的事情。

public class HelloAndroid extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    MyAsyncTask myTask = new MyAsyncTask(); 
    myTask.execute(); 
} 

private class MyAsyncTask extends AsyncTask<Void, String, Void> { 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     // TODO Auto-generated method stub 
     String[] first = {"1", "2", "3", "4", "5"}; 
     String[] second = {"5", "4", "3", "2", "1"}; 

     for(int i = 0; i < first.length; i++) { 
          //put together a string array with our values in the desired order 
      String[] vals = {first[i], second[i]}; 
          //publishProgress is what is used to call onProgressUpdate() 
      publishProgress(vals); 
     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(String...strings) { 

        //Strings are now in strings[0] and strings[1] in the order 
        //that they were put into vals array in doInBackground() 
     String passedValues = strings[0] + "," + strings[1]; 

        //Do whatever UI updates. 
     TextView mytext = (TextView)findViewById(R.id.mytext); 
     mytext.setText(passedValues); 
    } 

} 

}

+0

数组然后是它;) – adrian 2011-06-01 18:00:48

+0

我有这个保护无效onProgressUpdate(String [] ...数组){...}其中数组[]有两个元素....但数组[0] = viteza [0] ....我怎样才能获得我的元素....? – adrian 2011-06-01 18:18:05

+0

我马上就会在这里为你做一个实例。 – jmichalicek 2011-06-01 18:34:03

0

考虑,它包装在两个值的自定义对象,并通过此对象进展如下:

private class MyAsynch extends AsyncTask<Void,MyObject,Void>{ 

JAL