2012-11-03 5 views
1

동일한 활동 내에서 ListView의 이름과 위치를 ksoap2를 통해 표시 할 수 있지만 다른 활동의 이름과 위치를 ListView에 표시하고 싶습니다.다른 Activity의 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에서 같은 활동의에서 onCreate 방법에 contactRecords을 같이 얻을 :

내 코드입니다
contactRecords = getIntent().getStringArrayListExtra("contactRecords_list"); 

이제 ContactRecords ArrayList for ListView Adapter를 사용할 수 있습니다. 다른 활동의 데이터 소스로

+0

첫 번째 activity.compiler에 putStringArrayListExtra를 사용하면 오류가 발생합니다. parceableArraylist를 넣으라고 제안합니다. – abh22ishek

+0

아니요. becuase 문자열 유형 ArrayList는 기본적으로 parceable이므로 String 형의 ArrayList를 전달하기 위해 putStringArrayListExtra를 사용해야하고, parceable 인터페이스를 구현하는 클래스의 커스텀 객체의 ArrayList가 있다면, ArrayList를 다른 클래스에 전달하기 위해 parceableArraylist를 사용해야 할 것입니다 –

관련 문제