0

我在我的活动中有两个方法 getListIDs 和 getNamesIDs 所以在 AsycTask 类中有一些后台进程从数据库 Tow Lists 获取,第一个列表应该将其结果发送到 getListIDs() 和活动类,第二个列表也发送到 getNamesIDS ()

我的问题是:如何从 Async 类 2 中将这两个列表返回到活动类中的两个方法中?

我在活动类中的代码:

    public List<Student> getListIDs() {

    return list; // return full list 

    public List<Student> getNamesIDs() {

    return list; // return full list

我在 AsyncTask 类中的代码:

  public class Hashom extends AsyncTask<Void, Void, String> {

static Activity mActivity;
ProgressDialog progressDialog;

public Hashom(Activity activity, ProgressDialog progressDialog) {
    super();
    this.progressDialog = progressDialog;
    this.mActivity = activity;

}

final String NAMESPACE = "http://ws.sams.com";
final String URL = "http://88.198.82.92:8080/sams1/services/listActivityWS?WSDL"; 
                                                                                     localhost
final String METHOD_NAME = "getCurrentClassList";
final String SOAP_ACTION = "http://ws.sams.com/getCurrentClassList";

final String NAMESPACE2 = "http://ws.sams.com";
final String URL2 = "http://88.198.82.92:8080/sams1/services/listActivityWS?WSDL"; // usint
                                                                                    // localhost
final String METHOD_NAME2 = "getStudentId";
final String SOAP_ACTION2 = "http://ws.sams.com/getStudentId";

final String NAMESPACE3 = "http://ws.sams.com";
final String URL3 = "http://88.198.82.92:8080/sams1/services/listActivityWS?WSDL"; // usint
                                                                                    // localhost
final String METHOD_NAME3 = "getCourseName";
final String SOAP_ACTION3 = "http://ws.sams.com/getCourseName";

final String NAMESPACE4 = "http://ws.sams.com";
final String URL4 = "http://88.198.82.92:8080/sams1/services/listActivityWS?WSDL"; // usint
                                                                                    // localhost
final String METHOD_NAME4 = "getClassId";
final String SOAP_ACTION4 = "http://ws.sams.com/getClassId";

List<Student> StudentIdList = new ArrayList<Student>();
List<Student> StudentNamesList = new ArrayList<Student>();

@Override
protected String doInBackground(Void... voids) {




        SoapObject request3 = new SoapObject(NAMESPACE, METHOD_NAME);
        HttpTransportSE androidHttpTransport3 = new HttpTransportSE(URL);
        PropertyInfo pi3 = new PropertyInfo();
        pi3.setName("TID");
        pi3.setValue(1);
        pi3.setType(Integer.class);
        request3.addProperty(pi3);

        SoapSerializationEnvelope envelope3 = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);

        androidHttpTransport3.call(SOAP_ACTION, envelope3);

        KvmSerializable result = (KvmSerializable) envelope3.bodyIn;

        String str = null;
        for (int i = 0; i < result.getPropertyCount(); i++) {
            str = ((String) result.getProperty(i).toString());

            Student hesham = new Student(str);
            StudentNamesList.add(hesham);

        }
    } catch (Exception e) {

    }

    // ////////////////////////////////////////////////////////////////////////////////////

    SoapObject request4 = new SoapObject(NAMESPACE2, METHOD_NAME2);
    PropertyInfo pi4 = new PropertyInfo();
    pi4.setName("TID");
    pi4.setValue(1);
    pi4.setType(Integer.class);
    request.addProperty(pi4);
    SoapSerializationEnvelope envelope4 = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request4);
    HttpTransportSE androidHttpTransport4 = new HttpTransportSE(URL2);

    try {

        androidHttpTransport4.call(SOAP_ACTION2, envelope4);

        KvmSerializable result = (KvmSerializable) envelope.bodyIn;

        String str = null;
        for (int i = 0; i < result.getPropertyCount(); i++) {
            str = ((String) result.getProperty(i).toString());

            Student hesham = new Student(str);

            StudentIdList.add(hesham);

        }
    } catch (Exception ex) {
    }
    return "hh";  // i want here to return two lists (StudentIdList) and StudentNamesList and calling the reult into activity class 
}





@Override
protected void onPostExecute(String result) {

    Intent intent = new Intent(mActivity, ListActivity1.class);
    mActivity.startActivity(intent);

    progressDialog.dismiss();
}

  }
4

1 回答 1

0

您的 onPostExecute() 应如下所示:

public void onPostExecute()
{
    Intent intent = new Intent(mActivity, ListActivity1.class);
    intent.putParcelableArrayListExtra("STUDENT_IDS", StudentIdList);
    intent.putParcelableArrayListExtra("STUDENT_NAMES", StudentNamesList); 
    mActivity.startActivity(intent);
}

当然,这意味着您的 Student Class 必须是 Parcelable。您使用的 List 应该是 ArrayList

另外,您编写变量名的方式违反了标准的 Java 约定。只有类以大写字母开头(应该是 ArrayList studentNames)

在你的新活动中

Bundle b = getIntent().getExtras();
if(b != null)
{
    list1 = b.getParcelableArrayList<Student>("STUDENT_IDS");
    list2 = b.getParcelableArrayList<Student>("STUDENT_NAMES);
}

上面再次没有经过测试,但应该可以修复几个小错误。看看这里这里

于 2013-09-04T21:51:47.743 回答