gpt4 book ai didi

android - 使用 asyncTask 更新 ListView

转载 作者:行者123 更新时间:2023-11-29 21:43:17 25 4
gpt4 key购买 nike

我正在尝试从雅虎财经下载一些数据并使用 ListView 将它们显示在屏幕上。我很好地收集数据,我的问题是更新 ListView 和删除数据古董阵。

这里我留下我的代码:

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

}

最佳答案

像这样

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

}

关于android - 使用 asyncTask 更新 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16527341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com