2012-11-03 31 views
1

我可以通過ksoap2在同一活動中顯示ListView中的名稱和位置,但我想在ListView的另一個活動中顯示名稱和位置。如何在另一個活動的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(); 
      }*/ 



    } 

回答

1

使用意向的只是傳遞contactRecords的ArrayList到另一個活動:

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

和Other_Activity得到contactRecords在活動的onCreate方法爲:

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

現在,您可以將contactRecords ArrayList用於ListView適配器作爲您的其他活動的數據源

+0

我得到一個錯誤,如果我在我的第一個activity.compiler中使用putStringArrayListExtra是建議我把parceableArraylist – abh22ishek

+0

沒有這是不可能的becuase字符串類型ArrayList默認parceable,所以你只是需要使用putStringArrayListExtra來傳遞String類型的ArrayList,並且如果你有一個實現了parceable接口的類的自定義對象的ArrayList,那麼你將需要使用parceableArraylist來傳遞ArrayList到另一個類 –

相關問題