我正在尝试从 yahoo Finance 下载一些数据并使用列表视图在屏幕上显示它们。我收集数据很好,我的问题是更新列表视图和删除数据古董数组。
在这里我留下我的代码:
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();
}
}
}