2013-05-13 68 views
1

我試圖從雅虎財務下載一些數據並在屏幕上使用listview顯示它們。 我收集的數據不錯,我的問題是更新列表視圖和刪除數據 古董數組。使用asyncTask更新列表視圖

這裏,我離開我的代碼:

public class Fragment1 extends Fragment { 

static List<String[]> datos = new ArrayList<String[]>(); 
static ListView lv; 
static ArrayList nombres = new ArrayList(); 
static ArrayList valores = new ArrayList(); 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View v = inflater.inflate(R.layout.fragment_1, container, false); 
    messageList(v); 

    return v; 
} 

private void messageList(View v) { 
    adapter adapter = new adapter(getActivity(), null); 
    ArrayAdapter<String> adaptador = new ArrayAdapter<String>(
      getActivity(), android.R.layout.simple_list_item_1, valores); 
    new MyLongTask().execute(adaptador); 

    for (int i = 0; i < datos.size(); i++) { 
     for (int e = 0; e < 4; e++) { 
      nombres.add(datos.get(i)[e]); 
     } 
    } 
    datos.clear(); 
    valores = nombres; 
    System.out.println(valores); 

    lv = (ListView) v.findViewById(R.id.listView1); 
    lv.setAdapter(adaptador); 

} 

static class MyLongTask extends AsyncTask<ArrayAdapter<String>, Void, Void> { 

    protected void onPreExecute(ArrayAdapter<String> adaptador) { 
     // Avísele al usuario que estamos trabajando 
    } 

    @Override 
    protected Void doInBackground(ArrayAdapter<String>... params) { 
     System.out.println("entra en el hilo"); 
     datos.clear(); 
     String next[] = {}; 
     List<String[]> list = new ArrayList<String[]>(); 
     try { 
      URL url = new URL(
        "http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT&f=nab2b3"); 
      InputStreamReader in = new InputStreamReader(url.openStream()); 
      CSVReader reader = new CSVReader(in); 
      for (;;) { 
       next = reader.readNext(); 
       if (next != null) { 
        list.add(next); 

       } else { 
        break; 
       } 
      } 
      datos = list; 

     } catch (IOException e) { 
      System.out.println(e); 
     } 
     try { 
      Thread.sleep(200);// 2000 es 2 segundos 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     System.out.println("sale del hilo"); 

     return null; 
    } 

    protected void onPostExecute(ArrayAdapter<String> adaptador) { 
     adaptador.notifyDataSetChanged(); 
     System.out.println("onpostexecute"); 
     new MyLongTask().execute(); 
    } 
} 

} 

回答

2

像這樣的事情

public class Fragment1 extends Fragment { 

static List<String[]> datos = new ArrayList<String[]>(); 
static ListView lv; 
static ArrayList nombres = new ArrayList(); 
static ArrayList valores = new ArrayList(); 
ArrayAdapter<String> adaptador; 

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 

View v = inflater.inflate(R.layout.fragment_1, container, false); 
messageList(v); 

return v; 
} 

private void messageList(View v) { 
adapter adapter = new adapter(getActivity(), null);//what is this??? 
adaptador = new ArrayAdapter<String>(
     getActivity(), android.R.layout.simple_list_item_1, valores); 
new MyLongTask().execute(adaptador); 

for (int i = 0; i < datos.size(); i++) { 
    for (int e = 0; e < 4; e++) { 
     nombres.add(datos.get(i)[e]); 
    } 
} 
datos.clear(); 
valores = nombres; 
System.out.println(valores); 

lv = (ListView) v.findViewById(R.id.listView1); 
lv.setAdapter(adaptador); 

} 

static class MyLongTask extends AsyncTask<String, Void, List<String[]>> { 

protected void onPreExecute(ArrayAdapter<String> adaptador) { 
    // Avísele al usuario que estamos trabajando 
} 

@Override 
protected List<String[]> doInBackground(String... params) { 
    System.out.println("entra en el hilo"); 
    datos.clear(); 
    String next[] = {}; 
    List<String[]> list = new ArrayList<String[]>(); 
    try { 
     URL url = new URL(
       "http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT&f=nab2b3"); 
     InputStreamReader in = new InputStreamReader(url.openStream()); 
     CSVReader reader = new CSVReader(in); 
     for (;;) { 
      next = reader.readNext(); 
      if (next != null) { 
       list.add(next); 

      } else { 
       break; 
      } 
     } 
     datos = list; 

    } catch (IOException e) { 
     System.out.println(e); 
    } 
    try { 
     Thread.sleep(200);// 2000 es 2 segundos 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    System.out.println("sale del hilo"); 

    return list; 
} 

protected void onPostExecute(List<String[]> list) { 
    adaptador.clear(); 
    for(int i = 0; i < list.size(); i++) 
    { 
     adaptador.add(/*add each item*/); 
    } 
    adaptador.notifyDataSetChanged(); 
    System.out.println("onpostexecute"); 
    new MyLongTask().execute(); 
} 
} 

} 
0

我可以看到你的ArrayList的「VALORES」結合到ListView適配器。你完成下載新信息後,你想在ListView中顯示,在onPostExecute()(在UI線程)運行

valores.clear(); 
valores = (ArrayList) YOUR_NEW_VALUES; 
adaptador.notifyDataSetChanged(); 
其中YOUR_NEW_VALUES是一個ArrayList你的新雅虎財經信息在你的長期任務創建