1

我正在尝试从 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();
    }
}

}
4

2 回答 2

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();
}
}

}
于 2013-05-13T17:21:17.410 回答
0

我可以看到您正在将 ArrayList 'valores' 绑定到 listview 适配器。完成下载要在 ListView 中显示的新信息后,在 onPostExecute()(在 UI 线程上)运行

valores.clear();
valores = (ArrayList) YOUR_NEW_VALUES;
adaptador.notifyDataSetChanged();

其中 YOUR_NEW_VALUES 是您从长期任务中的新雅虎财务信息创建的数组列表

于 2013-05-13T17:08:57.560 回答