SouView
package com.bwie.jd.view.IView;
import com.bwie.jd.model.bean.SouBean;
public interface SouView extends IBaseView{
void onSuccess(SouBean souBean);
}
SouPresenter
package com.bwie.jd.presenter;
public class SouPresenter extends BasePresenter{
public void SouloadDataFromNet(String keywords,int page){
Observable souSuo = HttpUtils.getHttpUtilsInstance().apiClient.sousuo(keywords, page);
souSuo.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(SouBean souBean) {
List<SouBean.DataBean> data = souBean.getData();
getView().onSuccess(souBean);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}
LieActivity
package com.bwie.jd.view.activity;
public class LieActivity extends AppCompatActivity implements SouView {
@BindView(R.id.show_recyclerView)
RecyclerView showRecyclerView;
private String name;
private SouPresenter souPresenter;
private List<SouBean.DataBean> data;
private SouAdapter souAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lie);
ButterKnife.bind(this);
souPresenter = new SouPresenter();
souPresenter.attachView(this);
Intent intent = getIntent();
name = intent.getStringExtra("name");
Log.i("ccc", name);
souPresenter.SouloadDataFromNet(name, 1);
showRecyclerView.setLayoutManager(new LinearLayoutManager(LieActivity.this,LinearLayoutManager.VERTICAL,false));
}
@Override
public void onSuccess(SouBean souBean) {
data = souBean.getData();
souAdapter = new SouAdapter(LieActivity.this, data);
showRecyclerView.setAdapter(souAdapter);
}
}
SouAdapter
package com.bwie.jd.view.adapter;
public class SouAdapter extends RecyclerView.Adapter<SouAdapter.MyViewHolder>{
private Context context;
private List<SouBean.DataBean> data;
public SouAdapter(Context context, List<SouBean.DataBean> data) {
this.context = context;
this.data = data;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.showlayout,parent,false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
holder.showAdapterTitle.setText(data.get(position).getTitle());
holder.showAdapterPrice.setText(data.get(position).getPrice()+"");
String images = data.get(position).getImages();
String[] split = images.split("\\|");
Uri parse = Uri.parse(split[0]);
AbstractDraweeController fresco = Fresco.newDraweeControllerBuilder()
.setUri(parse)
.build();
holder.showAdapterSimple.setController(fresco);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pid = data.get(position).getPid();
Intent intent = new Intent(context, InfoActivity.class);
intent.putExtra("pid",pid);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return data.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private SimpleDraweeView showAdapterSimple;
private TextView showAdapterTitle,showAdapterPrice;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
showAdapterSimple = itemView.findViewById(R.id.show_adapter_simple);
showAdapterPrice = itemView.findViewById(R.id.show_adapter_price);
showAdapterTitle = itemView.findViewById(R.id.show_adapter_title);
}
}
}
InfoView
package com.bwie.jd.view.IView;
import com.bwie.jd.model.bean.GoodsInfoBean;
import com.bwie.jd.model.bean.JiaBean;
public interface InfoView extends IBaseView {
void resultInfoData(GoodsInfoBean goodsInfoBean);
void getSuccess(JiaBean jiaBean);
void getError(String msg);
}
InFoPresenter
package com.bwie.jd.presenter;
import com.bwie.jd.model.bean.GoodsInfoBean;
import com.bwie.jd.model.bean.JiaBean;
import com.bwie.jd.model.http.HttpUtils;
import com.bwie.jd.view.IView.InfoView;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
public class InfoPresenter extends BasePresenter {
public void queryGoodsInFoByPid(int pid ){
HttpUtils.getHttpUtilsInstance().apiClient.queryGoodsByPid(pid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<GoodsInfoBean>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(GoodsInfoBean goodsInfoBean) {
getView().resultInfoData(goodsInfoBean);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
public void jiagou(String uid,String pid){
Observable<JiaBean> jia = HttpUtils.getHttpUtilsInstance().apiClient.jia(uid, pid);
jia.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<JiaBean>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(JiaBean jiaBean) {
if(getView()!=null){
getView().getSuccess(jiaBean);
}
}
@Override
public void onError(Throwable e) {
if(getView()!=null){
getView().getError(e.toString());
}
}
@Override
public void onComplete() {
}
});
}
}
InFoActivity
package com.bwie.jd.view.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.bwie.jd.R;
import com.bwie.jd.model.bean.GoodsInfoBean;
import com.bwie.jd.model.bean.JiaBean;
import com.bwie.jd.presenter.InfoPresenter;
import com.bwie.jd.view.IView.InfoView;
import com.facebook.drawee.view.SimpleDraweeView;
import butterknife.BindView;
import butterknife.ButterKnife;
public class InfoActivity extends AppCompatActivity implements InfoView, View.OnClickListener {
@BindView(R.id.info_sdv_icon)
SimpleDraweeView infoSdvIcon;
@BindView(R.id.info_tv_title)
TextView infoTvTitle;
@BindView(R.id.info_btn_add)
Button infoBtnAdd;
@BindView(R.id.price)
TextView price;
private InfoPresenter infoPresenter;
private String pid;
private String pid1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
ButterKnife.bind(this);
infoPresenter = new InfoPresenter();
infoPresenter.attachView(this);
int pid = getIntent().getIntExtra("pid", 1);
infoPresenter.queryGoodsInFoByPid(pid);
}
@Override
public void resultInfoData(GoodsInfoBean goodsInfoBean) {
String s = goodsInfoBean.getData().getTitle();
infoTvTitle.setText(s);
price.setText(goodsInfoBean.getData().getPrice() + "¥");
infoSdvIcon.setImageURI(goodsInfoBean.getData().getImages().split("\\|")[0]);
pid1 = goodsInfoBean.getData().getPid();
//点击加入购物车
infoBtnAdd.setOnClickListener(this);
}
@Override
public void getSuccess(JiaBean jiaBean) {
String code = jiaBean.getCode();
if("0".equals(code)){
Toast.makeText(InfoActivity.this,"加购成功",Toast.LENGTH_SHORT).show();
}
}
@Override
public void getError(String msg) {
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.info_btn_add:
infoPresenter.jiagou("75",""+pid1);
break;
}
}
}
ShowPresenter
package com.bwie.jd.presenter;
import com.bwie.jd.model.bean.SouBean;
import com.bwie.jd.model.http.HttpUtils;
import com.bwie.jd.view.IView.ShowView;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
public class ShowPresenter extends BasePresenter {
public void ShowloadDataFromNet(String keywords, int page) {
Observable sousuo = HttpUtils.getHttpUtilsInstance().apiClient.sousuo(keywords, page);
sousuo.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(SouBean souBean) {
getView().onsuccer(souBean);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}
ShowActivity
package com.bwie.jd.view.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.bwie.jd.R;
import com.bwie.jd.model.bean.SouBean;
import com.bwie.jd.presenter.SouPresenter;
import com.bwie.jd.view.IView.SouView;
import com.bwie.jd.view.adapter.SouAdapter;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class ShowActivity extends AppCompatActivity implements SouView {
@BindView(R.id.sao)
ImageView sao;
@BindView(R.id.sousou_imageview)
ImageView sousouImageview;
@BindView(R.id.sousuo_edittext)
EditText sousuoEdittext;
@BindView(R.id.show_recyclerView)
RecyclerView showRecyclerView;
private String string;
private String string1;
private SouPresenter souPresenter;
private SouAdapter souAdapter;
private List<SouBean.DataBean> data;
private String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
ButterKnife.bind(this);
souPresenter = new SouPresenter();
souPresenter.attachView(this);
ButterKnife.bind(this);
Intent intent = getIntent();
string = intent.getStringExtra("sousuo");
Log.i("bbb",string);
souPresenter.SouloadDataFromNet(string,1);
// name = intent.getStringExtra(“name”);
// Log.i(“ccc”,name);
// souPresenter.SouloadDataFromNet(name,1);
showRecyclerView.setLayoutManager(new LinearLayoutManager(ShowActivity.this,LinearLayoutManager.VERTICAL,false));
}
@OnClick(R.id.sousou_imageview)
public void onViewClicked() {
string1 = sousuoEdittext.getText().toString();
souPresenter.SouloadDataFromNet(string1,1);
}
@Override
public void onSuccess(SouBean souBean) {
data = souBean.getData();
souAdapter = new SouAdapter(ShowActivity.this,data);
showRecyclerView.setAdapter(souAdapter);
// Toast.makeText(this, souBean.getData().get(0).getTitle(), Toast.LENGTH_SHORT).show();
}
}
ShowView
package com.bwie.jd.view.IView;
import com.bwie.jd.model.bean.SouBean;
public interface ShowView extends IBaseView{
void onsuccer(SouBean listBean);//成功的方法
}