gpt4 book ai didi

android - 如何在另一个 Activity 的 ListView 中显示解析后的数据?

转载 作者:行者123 更新时间:2023-11-30 03:56:53 24 4
gpt4 key购买 nike

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

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

这是我的代码:

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();
}*/



}

最佳答案

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

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");

现在,您可以使用 contactRecords ArrayList for ListView Adapter 作为其他 Activity 的数据源

关于android - 如何在另一个 Activity 的 ListView 中显示解析后的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13207834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com