0

私はアクティビティに2つのメソッドgetListIDsとgetNamesIDsを持っているので、AsycTaskクラスにはバックグラウンドでデータベースTow Listsから取得するプロセスがあり、最初のリストはその結果をgetListIDs()とアクティビティクラスに送信し、2番目のリストもgetNamesIDSに送信することになっています()

私の質問は: アクティビティ クラスの Two メソッドに使用される Async クラス 2 からこれらの 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 クラスが Parcelable でなければならないことを意味します。そして、使用しているリストは 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 に答える