1

我可以ListView通过 ksoap2 在同一活动中显示名称和位置,但我想在 a 中显示另一个活动中的名称和位置ListView

我不知道该怎么做。有人可以指导我吗?

这是我的代码:

package com.example.rotaryclubnew;
import java.util.ArrayList;
import java.util.List;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;



import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class Searchcontactswithposition extends Activity implements OnClickListener{


    EditText name1,pos;
    Button searching;


    public static String SOAP_ACTION1 = "http://tempuri.org/Search";

   public static String NAMESPACE = "http://tempuri.org/";

    public static String METHOD_NAME2 = "Search";

    private static String URL = "http://115.119.182.114/Rotaryclub/RotaryService.asmx"; 

    TextView txtdata;
    ListView lv;
     List<String> contactRecords = new ArrayList<String>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchcontactswithposition);
        lv=(ListView) findViewById(R.id.listView1);
        name1=(EditText) findViewById(R.id.editText1withname);
        pos=(EditText)findViewById(R.id.editText1withposition);

        searching=(Button) findViewById(R.id.button2search);
        searching.setOnClickListener(this);

        //txtdata=(TextView) findViewById(R.id.textViewtodisplayname);

        //abs();
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    /*  Intent i = new Intent(Searchcontactswithposition.this,Displaydetails.class);
        Bundle extras = new Bundle();
        extras.putString("Nameofperson", name1.getText().toString());
        extras.putString("Positionofcontacts", pos.getText().toString());
        i.putExtras(extras);
        startActivity(i);*/




             Thread networkThread = new Thread() {
                    @Override
                    public void run() {
                      try {
                         SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);     

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


                         HttpTransportSE ht = new HttpTransportSE(URL);
                        // ht.call(SOAP_ACTION1, envelope);
                         if( name1.getText().toString().equals("")&&pos.getText().toString().equals("")){

                            Toast.makeText(Searchcontactswithposition.this, "please enter details", 3000).show();
                                /*Intent i=new Intent(Searchcontactswithposition.this, Rotary_main.class);
                                startActivity(i);*/
                         }
                         request.addProperty("Name",name1.getText().toString());//pass the parameters
                         request.addProperty("Position",pos.getText().toString());

                         envelope.setOutputSoapObject(request);
                         ht.call(SOAP_ACTION1, envelope);


                      final SoapObject result = (SoapObject)envelope.getResponse();

        runOnUiThread (new Runnable()
                   { 
                     public void run() 
                     {


                         int count=result.getPropertyCount();
                         System.out.println("in main count "+count);
                         for (int i = 0; i <count; i++) 
                          {
                              SoapObject result1=(SoapObject) result.getProperty(i);//get the result 
                              if(result!=null)
                              {
                              contactRecords.add("Name "+result1.getProperty(2).toString()+"\n"+"Position "+result1.getProperty(1).toString());
                              name1.setText("");
                              pos.setText("");

                             System.out.println("in for loop "+"Name "+result1.getProperty(2).toString()+"\n"+"Position "+result1.getProperty(1).toString());
                          }

                          }
                      }
                   });
                   }
                    catch (Exception e) {
                         e.printStackTrace();
                     }
                     handler.sendEmptyMessage(0);//call the handle to display the data in a listview

                    }
                  };
                            /*lv.setOnItemClickListener(new OnItemClickListener() {
                              @Override
                              public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

                                // you can do ur work according to selected string or index here... 
                             Toast.makeText(getApplicationContext(),lv.getAdapter().getItem(position)+" is selected", Toast.LENGTH_LONG).show();


                             }

                            });*/

                  networkThread.start();//start the network thred




    }

    private Handler handler = new Handler()
        {
            @SuppressLint("HandlerLeak")
            @Override
            public void handleMessage(Message msg)
            {

                ArrayAdapter<String> aa;
            aa = new ArrayAdapter<String>(Searchcontactswithposition.this, android.R.layout.simple_list_item_1,contactRecords);
            lv.setAdapter(aa);
           // aa.clear();

            }
        };

    /*public void abs()
    {

        if( name1.getText().toString().equals("")||pos.getText().toString().equals("")){

                Intent i=new Intent(Searchcontactswithposition.this, Rotary_main.class);
                startActivity(i);

            Toast.makeText(this, "please enter details",4000).show();   
            }*/



    }
4

1 回答 1

1

只需使用 Intent 将 contactRecords ArrayList 传递给另一个活动:

Intent intent = new Intent(Searchcontactswithposition.this, Other_Activity.class);
intent.putStringArrayListExtra("contactRecords_list", contactRecords);
startActivity(intent);

并在 Other_Activity 中获取contactRecords,就像在 Activity 的 onCreate 方法中一样:

contactRecords = getIntent().getStringArrayListExtra("contactRecords_list");

现在,您可以使用 listView Adapter 的 contactRecords ArrayList 作为其他活动的数据源

于 2012-11-03T09:43:22.967 回答