从.net webservice到listview的Android数据集

时间:2012-12-02 09:52:07

标签: android dataset

我有一个.net webservice,它返回一个数据集。我想列出通过webservice返回的所有项目。我成功地从网络服务中获取它们但我在列表时遇到了一个小问题。这是我的代码。

public class RehberActivity extends Activity implements Runnable {

private static String SOAP_ACTION1 = "http://tempuri.org/GetNameSurname";
private static String NAMESPACE = "http://tempuri.org/";
private static String METHOD_NAME1 = "GetNameSurname";
private static String URL = "http://www.ogu.edu.tr/Service1.asmx";

Button btnSorgula;
EditText edtIsim, edtSoyisim;
TextView txtSonuc;
ListView lvSonuc;

public KisilerList[] kullanici;
public String[] ad;
public String[] soyadi;
public String[] dahili;
public String[] tel;
public String[] eposta;

public static ProgressDialog pd;

private ArrayList<HashMap<String, Object>> alKisi;
private static final String AdKey = "Ad";
private static final String SoyadKEY = "Soyadi";
private static final String DahiliKey = "Dahili";
private static final String TelKEY = "Tel";
private static final String EpostaKey = "Eposta";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rehber);

    btnSorgula = (Button) findViewById(R.id.btnSorgula);
    edtIsim = (EditText) findViewById(R.id.edtAd);
    edtSoyisim = (EditText) findViewById(R.id.edtSoyad);
    txtSonuc = (TextView) findViewById(R.id.txtSonuc);
    lvSonuc = (ListView) findViewById(R.id.listView1);

    Spinner spUnvanlar = (Spinner) findViewById(R.id.spUnvanlar);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            getApplicationContext(), R.array.unvanlar,
            android.R.layout.simple_spinner_item);

    spUnvanlar.setAdapter(adapter);

    Spinner spGorev = (Spinner) findViewById(R.id.spGorevYerleri);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
            getApplicationContext(), R.array.gorevyeri,
            android.R.layout.simple_spinner_item);

    spGorev.setAdapter(adapter2);

    btnSorgula.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            pd = ProgressDialog.show(RehberActivity.this,
                    "Lütfen Bekleyin...", "Rehber getiriliyor.", true, false);
            Thread thread = new Thread(RehberActivity.this);
            thread.start();

        }

    });
}

public void listPersonInfo() {
    serviceCall();
    // soru_Id=new int[soruArr.length];
    ad = new String[kullanici.length];
    soyadi = new String[kullanici.length];
    dahili = new String[kullanici.length];
    tel = new String[kullanici.length];
    eposta = new String[kullanici.length];

    for (int i = 0; i < kullanici.length; i++) {

        ad[i] = kullanici[i].getAd();
        soyadi[i] = kullanici[i].getSoyadi();
        dahili[i] = kullanici[i].getDahili();
        tel[i] = kullanici[i].getTel();
        eposta[i] = kullanici[i].getEposta();

    }
    // ---------------------------------------------------------------------
    alKisi = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> hm;
    for (int i = 0; i < kullanici.length; i++) {
        hm = new HashMap<String, Object>();
        // hm.put(idKEY, soru_Id[i]);
        hm.put(AdKey, ad[i]);
        hm.put(SoyadKEY, soyadi[i]);
        hm.put(DahiliKey, dahili[i]);
        hm.put(TelKEY, tel[i]);
        hm.put(EpostaKey, eposta[i]);

        alKisi.add(hm);
    }

    lvSonuc.setAdapter(new myListAdapter(alKisi, this));
    lvSonuc.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}

private void serviceCall() {
    // Initialize soap request + add parameters
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
    // Use this to add parameters
    request.addProperty("name", edtIsim.getText().toString());
    request.addProperty("surname", edtSoyisim.getText().toString());
    // Declare the version of the SOAP request
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.setOutputSoapObject(request);
    envelope.dotNet = true;

    try {

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        // this is the actual part that will call the
        // webservice
        androidHttpTransport.call(SOAP_ACTION1, envelope);
        // Get the SoapResult from the envelope body.
        SoapObject result = (SoapObject) envelope.getResponse();

        KisilerList[] kullaniciArr = new KisilerList[result
                .getPropertyCount()];

        for (int i = 0; i < result.getPropertyCount(); i++) {
            KisilerList objectResult = new KisilerList();
            SoapObject pii = (SoapObject) result.getProperty(i);
            // objectSoru.setSoru_id(Integer.parseInt(pii.getProperty(0).toString()));
            objectResult.setAd(pii.getProperty(0).toString());
            objectResult.setSoyadi(pii.getProperty(1).toString());
            objectResult.setDahili(pii.getProperty(2).toString());
            objectResult.setTel(pii.getProperty(3).toString());
            objectResult.setEposta(pii.getProperty(4).toString());

            kullaniciArr[i] = objectResult;

        }

        this.kullanici = kullaniciArr;

        // txtSonuc.setText(result.getProperty(0).toString()
        // + result.getProperty(1).toString());

    } catch (Exception e) {
        // SoapObject result = (SoapObject) envelope.bodyIn;
        // Log.d("isa", result.toString());
        e.printStackTrace();
    }

}
@Override
public void run() {

    listPersonInfo();
    pd.dismiss();
}

}

1 个答案:

答案 0 :(得分:0)

UI上拨打所有UI thread来电。您无法从后台线程更改UI。 我可以看到你的两个UI来电。如果你有其他的,那么也要改变它们

更改此

 @Override
public void run() {

    listPersonInfo();
    pd.dismiss();
}

@Override
public void run() {

    listPersonInfo();
    RehberActivity.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            pd.dismiss();

        }
    });
}

lvSonuc.setAdapter(new myListAdapter(alKisi, this));
lvSonuc.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

@Override
public void run() {

    listPersonInfo();
    RehberActivity.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            lvSonuc.setAdapter(new myListAdapter(alKisi, this));
            lvSonuc.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        }
    });
}