1
回答
想当年,苦苦学习了android客户端制作,重点内容,数据异步处理,如今这些代码保存了下来,却都不认识了,倾囊传授给有需要的人
华为云4核8G,高性能云服务器,免费试用   

看170行的提示,网络数据加载中……,想当年,难倒了我几个通宵,现在问我我也不知道意思了,只是记录了代码。

package org.stu80;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

public class list extends Activity {
	/** Called when the activity is first created. */
	public static SoapObject soapObject;
	private static final String NAMESPACE = "http://tempuri.org/";
	private static String URL = "http://www.stu80.com/android/list.asmx";
	private static final String METHOD_NAME = "ListInfo";
	private static String SOAP_ACTION = "http://tempuri.org/ListInfo";
	private ListView ls;
	private ArrayList items_m;
	private Button bt1;
	private Button bt2;
	// private TextView list_id;
	private int num;
	private int x, y;
	public ProgressDialog mydialog = null;
	public int par;
	public ArrayList items;
	private int poss;
	private Handler handler = new Handler() {
		public void dispatchMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
			//	System.out.println("收到消息");
				mydialog.cancel();
			//	System.out.println("关闭提示");
				display(items);
			//	System.out.println("调用完毕");
				
				ls.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						TextView ls_id_i = (TextView) arg1
								.findViewById(R.id.ls_id);

						/*
						 * Toast.makeText(getApplicationContext(),
						 * ls_id_i.getText(), Toast.LENGTH_SHORT).show();
						 */
						Intent intent = new Intent();
						intent.setClass(list.this, content.class);
						Bundle bundle = new Bundle();
						bundle.putString("id", ls_id_i.getText().toString());
						intent.putExtras(bundle);
						startActivity(intent);
					}
				});
				
				bt1.setOnClickListener(new Button.OnClickListener() {
					public void onClick(View v) {
						if (num > 0) {
							num--;
						}
						display(items_m);
					}
				});
				bt2.setOnClickListener(new Button.OnClickListener() {
					public void onClick(View v) {
						if (num * 12 < items_m.size()) {
							num++;
						}
						display(items_m);
					}
				});

				// 添加Spinner事件监听
				Spinner sp = (Spinner) findViewById(R.id.sp);
				ArrayAdapter ada = ArrayAdapter.createFromResource(list.this,
						R.array.list, android.R.layout.simple_spinner_item);
				ada.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				sp.setAdapter(ada);
				// 定义子元素选择监听器
				sp.setSelection(0, false);
				sp.setOnItemSelectedListener(new OnItemSelectedListener() {
					@Override
					public void onItemSelected(AdapterView<?> parent,
							View view, int position, long id) {
						// Toast.makeText(Stu80Activity.this,"选择的色彩: " +
						// parent.getItemAtPosition(position).toString(),
						// Toast.LENGTH_LONG).show();
						String pos = parent.getItemAtPosition(position)
								.toString();
						// TextView tv=(TextView) findViewById(R.id.logotitle);
						// tv.setText(pos);
						pos = pos.substring(0, 2);
						jump(pos);
					}

					@Override
					public void onNothingSelected(AdapterView<?> parent) {
					}
				});
				break;

			default:
				break;
			}
		};
	};

	private Handler handler2 = new Handler() {
		public void dispatchMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
			//	System.out.println("收到消息");
				mydialog.cancel();
			//	System.out.println("关闭提示");
				display(items);
				break;

			default:
				break;
			}
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ls = (ListView) findViewById(R.id.ls);
		bt1 = (Button) findViewById(R.id.bt1);
		bt2 = (Button) findViewById(R.id.bt2);
		Bundle bundle = this.getIntent().getExtras();
		String param = bundle.getString("param");
		// Toast.makeText(list.this,"123", Toast.LENGTH_LONG).show();
		par = Integer.parseInt(param);

		mydialog = ProgressDialog.show(list.this, "(∩_∩)", "网络数据加载中...", true);
		new Thread() {
			public void run() {
				try {
					// 涉及到网络访问的操作,用多线程来实现
					items = getItems(par);
					items_m = items;
				} catch (Exception e) {
					e.printStackTrace();
				}
				handler.sendEmptyMessage(0);
			}
		}.start();

	}

	public void display(ArrayList items) {
	//	System.out.println("开始调用");
		if (items == null)
			return;
	//	System.out.println("items.size()=" + items.size());

		if (num * 12 > items.size()) {
			x = items.size();
		} else {
			x = num * 12;
		}
		if (12 + num * 12 > items.size()) {
			y = items.size();
		} else {
			y = 12 + num * 12;
		}
		ArrayList items_tmp = new ArrayList(items.subList(x, y));
		SimpleAdapter adapter = new SimpleAdapter(this, items_tmp,
				R.layout.simple_expandable_list_item_1, new String[] { "ls_id",
						"ls_title", "ls_name", "ls_time", "ls_num" },
				new int[] { R.id.ls_id, R.id.ls_title, R.id.ls_name,
						R.id.ls_time, R.id.ls_num });

		// 位ListView设置Adapter
		ls.setAdapter(adapter);

	}

	public void jump(String pos) {
		// String pos=java.net.URLDecoder.decode(poss);
		// Toast.makeText(Stu80Activity.this,"选择的pos: " +
		// pos, Toast.LENGTH_LONG).show();
		poss = Integer.parseInt(pos);
	
		mydialog = ProgressDialog.show(list.this, "(∩_∩)", "网络数据加载中...", true);
		new Thread() {
			public void run() {
				try {
					// 涉及到网络访问的操作,用多线程来实现
					items = getItems(poss);
					items_m = items;
				} catch (Exception e) {
					e.printStackTrace();
				}
				handler2.sendEmptyMessage(0);
			}
		}.start();
		
	}

	public ArrayList getItems(int pos) {
		num = 0;
		ArrayList items = new ArrayList();
		try {

			soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
			soapObject.addProperty("param", pos);
			AndroidHttpTransport ht = new AndroidHttpTransport(URL);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);

			envelope.bodyOut = soapObject;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(soapObject);

			ht.call(SOAP_ACTION, envelope);

			soapObject = (SoapObject) envelope.bodyIn;
			// soapObject = (SoapObject) envelope.getResponse();
			Object result = (Object) soapObject.getProperty(METHOD_NAME
					+ "Result");
			Document document = DocumentHelper.parseText(result.toString());
			Element root = document.getRootElement();
			Iterator iterator = root.elementIterator();
			while (iterator.hasNext()) {
				HashMap map = new HashMap();
				Element element = (Element) iterator.next();
				if (element.getName().equals("list")) {
					map.put("ls_id", element.element("id").getText());
					map.put("ls_title", element.element("title").getText());
					map.put("ls_name", "by: "
							+ element.element("name").getText());
					map.put("ls_time", element.element("time").getText());
					if (element.element("num").getText() == ""
							|| element.element("num").getText() == null) {
						map.put("ls_num", "[0]");
					} else {
						map.put("ls_num", "["
								+ element.element("num").getText() + "]");
					}
					items.add(map);
				}
			}
			// tx.setText(s);

		} catch (Exception e) {
			e.printStackTrace();
			// tx.setText(""+e+"");
		}
		return items;
	}
}

<无标签>
举报
顶部