您现在的位置是:网站首页> Android
常用界面XML
- Android
- 2022-04-12
- 840人已阅读
常用界面XML
splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
splash.java
public class splash extends Activity implements Runnable {
/** Called when the activity is first created. */
private Handler mHandler = new Handler(){
public void handleMessage(Message msg)
{
switch (msg.what)
{
default:
{
Intent iMain = new Intent(splash.this,login.class);
startActivity (iMain);
finish();
}
break;
}
};
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.splash);
this.getWindow().setBackgroundDrawable(drawable);
Thread m_Thread=new Thread(this);
m_Thread.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Message message = new Message();
message.what = 1;
mHandler.sendMessage(message);
}
}
login.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bkcolor"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/bkcolor"
>
<ImageView android:id="@+id/dxlg" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:src="@drawable/dxlg"
android:layout_marginTop="25.0dip"
/>
<EditText android:id="@+id/editUsername" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="15.0dip" android:layout_marginTop="25.0dip" android:layout_marginRight="15.0dip" android:hint="输入登录名" android:layout_alignParentLeft="true" android:layout_alignWithParentIfMissing="false" android:inputType="text" android:isScrollContainer="false" android:layout_below="@id/dxlg"/>
<EditText android:id="@+id/editPassword" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="9.0dip" android:hint="输入口令" android:layout_below="@id/editUsername" android:layout_alignLeft="@id/editUsername" android:layout_alignRight="@id/editUsername" android:inputType="textPassword" android:isScrollContainer="false" />
<CheckBox
android:id="@+id/checkRmPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15.0dip"
android:text="记住口令"
android:layout_below="@id/editPassword"
android:layout_alignLeft="@id/editPassword"
android:textColor="@color/forcolor"
/>
<Button android:textSize="16.0dip"
android:id="@+id/buttonLogin"
android:layout_width="90.0dip"
android:layout_height="45.0dip"
android:layout_marginTop="15.0dip"
android:text="登录"
android:layout_below="@id/checkRmPassword"
android:layout_alignLeft="@id/checkRmPassword" />
<Button android:textSize="16.0dip" android:id="@+id/buttonCancel"
android:layout_width="90.0dip" android:layout_height="45.0dip" android:text="取消" android:layout_toRightOf="@id/buttonLogin" android:layout_alignTop="@id/buttonLogin" />
<Button android:textSize="16.0dip" android:id="@+id/buttonUpdate" android:layout_width="90.0dip" android:layout_height="45.0dip" android:text="升级" android:layout_toRightOf="@id/buttonCancel" android:layout_alignTop="@id/buttonLogin"
/>
</RelativeLayout>
</ScrollView>
login.java
public class login extends BaseHttpActivity{
public android.view.View.OnClickListener handler=null;
public EditText m_userID=null,m_userPwd=null;
public CheckBox m_CheckBox=null;
private iniInfo m_iniInfo=new iniInfo();
private XNini m_XNini=new XNini();
wlbJson m_wlbJson=new wlbJson();
public boolean bPost=false;
private Handler mHandler = new Handler(){
public void handleMessage(Message msg)
{
switch (msg.what)
{
default:
{
}
break;
}
};
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
m_XNini.m_ParentActivity=this;
m_XNini.ReadIni(m_iniInfo);
handler = new View.OnClickListener()
{
public void onClick(View v)
{
switch (v.getId())
{
case R.id.buttonLogin:
{
LG();
}
break;
case R.id.buttonCancel:
{
android.os.Process.killProcess(android.os.Process.myPid());
}
break;
case R.id.buttonUpdate:
TrySoft();
break;
}
}
};
findViewById(R.id.buttonLogin).setOnClickListener(handler);
findViewById(R.id.buttonCancel).setOnClickListener(handler);
findViewById(R.id.buttonUpdate).setOnClickListener(handler);
m_userID=(EditText)findViewById(R.id.editUsername);
m_userPwd=(EditText)findViewById(R.id.editPassword);
// m_iniInfo.sUserName="中文测试";
m_userID.setText(m_iniInfo.sUserName);
m_CheckBox=(CheckBox)findViewById(R.id.checkRmPassword);
if(m_iniInfo.bMark)
{
m_userPwd.setText(m_iniInfo.sUserPWD);
m_CheckBox.setChecked(true);
//LG();
}
else
{
}
/*
NetConnect m_NetConnect=new NetConnect();
m_NetConnect.act=this;
this.DisplayToast(m_NetConnect.GetNetType());
m_NetConnect=null;
*/
/*
findViewById(R.id.buttonLogin).setFocusable(true);
findViewById(R.id.buttonLogin).requestFocus(0);
*/
Button m_LGBT=(Button)findViewById(R.id.buttonLogin);
m_LGBT.setFocusable(true);
m_LGBT.setFocusableInTouchMode(true);
m_LGBT.requestFocus();
m_LGBT.requestFocusFromTouch();
}
public void LG()
{
String sName=m_userID.getText().toString();
String sPWD=m_userPwd.getText().toString();
if(sName.equals(""))
{
DisplayToast("请输入登录用户名");
this.m_userID.requestFocus();
return;
}
if(sPWD.equals(""))
{
DisplayToast("请输入登录口令");
this.m_userPwd.requestFocus();
return;
}
m_iniInfo.sUserName=sName;
m_iniInfo.sUserPWD=sPWD;
m_iniInfo.bMark=m_CheckBox.isChecked();
if(!m_iniInfo.bMark)
m_iniInfo.sUserPWD="";
m_XNini.WriteIni(m_iniInfo);
String sUrl=getString(R.string.url);
if(bPost)
{
String sData=m_wlbJson.PackLG(sName, sPWD);
PostHttp(sUrl+"wlb.aspx",sData,"正在登录请稍候...",new onOutAppData()
{
@Override
public void PutAppData(boolean bOK, JSONObject O) {
// TODO Auto-generated method stub
if(O!=null)
{
JSONObject m_userInfo=new JSONObject();
if(m_wlbJson.UnPackLG(O,m_userInfo))
{
try {
String sError=m_userInfo.getString("error");
DisplayToast(sError);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
m_OtherObject=m_userInfo;
Intent iMain = new Intent(login.this,xjwlb.class);
startActivity (iMain);
finish();
}
}
else
DisplayToast(O.toString());
}
else
{
DisplayToast("由于网络原因登陆失败");
}
}
});
}
else
{
sUrl=getString(R.string.neturl);
sUrl=m_wlbJson.PackLGUrl(sUrl,sName,sPWD);
GetHttp(sUrl,"正在登录请稍候...",new onOutAppData()
{
@Override
public void PutAppData(boolean bOK, JSONObject O) {
// TODO Auto-generated method stub
if(O!=null)
{
JSONObject m_userInfo=new JSONObject();
if(m_wlbJson.UnPackLG(O,m_userInfo))
{
m_OtherObject=m_userInfo;
String sError="";
try {
sError=m_userInfo.getString("error");
DisplayToast(sError);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Intent iMain = new Intent(login.this,xjwlb.class);
startActivity (iMain);
finish();
}
}
else
DisplayToast(O.toString());
}
else
{
DisplayToast("由于网络原因登陆失败");
}
}
});
}
}
public void TrySoft()
{
UpdateSoft m_UpdateSoft=new UpdateSoft();
m_UpdateSoft.m_ParentActivty=this;
if(bPost)
m_UpdateSoft.PostVer();
else
m_UpdateSoft.GetVer(getString(R.string.updateurl));
}
}
UpdateSoft.java
public class UpdateSoft {
public Activity m_ParentActivty=null;
public ProgressDialog pBar;
private Handler handler = new Handler();
public String sUpdateUrl;
public boolean bShow=true;
public void PostVer()
{
if(m_ParentActivty==null)
return;
PackageManager manager = m_ParentActivty.getPackageManager();
try {
PackageInfo info = manager.getPackageInfo(m_ParentActivty.getPackageName(), 0);
String appVersion = info.versionName; // 版本名
final int nVer=info.versionCode;
final String sUrl=m_ParentActivty.getString(R.string.url)+"wlb.aspx";
new Thread(){
public void run()
{
RealPostVer(sUrl,nVer);
}
}.start();
}
catch (NameNotFoundException e)
{ // TODO Auto-generated catch block
e.printStackTrace();
}
}
public void InitUpdate(String FName)
{
final String TFName=FName;
final String sUrl=m_ParentActivty.getString(R.string.url);
Dialog dialog =
new AlertDialog.Builder(m_ParentActivty).setTitle("系统更新")
.setMessage("发现新版本,请更新!")// 设置内容
.setPositiveButton("确定",// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
pBar =
new ProgressDialog(m_ParentActivty);
pBar.setTitle("正在下载");
pBar.setMessage("请稍候...");
pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
downFile(sUrl+TFName);
}
}).setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 点击"取消"按钮之后退出程序
}
}).create();// 创建
// 显示对话框
dialog.show();
}
public void InitGetUpdate(String FName)
{
final String sUrl=FName;
Dialog dialog =
new AlertDialog.Builder(m_ParentActivty).setTitle("系统更新")
.setMessage("发现新版本,请更新!")// 设置内容
.setPositiveButton("确定",// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
pBar =
new ProgressDialog(m_ParentActivty);
pBar.setTitle("正在下载");
pBar.setMessage("请稍候...");
pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
downFile(sUrl);
System.out.println(sUrl);
}
}).setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 点击"取消"按钮之后退出程序
}
}).create();// 创建
// 显示对话框
dialog.show();
}
void downFile(final String url) {
pBar.show();
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
// params[0]代表连接的url
HttpGet get=new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is != null)
{
File file=new File(Environment.getExternalStorageDirectory(), "XJWLB.apk");
fileOutputStream =new FileOutputStream(file);
byte[] buf =new byte[1024];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) !=-1)
{
fileOutputStream.write(buf, 0, ch);
count += ch;
if (length >0)
{
}
}
}
fileOutputStream.flush();
if (fileOutputStream !=null)
{
fileOutputStream.close();
}
down();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
void down() {
handler.post(new Runnable() {
public void run() {
pBar.cancel();
update();
}
});
}
void update()
{
Intent intent =
new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/sdcard/XJWLB.apk")),
"application/vnd.android.package-archive");
m_ParentActivty.startActivity(intent);
}
public String PackVer(int nVer)
{
// 先封装一个 JSON 对象
JSONObject Main=new JSONObject();
JSONObject param = new JSONObject();
try {
Main.put("fname","UpdateSoft");
param.put("ver", nVer);
Main.put("param",param);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Main.toString();
}
public boolean RealPostVer(String sUrl,int nVer)
{
HttpClient httpclient=null;
httpclient=new DefaultHttpClient();
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
String sData=PackVer(nVer);
HttpPost httpPost=new HttpPost(sUrl);
StringEntity entity2;
try {
entity2 = new StringEntity(sData,HTTP.UTF_8);
httpPost.setEntity(entity2);
System.out.println("start开始Post");
HttpResponse httpResponse = httpclient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
System.out.println("Login form get: " + httpResponse.getStatusLine());
System.out.println("end post");
if (entity != null)
{
String result = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
JSONObject O=null;
try {
O = new JSONObject( result);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
HaveNewVer(false,"");
return false;
}
boolean bUpdate=false;
String FName;
try {
Object oo=O.get("bUpdate");
bUpdate =false;
if(oo!=null)
{
bUpdate=((Boolean)oo).booleanValue();
}
FName=(String)O.get("FileName");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
HaveNewVer(false,"");
return false;
}
if(bUpdate)
HaveNewVer(true,FName);
else
HaveNewVer(false,"");
return true;
}
else
{
HaveNewVer(false,"");
return false;
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
HaveNewVer(false,"");
return false;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
HaveNewVer(false,"");
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
HaveNewVer(false,"");
return false;
}
}
public void HaveNewVer(boolean bHave,String FName)
{
final String TFName=FName;
if(bHave)
{
handler.post(new Runnable() {
public void run() {
InitGetUpdate(TFName);
}
});
}
else
{
handler.post(new Runnable() {
public void run() {
if(bShow)
Toast.makeText(m_ParentActivty, "未发现新版本", Toast.LENGTH_SHORT).show();
}
});
}
}
public void GetVer(String sUpUrl)
{
if(m_ParentActivty==null)
return;
PackageManager manager = m_ParentActivty.getPackageManager();
try {
PackageInfo info = manager.getPackageInfo(m_ParentActivty.getPackageName(), 0);
String appVersion = info.versionName; // 版本名
final int nVer=info.versionCode;
final String sUrl=sUpUrl;//m_ParentActivty.getString(R.string.url)+"wlb.aspx";
new Thread(){
public void run()
{
RealGetVer(sUrl,nVer);
}
}.start();
}
catch (NameNotFoundException e)
{ // TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean RealGetVer(String sUrl,int nVer)
{
HttpClient httpclient=null;
httpclient=new DefaultHttpClient();
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
String sData=PackVer(nVer);
HttpGet httpget = new HttpGet(sUrl);
StringEntity entity2;
try {
System.out.println("start开始HTTP");
HttpResponse httpResponse = httpclient.execute(httpget);;
HttpEntity entity = httpResponse.getEntity();
System.out.println("Login form get: " + httpResponse.getStatusLine());
System.out.println("end post");
if (entity != null)
{
String result = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
JSONObject O=null;
try {
O = new JSONObject( result);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
HaveNewVer(false,"");
return false;
}
boolean bUpdate=false;
String FName;
try {
int nTVer=O.getInt("version");
bUpdate =false;
if(nVer<nTVer)
{
bUpdate=true;
}
FName=(String)O.get("update_apk");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
HaveNewVer(false,"");
return false;
}
if(bUpdate)
HaveNewVer(true,FName);
else
HaveNewVer(false,"");
return true;
}
else
{
HaveNewVer(false,"");
return false;
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
HaveNewVer(false,"");
return false;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
HaveNewVer(false,"");
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
HaveNewVer(false,"");
return false;
}
}
}
BaseHttpActivity.java
public class BaseHttpActivity extends Activity {
private static XNWebApplication m_XNWebApplication=new XNWebApplication();
public static XYPack m_XYPack=new XYPack();
public static Object m_OtherObject=null;
private Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what)
{
default:
{
ReturnData m_ReturnData=(ReturnData)msg.obj;
if(m_ReturnData!=null)
{
m_ReturnData.m_onOutAppData.PutAppData(m_ReturnData.bOK,m_ReturnData.m_JSONObject);
}
}
break;
}
};
};
public void GetHttp(String sUrl,String sTiShi,onOutAppData m_onOutAppData)
{
//String sUrl="http://192.168.20.215/Test.aspx";
final onOutAppData m_onOutAppData0=m_onOutAppData;
ProgressDialog pdg=null;
if(sTiShi.equals(""))
{
}
else
{
pdg=ShowWait("提示",sTiShi);
pdg.show();
}
final ProgressDialog pdialog=pdg;
//final ProgressDialog pdialog=ProgressDialog.show(this, "提示", "正在等待数据回馈...");
m_XNWebApplication.GetHttp(sUrl,new AppOutNetMsg()
{
@Override
public void PutInNetData(boolean bOK,JSONObject mJSONObject) {
Message message = new Message();
message.what = 1;
ReturnData m_ReturnData=new ReturnData();
m_ReturnData.bOK=bOK;
m_ReturnData.m_JSONObject=mJSONObject;
m_ReturnData.m_onOutAppData=m_onOutAppData0;
// message.obj=XYPack.UnPack(mJSONObject);
if(pdialog!=null)
pdialog.dismiss();
message.obj=m_ReturnData;
mHandler.sendMessage(message);
}
}
);
}
public void PostHttp(String sUrl,String sData,String sTiShi,onOutAppData m_onOutAppData)
{
//String sUrl="http://192.168.20.215/Test.aspx";
final onOutAppData m_onOutAppData0=m_onOutAppData;
final ProgressDialog pdialog=ShowWait("提示",sTiShi);
pdialog.show();
//final ProgressDialog pdialog=ProgressDialog.show(this, "提示", "正在等待数据回馈...");
m_XNWebApplication.PostHttp(sUrl,sData,new AppOutNetMsg()
{
@Override
public void PutInNetData(boolean bOK,JSONObject mJSONObject) {
Message message = new Message();
message.what = 1;
ReturnData m_ReturnData=new ReturnData();
m_ReturnData.bOK=bOK;
m_ReturnData.m_JSONObject=mJSONObject;
m_ReturnData.m_onOutAppData=m_onOutAppData0;
pdialog.dismiss();
message.obj=m_ReturnData;
mHandler.sendMessage(message);
}
}
);
}
public void DisplayToast(String Str)
{
Toast.makeText(this, Str, Toast.LENGTH_SHORT).show();
}
private ProgressDialog ShowWait(String sTitle,String sText)
{
ProgressDialog pdialog = new ProgressDialog(this);
pdialog.setTitle(sTitle);
pdialog.setMessage(sText);
//pdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//pdialog.setIndeterminate(false);
//pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
return pdialog;
}
}
公有类
public interface AppOutNetMsg {
public void PutInNetData(boolean bOK,JSONObject m_JSONObject);
}
public interface HttpWebMsg
{
public void ReturnPostHttp(AppOutNetMsg m_AppOutNetMsg,boolean bOK,String sData);
public void ReturnGetHttp(AppOutNetMsg m_AppOutNetMsg,boolean bOK,String sData);
public void ReturnPostHttpData(AppOutNetMsg m_AppOutNetMsg,boolean bOK,String sData);
}
public interface onOutAppData {
public void PutAppData(boolean bOK,JSONObject O);
}
public class HttpWeb {
public DefaultHttpClient httpclient = new DefaultHttpClient();//维持连接Cookie
public HttpWebMsg m_HttpWebMsg=null;
public HttpWeb()
{
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
}
public void TestGetNet() throws ClientProtocolException, IOException
{
//DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://192.168.20.215/Test.aspx");
HttpResponse httpResponse = httpclient.execute(httpget);
HttpEntity entity = httpResponse.getEntity();
System.out.println("Login form get: " + httpResponse.getStatusLine());
if (entity != null)
{
/*
InputStream in=entity.getContent();
byte[] m_NetData=new byte[100];
in.read(m_NetData);
System.out.println("Data:"+new String(m_NetData));
m_NetData=null;
*/
String result = EntityUtils.toString(httpResponse.getEntity());
System.out.println("GET收到数据:"+result);
}
System.out.println("Initial set of cookies:");
Log.e("1","nitial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty())
{
System.out.println("None");
Log.e("1","None");
}
else {
for (int i = 0; i < cookies.size(); i++)
{ System.out.println("- " + cookies.get(i).toString());
Log.e("1",cookies.get(i).toString());
}
}
}
/*
也可以使用 HttpURLConnection直接通过stream发送,这要看服务端是如何接收的,代码如下:
URL url = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestProperty("Charset", "UTF-8");
InputStream is = httpURLConnection.getInputStream();
OutputStream os = httpURLConnection.getOutputStream();
// 使用os发送xml数据,使用is接收服务端返回的数据
is.close();
os.close();
*/
public void TestPost(String sData) throws ClientProtocolException, IOException
{
HttpPost httpPost=new HttpPost("http://192.168.20.215/TestPost.aspx");
//List<NameValuePair> params = new ArrayList<NameValuePair>();
//params.add(new BasicNameValuePair("xml", sData));
//httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
StringEntity entity2 = new StringEntity(sData);
httpPost.setEntity(entity2);//new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = httpclient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
System.out.println("Login form get: " + httpResponse.getStatusLine());
if (entity != null)
{ //entity.consumeContent();
/*
InputStream in=entity.getContent();
byte[] m_NetData=new byte[100];
in.read(m_NetData);
System.out.println("Data:"+new String(m_NetData));
m_NetData=null;
*/
String result = EntityUtils.toString(httpResponse.getEntity());
System.out.println("POST收到数据:"+result);
}
System.out.println("Initial set of cookies:");
Log.e("1","nitial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty())
{
System.out.println("Cookies为空");
Log.e("1","None");
}
else {
for (int i = 0; i < cookies.size(); i++)
{
System.out.println("- " + cookies.get(i).toString());
Log.e("1",cookies.get(i).toString());
}
}
}
public boolean GetHttp(String sUrl,AppOutNetMsg m_AppOutNetMsg)
{
if(httpclient==null)
{
httpclient=new DefaultHttpClient();
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
}
HttpGet httpget = new HttpGet(sUrl);//"http://192.168.20.215/Test.aspx");
System.out.println("start开始请求");
HttpResponse httpResponse;
try {
httpResponse = httpclient.execute(httpget);
System.out.println("end请求完毕");
HttpEntity entity = httpResponse.getEntity();
System.out.println("Login form get: " + httpResponse.getStatusLine());
if (entity != null)
{
String result = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
if(m_HttpWebMsg!=null)
{
m_HttpWebMsg.ReturnGetHttp(m_AppOutNetMsg,true,result);
}
return true;
}
else
{
if(m_HttpWebMsg!=null)
{
m_HttpWebMsg.ReturnGetHttp(m_AppOutNetMsg,false,"");
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(m_HttpWebMsg!=null)
{
m_HttpWebMsg.ReturnGetHttp(m_AppOutNetMsg,false,"");
}
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if(m_HttpWebMsg!=null)
{
m_HttpWebMsg.ReturnGetHttp(m_AppOutNetMsg,false,"");
}
return false;
}
return false;
}
public boolean PostHttpData(String sUrl,String sData,AppOutNetMsg m_AppOutNetMsg)
{
if(httpclient==null)
{
httpclient=new DefaultHttpClient();
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
}
HttpPost httpPost=new HttpPost(sUrl);
StringEntity entity2;
try {
entity2 = new StringEntity(sData,HTTP.UTF_8);
httpPost.setEntity(entity2);
System.out.println("start开始Post");
HttpResponse httpResponse = httpclient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
System.out.println("Login form get: " + httpResponse.getStatusLine());
System.out.println("end post");
if (entity != null)
{
String result = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
if(m_HttpWebMsg!=null)
{
m_HttpWebMsg.ReturnPostHttpData(m_AppOutNetMsg,true,result);
}
return true;
}
else
m_HttpWebMsg.ReturnPostHttpData(m_AppOutNetMsg,false,"");
return false;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
m_HttpWebMsg.ReturnPostHttpData(m_AppOutNetMsg,false,"");
return false;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
m_HttpWebMsg.ReturnPostHttpData(m_AppOutNetMsg,false,"");
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
m_HttpWebMsg.ReturnPostHttpData(m_AppOutNetMsg,false,"");
return false;
}
}
public boolean PostHttp(String sUrl,List<NameValuePair> params,AppOutNetMsg m_AppOutNetMsg)
{
if(httpclient==null)
{
httpclient=new DefaultHttpClient();
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
}
HttpPost httpPost=new HttpPost(sUrl);
//List<NameValuePair> params = new ArrayList<NameValuePair>();
// params.add(new BasicNameValuePair("xml", sData));
try {
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
m_HttpWebMsg.ReturnPostHttp(m_AppOutNetMsg,false,"");
return false;
}
HttpResponse httpResponse=null;
try {
httpResponse = httpclient.execute(httpPost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
m_HttpWebMsg.ReturnPostHttp(m_AppOutNetMsg,false,"");
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
m_HttpWebMsg.ReturnPostHttp(m_AppOutNetMsg,false,"");
return false;
}
HttpEntity entity = httpResponse.getEntity();
System.out.println("Login form get: " + httpResponse.getStatusLine());
if (entity != null)
{
String result="";
try {
result = EntityUtils.toString(httpResponse.getEntity());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
m_HttpWebMsg.ReturnPostHttp(m_AppOutNetMsg,false,"");
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
m_HttpWebMsg.ReturnPostHttp(m_AppOutNetMsg,false,"");
return false;
}
//System.out.println("POST收到数据:"+result);
if(m_HttpWebMsg!=null)
{
m_HttpWebMsg.ReturnPostHttp(m_AppOutNetMsg,true,result);
}
}
else
m_HttpWebMsg.ReturnPostHttp(m_AppOutNetMsg,false,"");
return true;
}
public void OpenWeb()
{
if(httpclient!=null)
return;
httpclient=new DefaultHttpClient();
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
}
public void CloseWeb()
{
httpclient=null;
}
public void ReOpenWeb()
{
if(httpclient!=null)
httpclient=null;
httpclient=new DefaultHttpClient();
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
}
public void OpenWebProxy()
{
if(httpclient!=null)
return;
httpclient=new DefaultHttpClient();
HttpHost proxy = new HttpHost("10.0.0.172",80);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
}
public void ReOpenWebProxy()
{
if(httpclient!=null)
httpclient=null;
httpclient=new DefaultHttpClient();
HttpHost proxy = new HttpHost("10.0.0.172",80);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
}
}
HttpWork.java
public class HttpWork {
public boolean bWorking;
public String FName;
public AppOutNetMsg m_AppOutNetMsg;
public String sUrl;
public String sData;
}
public class ReturnData {
public onOutAppData m_onOutAppData;
public JSONObject m_JSONObject;
public boolean bOK;
}
XNWebApplication.java
public class XNWebApplication implements HttpWebMsg, Runnable
{
private HttpWeb m_HttpWeb=null;
private XYPack m_XYPack=null;
//private static final String sUrl = "视频监控Ver1.0";
private HttpWork m_HttpWork=new HttpWork();
public XNWebApplication()
{
m_HttpWeb=new HttpWeb();
m_HttpWeb.m_HttpWebMsg=this;
m_XYPack=new XYPack();
m_HttpWork.bWorking=false;
}
public boolean LGWlb(String sUrl,String sUser,String sPwd,AppOutNetMsg m_AppOutNetMsg)
{
if(m_HttpWork.bWorking)
return false;
String sData=m_XYPack.PackLG(sUser, sPwd);
m_HttpWork.bWorking=true;
m_HttpWork.FName="PostHttpData";
m_HttpWork.m_AppOutNetMsg=m_AppOutNetMsg;
m_HttpWork.sData=sData;
m_HttpWork.sUrl=sUrl;
Thread m_Thread=new Thread(this);
m_Thread.start();
return true;
/*
try {
m_HttpWeb.PostHttpData(sUrl, sData,m_AppOutNetMsg);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
*/
}
public boolean PostHttp(String sUrl,String sData,AppOutNetMsg m_AppOutNetMsg)
{
if(m_HttpWork.bWorking)
return false;
m_HttpWork.bWorking=true;
m_HttpWork.FName="PostHttpData";
m_HttpWork.m_AppOutNetMsg=m_AppOutNetMsg;
m_HttpWork.sData=sData;
m_HttpWork.sUrl=sUrl;
Thread m_Thread=new Thread(this);
m_Thread.start();
return true;
}
public boolean GetHttp(String sUrl,AppOutNetMsg m_AppOutNetMsg)
{
/*
try {
m_HttpWeb.GetHttp(sUrl,m_AppOutNetMsg);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
*/
if(m_HttpWork.bWorking)
return false;
m_HttpWork.bWorking=true;
m_HttpWork.FName="GetHttp";
m_HttpWork.m_AppOutNetMsg=m_AppOutNetMsg;
m_HttpWork.sData="";
m_HttpWork.sUrl=sUrl;
Thread m_Thread=new Thread(this);
m_Thread.start();
return true;
}
@Override
public void ReturnGetHttp(AppOutNetMsg m_AppOutNetMsg,boolean bOK,String sData) {
// TODO Auto-generated method stub
if(m_AppOutNetMsg!=null)
{
JSONObject result;
try {
result = new JSONObject( sData);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
m_AppOutNetMsg.PutInNetData(false,null);
return;
}
m_AppOutNetMsg.PutInNetData(true,result);
}
}
@Override
public void ReturnPostHttp(AppOutNetMsg m_AppOutNetMsg,boolean bOK,String sData) {
// TODO Auto-generated method stub
if(m_AppOutNetMsg!=null)
{
JSONObject result;
try {
result = new JSONObject( sData);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
m_AppOutNetMsg.PutInNetData(false,null);
return;
}
m_AppOutNetMsg.PutInNetData(true,result);
}
}
@Override
public void ReturnPostHttpData(AppOutNetMsg m_AppOutNetMsg,boolean bOK,String sData) {
// TODO Auto-generated method stub
if(m_AppOutNetMsg!=null)
{
JSONObject result;
try {
result = new JSONObject( sData);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
m_AppOutNetMsg.PutInNetData(false,null);
return;
}
m_AppOutNetMsg.PutInNetData(true,result);
}
}
@Override
public void run() {
// TODO Auto-generated method stub
if(m_HttpWork.FName=="PostHttpData")
{
m_HttpWeb.PostHttpData(m_HttpWork.sUrl, m_HttpWork.sData,m_HttpWork.m_AppOutNetMsg);
m_HttpWork.bWorking=false;
return ;
}
else
{
if(m_HttpWork.FName=="GetHttp")
{
m_HttpWeb.GetHttp(m_HttpWork.sUrl,m_HttpWork.m_AppOutNetMsg);
m_HttpWork.bWorking=false;
return;
}
}
}
}
XNPack.java
public class XYPack {
public String PackLG(String sUser,String sPWD)
{
// 先封装一个 JSON 对象
JSONObject param = new JSONObject();
try {
param.put("name", sUser);
param.put("password", sPWD);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return param.toString();
}
public boolean UnPackTest(String sResult)
{
JSONObject result;
try {
result = new JSONObject( sResult);
String token = (String)result.get("token");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
/*完美解。NET端Json
*
namespace SessionWeb
{
public class OneUser
{
public string Name;
public string Pic;
}
public class RetData
{
public bool bOK;
public string SessionNum;
public OneUser[] m_AllUser;
}
public partial class _Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Session["Hello"]==null)
{
Session["Hello"] = 1;
}
else
{
int A =Convert.ToInt32(Session["Hello"].ToString());
A += 1;
Session["Hello"] = A;
}
RetData m_RetData = new RetData();
m_RetData.bOK=true;
m_RetData.SessionNum=Session["Hello"].ToString();
m_RetData.m_AllUser = new OneUser[10];
for(int i=0;i<10;i++)
{
OneUser m_OneUser = new OneUser();
m_OneUser.Name = "xuneng"+i.ToString();
m_OneUser.Pic = i.ToString();
m_RetData.m_AllUser[i] = m_OneUser;
}
string jsonText = JsonConvert.SerializeObject(m_RetData);
Response.Write(jsonText);
//DataTableToJson();
//XmlToJson();
}
public void DataTableToJson()
{
string result = "";
DataTable dt = new DataTable();
dt.Columns.Add("name");//添加一列,想加几列再自己加
dt.Columns.Add("date");//添加一列,想加几列再自己加
DataRow dr;
for (int i = 0; i < 10; i++)//每列添多少行,条件自己加
{
dr = dt.NewRow();
dr["name"] = "1";
dr["date"] = DateTime.Now.ToString();
dt.Rows.Add(dr);//添加一行
}
result = JsonConvert.SerializeObject(dt, new DataTableConverter());
Response.Write(result);
}
public void XmlToJson()
{
string xml = "<?xml version=\"1.0\" standalone=\"no\"?>";
xml+="<root>";
xml += "<ret>true</ret>";
xml+="<person id=\"1\">";
xml+="<name>Alan</name>";
xml+="<url>http://www.google.com</url>";
xml+="</person>";
xml+="<person id=\"2\">";
xml+="<name>Louis</name>";
xml+="<url>http://www.yahoo.com</url>";
xml+="</person>";
xml+="Hello World";
xml+="</root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
//XML to JSON
string jsonText = JsonConvert.SerializeXmlNode(doc, Newtonsoft.Json.Formatting.Indented);
Response.Write(jsonText);
}
}
}
*/
static public String UnPack(JSONObject result)
{
String Str="SessionNum=";
try {
Str+=result.get("SessionNum").toString()+"\n";
Str+="bOK="+result.get("bOK").toString()+"\n";
JSONArray array = result.getJSONArray("m_AllUser");
Str+="UserCount="+array.length()+"\n";
int i;
for(i=0;i<array.length();i++)
{
JSONObject m_OneUser=array.getJSONObject(i);
Str+="Name="+m_OneUser.get("Name").toString()+"\n";
Str+="Pic="+m_OneUser.get("Pic").toString()+"\n";
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Str;
}
}
代码
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bkcolor"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="240dip"
android:gravity="center_horizontal|top"
>
<ImageButton
android:id="@+id/shyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shy_bt"
android:layout_marginTop="100dip"
>
</ImageButton>
<ImageButton
android:id="@+id/fkcButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/fkc_bt"
android:layout_marginTop="100dip"
>
</ImageButton>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"
android:paddingBottom="4dip"
android:paddingLeft="12dip">
>
<ImageButton
android:id="@+id/szButton"
android:layout_width="81dip"
android:layout_height="60dip"
android:background="@drawable/person_bt"
android:layout_marginTop="100dip"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dip"
android:layout_weight="1"
>
</ImageButton>
<TextView android:id="@+id/kongLine"
android:layout_width="100dip"
android:layout_height="60px"
android:layout_marginTop="100dip"
android:layout_weight="6"
>
</TextView>
<ImageButton android:layout_marginLeft="12dip"
android:layout_height="60dip"
android:background="@drawable/exit_bt"
android:id="@+id/exitButton"
android:layout_marginTop="100dip"
android:layout_width="81dip"
android:layout_weight="1"
></ImageButton>
</LinearLayout>
</LinearLayout>
xjwlb.java
public class xjwlb extends BaseHttpActivity {
/** Called when the activity is first created. */
public android.view.View.OnClickListener handler=null;
boolean bPost=false;
private Handler mHandler = new Handler(){
public void handleMessage(Message msg)
{
switch (msg.what)
{
default:
{
}
break;
}
};
};
public LGUserInfo GetUserInfo()
{
LGUserInfo m_LGUserInfo=new LGUserInfo();
JSONObject m_userInfo=(JSONObject)m_OtherObject;
JSONObject O;
try {
O = (JSONObject)m_userInfo.get("userInfo");
m_LGUserInfo.yhid=O.getString("yhid");
m_LGUserInfo.lxsj=O.getString("lxsj");
m_LGUserInfo.cc=O.getDouble("cc");
m_LGUserInfo.cx=O.getString("cx");
m_LGUserInfo.dlbs=O.getString("dlbs");
m_LGUserInfo.dqcs=O.getString("dqcs");
m_LGUserInfo.dw=O.getDouble("dw");
m_LGUserInfo.dqsf=O.getString("dqsf");
m_LGUserInfo.yhnc=O.getString("yhnc");
m_LGUserInfo.qymc=O.getString("qymc");
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return m_LGUserInfo;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
handler = new View.OnClickListener()
{
public void onClick(View v)
{
switch (v.getId())
{
case R.id.szButton:
{
szPerson();
}
break;
case R.id.exitButton:
{
Exit();
}
break;
case R.id.shyButton:
{
shObject();
}
break;
case R.id.fkcButton:
{
fkc();
}
break;
}
}
};
findViewById(R.id.szButton).setOnClickListener(handler);
findViewById(R.id.exitButton).setOnClickListener(handler);
findViewById(R.id.shyButton).setOnClickListener(handler);
findViewById(R.id.fkcButton).setOnClickListener(handler);
LGUserInfo m_LGUserInfo=GetUserInfo();
PZConfig m_PZConfig=new PZConfig();
m_PZConfig.m_ParentActivity=this;
DriverInfo m_DriverInfo=new DriverInfo();
if(m_PZConfig.ReadDriver(m_DriverInfo,m_LGUserInfo.lxsj,m_LGUserInfo.cx,m_LGUserInfo.cc,m_LGUserInfo.dw))
{
if(m_DriverInfo._yhid.equals(m_LGUserInfo.yhid))
{
m_LGUserInfo.lxsj=m_DriverInfo._lxfs;
m_LGUserInfo.cx=m_DriverInfo._cx;
m_LGUserInfo.dw=m_DriverInfo._dw;
m_LGUserInfo.cc=m_DriverInfo._cc;
JSONObject m_userInfo=(JSONObject)m_OtherObject;
try {
JSONObject O = (JSONObject)m_userInfo.get("userInfo");
O.put("lxsj",m_DriverInfo._lxfs);
O.put("cx",m_DriverInfo._cx);
O.put("dw",m_DriverInfo._dw);
O.put("cc",m_DriverInfo._cc);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
TrySoft();
}
public void Exit()
{
new AlertDialog.Builder(this)
.setTitle("提示")
.setIcon(R.drawable.why)
.setMessage("确定退出系统吗?")
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
})
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
}).show();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 按下键盘上返回按钮
if (keyCode == KeyEvent.KEYCODE_BACK) {
Exit();
return true;
} else {
return super.onKeyDown(keyCode, event);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// 或者下面这种方式
//System.exit(0);
//建议用这种
}
public void szPerson()
{
Intent iPerson = new Intent(xjwlb.this,PersonWindow.class);
startActivity (iPerson);
}
public void shObject()
{
Intent ishObject = new Intent(xjwlb.this,shobject.class);
startActivity (ishObject);
}
public void fkc()
{
Intent iFkc = new Intent(xjwlb.this,publishCar.class);
startActivity (iFkc);
}
public void TrySoft()
{
UpdateSoft m_UpdateSoft=new UpdateSoft();
m_UpdateSoft.m_ParentActivty=this;
m_UpdateSoft.bShow=false;
if(bPost)
m_UpdateSoft.PostVer();
else
m_UpdateSoft.GetVer(getString(R.string.updateurl));
}
}
XNDataSource.Java
public class XNDataSource extends BaseAdapter{
public ArrayList<HashMap<String, Object>> mylist = new ArrayList<HashMap<String, Object>>(); //这里的String表示对第二个字段的标题,后面的Object可以扩展为其他类对象
Activity activity=null;
onItemRender m_onOutItemRender=null;
int nItemRenderID;
public XNDataSource(Activity a,int nListItemResourceID,onItemRender m_onItemRender ){
activity = a;
m_onOutItemRender=m_onItemRender;
nItemRenderID=nListItemResourceID;
}
public int getCount() {
// TODO Auto-generated method stub
return mylist.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return mylist.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater =activity.getLayoutInflater();
final View view = inflater.inflate(nItemRenderID, null);
HashMap<String, Object>mOneRow=(HashMap<String, Object>)mylist.get(position);
if(m_onOutItemRender!=null)
{
m_onOutItemRender.RenderOneRow(this,view,position,mOneRow);
}
return view;
}
public void InitData()
{
for(int i=0;i<10;i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("icon",R.drawable.icon);
map.put("ItemTitle", "标题字段"+i);
map.put("ItemText", "显示文字字段显示文字字段显示文字字段显示文字字段显示文字字段显示文字字段");
mylist.add(map);
}
}
public void AddOneRow(Object O)
{
HashMap<String, Object> map=(HashMap<String, Object>)O;
mylist.add(map);
XNDataSource.this.notifyDataSetChanged();
}
public void AddOneRow(HashMap<String, Object>m_OneRow)
{
mylist.add(m_OneRow);
XNDataSource.this.notifyDataSetChanged();
}
public void DelOneRow(int position)
{
mylist.remove(position);
XNDataSource.this.notifyDataSetChanged();
}
public void DelAllRow()
{
mylist.removeAll(mylist);
XNDataSource.this.notifyDataSetChanged();
}
}
onItemRender.java
public interface onItemRender {
//渲染每一项接口
public void RenderOneRow(Object BaseAdapter,View m_View,int nPostion,HashMap<String, Object>m_OneRow);
/*
例子
@Override
public void RenderOneRow(Object BaseAdapter,View mView, int nPostion,
HashMap<String, Object> mOneRow) {
// TODO Auto-generated method stub
final ImageView image=(ImageView)mView.findViewById(R.id.ItemImage);
final TextView textTitle = (TextView) mView.findViewById(R.id.ItemTitle);
final TextView textRong = (TextView) mView.findViewById(R.id.ItemText);
final Button button = (Button)mView.findViewById(R.id.buttonDelete);
final Button addbutton = (Button)mView.findViewById(R.id.buttonAdd);
image.setImageResource((Integer)mOneRow.get("icon"));
textTitle.setText(mOneRow.get("ItemTitle").toString());
textRong.setText(mOneRow.get("ItemText").toString());
button.setTag(nPostion);
final Object OO=BaseAdapter;
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
int position = Integer.parseInt(v.getTag().toString());
XNDataSource m_XNDataSource=(XNDataSource)OO;
m_XNDataSource.DelOneRow(position);
}});
addbutton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
XNDataSource m_XNDataSource=(XNDataSource)OO;
HashMap<String,Object>map=new HashMap<String,Object>();
map.put("icon",R.drawable.icon);
map.put("ItemTitle", "标题字段"+200);
map.put("ItemText", "显示文字字段");
m_XNDataSource.AddOneRow(map);
}});
}
*/
}
下一篇:直接运行JAVA代码