Java Android-从AsyncTask结果获取对象
我想在一个AsyncTask中创建10个Employee对象,并将结果返回给MainActivity类,以便在具有Employee对象3个属性的ListView上打印它 这是我到目前为止所做的,但它只是在运行后崩溃 菜单类Java Android-从AsyncTask结果获取对象,java,android,object,Java,Android,Object,我想在一个AsyncTask中创建10个Employee对象,并将结果返回给MainActivity类,以便在具有Employee对象3个属性的ListView上打印它 这是我到目前为止所做的,但它只是在运行后崩溃 菜单类 public class Menu { public Employee person; public void onButtonClick(View v) { new setEMPInfo() { protected
public class Menu
{
public Employee person;
public void onButtonClick(View v) {
new setEMPInfo() {
protected void onPostExecute(Employee person)
{
doSomething(person);
}
}.execute();
}
public void doSomething(Employee person) {
//use person object to print on TextView
}
}
setEMPInfo类
public class setEMPInfo extends AsyncTask<Void, Void, Employee>
{
Public Employee person;
protected Bus doInBackground(String... params) {
String id = "100A";
String Fname = "John";
String Lname = "Smith";
for (int i = 0; i < 10; i++) {
person = new Employee(id, Fname, Lname);
}
return person;
}
}
公共类setEMPInfo扩展异步任务
{
公职人员;
受保护总线doInBackground(字符串…参数){
字符串id=“100A”;
字符串Fname=“John”;
字符串Lname=“Smith”;
对于(int i=0;i<10;i++){
人员=新员工(id、Fname、Lname);
}
返回人;
}
}
这里有一个完整的异步任务示例(由google docs提供)
私有类下载文件任务扩展异步任务{
受保护的长doInBackground(URL…URL){
int count=url.length;
长totalSize=0;
for(int i=0;i
例如,您可以在调用者类中创建一个静态对象,并在onPostExecute中对其进行更改。当您的
doInBackground
完成时,它会向onPostExecute
返回一些值。然后,您可以使用此方法执行任何操作(保存在数据库中、保存在SD卡中等) 问题在于您的类setEMPInfo
扩展了AsyncTask
而不是AsyncTask
修复方法是更改(在setEMPInfo
类中)
到
及
Menu.java
public class SetEMPInfo extends AsyncTask<String, Void, Employee>
{
public Employee person;
@Override
protected Employee doInBackground(String... params)
{
String id = "100A";
String Fname = "John";
String Lname = "Smith";
person = new Employee(id, Fname, Lname);
return person;
}
}
public class Menu
{
public Employee person;
public void onButtonClick(View v)
{
new SetEMPInfo()
{
@Override
protected void onPostExecute(Employee employee)
{
doSomething(employee);
}
}.execute();
}
public void doSomething(Employee person)
{
//use person object to print on TextView
}
}
试试这个:
public class TestActivity extends Activity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = (ListView) findViewById(R.id.list_item);
setEMPInfo task = new setEMPInfo().execute();
}
private class setEMPInfo extends AsyncTask<Void, Void, ArrayList<Employee>> {
@Override
protected ArrayList<Employee> doInBackground(Void... params) {
String id = "100A";
String Fname = "John";
String Lname = "Smith";
ArrayList<Employee> employees = new ArrayList<>();
for (int i = 0; i < 10; i++) {
person = new Employee(id, Fname, Lname);
employees.add(person);
}
return employees;
}
@Override
protected void onPostExecute( ArrayList<Employee>result)
//print it on a ListView
list.setAdapter(new YourAdapret(getApplicationContext(), result));
}
}
公共类测试活动扩展活动{
列表视图列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
list=(ListView)findViewById(R.id.list\u项);
setEMPInfo任务=新建setEMPInfo().execute();
}
私有类setEMPInfo扩展了异步任务{
@凌驾
受保护的ArrayList doInBackground(无效…参数){
字符串id=“100A”;
字符串Fname=“John”;
字符串Lname=“Smith”;
ArrayList employees=新的ArrayList();
对于(int i=0;i<10;i++){
人员=新员工(id、Fname、Lname);
员工。添加(人);
}
返回员工;
}
@凌驾
受保护的void onPostExecute(ArrayListresult)
//在列表视图上打印它
setAdapter(新的YourAdapret(getApplicationContext(),result));
}
}
}试试这个
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new setEMPInfo() {
protected void onPostExecute(Employee person) {
doSomething(person);
}
}.execute();
}
}
);
此处仅用于测试将所有员工添加到列表中-
public void doSomething(Employee person) {
eList.add(person);
Log.e("Emp->", eList.toString());
}
您的异步任务应该如下所示-
class setEMPInfo extends AsyncTask<Employee, Void, Employee> {
Employee person;
@Override
protected Employee doInBackground(Employee... params) {
String id = "100A";
String Fname = "John";
String Lname = "Smith";
for (int i = 0; i < 10; i++) {
person = new Employee(id, Fname, Lname);
}
return person;
}
}
类setEMPInfo扩展了异步任务{
员工个人;
@凌驾
受保护的员工doInBackground(员工…参数){
字符串id=“100A”;
字符串Fname=“John”;
字符串Lname=“Smith”;
对于(int i=0;i<10;i++){
人员=新员工(id、Fname、Lname);
}
返回人;
}
}
使用onPostExecute
从doInBackground
什么是doSomething?您的setEMPInfo
没有接受Employee
对象的构造函数。发布您正在使用的代码已将我的帖子更新为使用onPostExecute
,但它刚刚崩溃您正在返回ArrayList
,但您正在扩展AsyncTask
。您应该扩展AsyncTask
<代码>PostExecute时受保护的无效(员工结果)应变为@重写PostExecute时受保护的无效(ArrayListresult)
谢谢dude;)这看起来像是用错误来纠正错误)我应该留心
public class Menu
{
public Employee person;
public void onButtonClick(View v)
{
new SetEMPInfo()
{
@Override
protected void onPostExecute(Employee employee)
{
doSomething(employee);
}
}.execute();
}
public void doSomething(Employee person)
{
//use person object to print on TextView
}
}
public class TestActivity extends Activity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = (ListView) findViewById(R.id.list_item);
setEMPInfo task = new setEMPInfo().execute();
}
private class setEMPInfo extends AsyncTask<Void, Void, ArrayList<Employee>> {
@Override
protected ArrayList<Employee> doInBackground(Void... params) {
String id = "100A";
String Fname = "John";
String Lname = "Smith";
ArrayList<Employee> employees = new ArrayList<>();
for (int i = 0; i < 10; i++) {
person = new Employee(id, Fname, Lname);
employees.add(person);
}
return employees;
}
@Override
protected void onPostExecute( ArrayList<Employee>result)
//print it on a ListView
list.setAdapter(new YourAdapret(getApplicationContext(), result));
}
}
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new setEMPInfo() {
protected void onPostExecute(Employee person) {
doSomething(person);
}
}.execute();
}
}
);
public void doSomething(Employee person) {
eList.add(person);
Log.e("Emp->", eList.toString());
}
class setEMPInfo extends AsyncTask<Employee, Void, Employee> {
Employee person;
@Override
protected Employee doInBackground(Employee... params) {
String id = "100A";
String Fname = "John";
String Lname = "Smith";
for (int i = 0; i < 10; i++) {
person = new Employee(id, Fname, Lname);
}
return person;
}
}