1. 程式人生 > >MVP展示列表

MVP展示列表

要寫網路許可權
Model層
package com.jia.logindemo.model;

import android.util.Log;

import com.google.common.io.CharStreams;
import com.google.gson.Gson;
import com.jia.logindemo.HttpModelCallBack;
import com.jia.logindemo.bean.ShowBean;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class ShowModel {

public void show(final HttpModelCallBack<List> callBack){
    final String data = "http://www.xieast.com/api/news/news.php?type=shehui&page=1";
    new Thread(){
        @Override
        public void run() {
            super.run();
            try {
                URL url = new URL(data);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(3000);
                int code = connection.getResponseCode();
                if(code == HttpURLConnection.HTTP_OK){}
                InputStream inputStream = connection.getInputStream();
                String s = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8"));
                Gson gson = new Gson();
                ShowBean showBean = gson.fromJson(s,ShowBean.class);
                List<ShowBean.DataBean> data1 = showBean.getData();
                Log.d("tttttttt:",data1.get(1).toString());
                if(showBean.getCode() == 1){
                    callBack.onSuccess(data1);
                }else{
                    callBack.onFailer("展示失敗");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.start();
}

}

View層
package com.jia.logindemo.view;

import com.jia.logindemo.bean.ShowBean;

import java.util.List;

public interface ShowView {

void showSuccess(List<ShowBean.DataBean> result);
void showFailer(String msg);

}

Presenter層

package com.jia.logindemo.presenter;

import com.jia.logindemo.HttpModelCallBack;
import com.jia.logindemo.bean.ShowBean;
import com.jia.logindemo.model.ShowModel;
import com.jia.logindemo.view.ShowView;

import java.util.List;

public class ShowPresenter {
private ShowView showView;
private ShowModel showModel;

public ShowPresenter(ShowView showView) {
    this.showView = showView;
    showModel = new ShowModel();
}
public void show(){
    showModel.show(new HttpModelCallBack<List>() {
        @Override
        public void onSuccess(List<ShowBean.DataBean> result) {

            showView.showSuccess(result);
        }
        @Override
        public void onFailer(String msg) {
            showView.showFailer("展示失敗");
        }
    });

}

}

Activity
package com.jia.logindemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.Toast;

import com.jia.logindemo.bean.ShowBean;
import com.jia.logindemo.presenter.ShowPresenter;
import com.jia.logindemo.view.ShowView;

import java.util.ArrayList;
import java.util.List;

public class ShowActivity extends AppCompatActivity implements ShowView {

private GridView gv_show;
private ShowPresenter showPresenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show);
    gv_show = findViewById(R.id.gv_show);
    showPresenter = new ShowPresenter(this);
    showPresenter.show();
}


@Override
public void showSuccess(final List<ShowBean.DataBean> result) {
	//快速切換執行緒
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            MyAdapter adapter = new MyAdapter(ShowActivity.this,result);
            // Toast.makeText(ShowActivity.this,result.toString(),Toast.LENGTH_SHORT).show();
            gv_show.setAdapter(adapter);
        }
    });

}

@Override
public void showFailer(String msg) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(ShowActivity.this,"展示失敗",Toast.LENGTH_SHORT).show();
        }
    });
}

}

介面卡Adapter
package com.jia.logindemo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.jia.logindemo.bean.ShowBean;

import java.util.List;

public class MyAdapter extends BaseAdapter {
private Context context;
private List<ShowBean.DataBean> list;

public MyAdapter(Context context, List<ShowBean.DataBean> list) {
    this.context = context;
    this.list = list;
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if(convertView == null){
        convertView = View.inflate(context,R.layout.show_grid,null);
        holder = new ViewHolder();
        holder.title = convertView.findViewById(R.id.title);
        holder.date = convertView.findViewById(R.id.date);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }
    holder.date.setText(""+list.get(position).getDate());
    holder.title.setText(""+list.get(position).getTitle());
    return convertView;
}
class ViewHolder{
    TextView title;
    TextView date;
}

}