gpt4 book ai didi

android - AsyncTask 和 listActivity

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

我做了这个简单的应用程序,在这个应用程序中,当我点击开始按钮时,我想在 Listview 中查看数据,但它不起作用。你能帮帮我吗?

ma​​inActivity.java

public class MainActivity extends ListActivity {
public ArrayList<Map<String, String>> list= new ArrayList<Map<String, String>>();
public ListAdapter adapter;
public ProgressDialog progressDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button buttonAggiorna = (Button) findViewById(R.id.buttonStart);
buttonAggiorna.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {

String[] from = { "name", "purpose" };
int[] to = { android.R.id.text1, android.R.id.text2 };
adapter = new SimpleAdapter(MainActivity.this, list,android.R.layout.simple_list_item_2, from, to);

progressDialog = ProgressDialog.show(MainActivity.this, "Getting data", "Loading...");
taskBack t=new taskBack();
t.execute();
}

});// fine onClickListner


Button buttonAllarm = (Button) findViewById(R.id.buttonAllarm);
buttonAllarm.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
AppLog.logString("list size:"+list.size());

}
});


}

private class taskBack
extends AsyncTask<Void, Void, ArrayList<Map<String, String>>> {

@Override
protected ArrayList<Map<String, String>> doInBackground(Void... arg0) {
ArrayList<Map<String, String>> l = new ArrayList<Map<String, String>>();
l = startEla();
return l;
}

protected void onPostExecute(ArrayList<Map<String, String>> result) {
AppLog.logString("Parte onPostExecute");
list=result;
setListAdapter(adapter);
progressDialog.dismiss();

}

private ArrayList<Map<String, String>> startEla() {
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(putData("android","mobile"));
list.add(putData("Windows7", "Windows7"));
list.add(putData("iPhone", "iPhone"));
return list;

}

private Map<String, String> putData(String name, String purpose) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("name", name);
item.put("purpose", purpose);
return item;

}
}
}

在此链接中,您可以看到 xml 文件 enter link description here

最佳赛程 A.

最佳答案

将这两行移动到 ListActivity 的主体...

String[] from = { "name", "purpose" };
int[] to = { android.R.id.text1, android.R.id.text2 };

onCreate(...)... 中的 onClick(...) 监听器中删除以下行

adapter = new SimpleAdapter(MainActivity.this, list, android.R.layout.simple_list_item_2, from, to);

...并把它放在AsyncTaskonPostExecute(...)方法中,如下...

protected void onPostExecute(ArrayList<Map<String, String>> result) {
AppLog.logString("Parte onPostExecute");
list=result;
adapter = new SimpleAdapter(MainActivity.this, list, android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
progressDialog.dismiss();
}

关于android - AsyncTask 和 listActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12045051/

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