xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:id="@+id/userName"
android:padding="16dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:id="@+id/pass"
android:padding="16dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/app_login"
android:id="@+id/login"
android:layout_gravity="center"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/app_reg"
android:id="@+id/reg"
android:layout_gravity="center"
/>
</LinearLayout>
MainCctivity主代码
package bwie.com.app1;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import bwie.com.app1.base.BaseActivity;
import bwie.com.app1.present.PresentClass;
import bwie.com.app1.view.InteView;
public class MainActivity extends BaseActivity implements View.OnClickListener {
private PresentClass presentClass;
private EditText userName;
private EditText pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void initData() {
}
@Override
protected void initView() {
userName = findViewById(R.id.userName);
pass = findViewById(R.id.pass);
findViewById(R.id.login).setOnClickListener(this);
findViewById(R.id.reg).setOnClickListener(this);
presentClass = new PresentClass(this);
}
@Override
protected int initLayout() {
return R.layout.activity_main;
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.login:
Toast.makeText(MainActivity.this,"啦啦啦啦",Toast.LENGTH_SHORT).show();
presentClass.getPresentData(userName.getText().toString(),pass.getText().toString());
Intent intent1 = new Intent(MainActivity.this, HomeActivity.class);
startActivity(intent1);
break;
case R.id.reg:
Intent intent = new Intent(MainActivity.this, RegActivity.class);
startActivity(intent);
break;
}
}
}
登录的model 接口 因为登录需要地址 ,用户名 密码,请求成功的返回方法 所以带有四个参数
package bwie.com.app1.model;
public interface InterLogin
{
public void getLoginData(String url,String name,String pass,ILoginBack iLoginBack);
//请求成功失败回调
interface ILoginBack{
void LoadSuccess(String data);
void LoadFail();
}
}
登录的model.class
package bwie.com.app1.model;
import bwie.com.app1.network.OkHttp3;
public class LoginClass implements InterLogin
{
@Override
public void getLoginData(final String url, final String name, final String pass, final ILoginBack iLoginBack) {
//开启线程
new Runnable(){
@Override
public void run() {
new OkHttp3().okHttpPost(url,name,pass);
}
}.run();
}
}
登录的presenter定义的接口
package bwie.com.app1.present;
public interface InterPresenter
{
void getPresentData(String name,String pwd);
}
登录的presenter定义的类
package bwie.com.app1.present;
import android.util.Log;
import bwie.com.app1.MainActivity;
import bwie.com.app1.api.Api;
import bwie.com.app1.model.InterLogin;
import bwie.com.app1.model.LoginClass;
public class PresentClass implements InterPresenter
{
private final LoginClass loginClass;
MainActivity mainActivity;
public PresentClass(MainActivity mainActivity) {
//初始化model
loginClass = new LoginClass();
//初始化view
this.mainActivity =mainActivity;
}
@Override
public void getPresentData(String name, String pwd) {
loginClass.getLoginData(Api.LOGIN, name, pwd, new InterLogin.ILoginBack() {
@Override
public void LoadSuccess(String data) {
Log.i("LoadSuccess",data+"");
}
@Override
public void LoadFail() {
Log.i("LoadFail","失败了");
}
});
}
}
登录的view接口
package bwie.com.app1.view;
public interface InteView
{
public void getViewData(String mViewData);
}
登录成功最终的显示页面 xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<com.recker.flybanner.FlyBanner
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/ban"
></com.recker.flybanner.FlyBanner>
<ListView
android:id="@+id/list"
android:layout_weight="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
登录成功最终的显示页面 主代码
package bwie.com.app1;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import com.google.gson.Gson;
import com.recker.flybanner.FlyBanner;
import java.util.ArrayList;
import java.util.List;
import bwie.com.app1.adapter.MyAdapter;
import bwie.com.app1.bean.HomeBean;
import bwie.com.app1.present.ShouPresenterClass;
import bwie.com.app1.view.InteView;
public class HomeActivity extends AppCompatActivity implements InteView
{
private ShouPresenterClass shouPresenterClass;
private FlyBanner ban;
private ListView list;
private List<HomeBean.ResultBean.PzshBean.CommodityListBeanX> beanXList;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
shouPresenterClass = new ShouPresenterClass(this);
shouPresenterClass.getShouPresenterData();
ban = findViewById(R.id.ban);
list = findViewById(R.id.list);
ArrayList<String> strings = new ArrayList<>();
strings.add("http://172.17.8.100/images/small/commodity/nx/ddx/3/1.jpg");
strings.add("http://172.17.8.100/images/small/commodity/nx/ddx/3/1.jpg");
strings.add("http://172.17.8.100/images/small/commodity/nx/ddx/3/1.jpg");
strings.add("http://172.17.8.100/images/small/commodity/nx/ddx/3/1.jpg");
ban.setImagesUrl(strings);
}
@Override
public void getViewData(final String mViewData) {
new Runnable(){
@Override
public void run() {
if(!mViewData.equals(""))
{
Gson gson = new Gson();
HomeBean bean = gson.fromJson(mViewData, HomeBean.class);
beanXList = bean.getResult().getPzsh().get(0).getCommodityList();
Message message = new Message();
message.what=0;
message.obj= beanXList;
handler.sendMessage(message);
}
}
}.run();
}
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
MyAdapter myAdapter = new MyAdapter(HomeActivity.this, beanXList);
list.setAdapter(myAdapter);
break;
}
}
};
}
登录成功最终的显示页面 适配器
package bwie.com.app1.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
import bwie.com.app1.HomeActivity;
import bwie.com.app1.R;
import bwie.com.app1.bean.HomeBean;
public class MyAdapter extends BaseAdapter
{
Context context;
List<HomeBean.ResultBean.PzshBean.CommodityListBeanX> beanXList;
public MyAdapter(Context context, List<HomeBean.ResultBean.PzshBean.CommodityListBeanX> beanXList) {
this.context=context;
this.beanXList=beanXList;
}
@Override
public int getCount() {
return beanXList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView= View.inflate(context, R.layout.list,null);
TextView name= convertView.findViewById(R.id.name);
ImageView img = convertView.findViewById(R.id.img);
name.setText(beanXList.get(position).getCommodityName());
ImageLoader.getInstance().displayImage(beanXList.get(position).getMasterPic(),img);
return convertView;
}
}