一直报错 No adapter attached; skipping layout,我不知道哪里出问题了,求指点,谢谢你们
package com.example.myapplication;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
public class WanbanActivity extends AppCompatActivity {
private List<price> priceList;
private PriceAdapter adapter;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wanban);
initView();
connCs();
}
private void initView() {
recyclerView = findViewById(R.id.recyler);
LinearLayoutManager manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
}
private void showPrice(List<price> pri){
adapter=new PriceAdapter(this,priceList);
recyclerView.setAdapter(adapter);
}
public void connectwanban(){
new Thread(new Runnable() {
@Override
public void run() {
try {
Document document = Jsoup.connect("http://www.xinfadi.com.cn/getPriceData.html")
.data("query", "Java")
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.1.5162 SLBChan/11")
.cookie("auth", "token")
.timeout(8000)
.ignoreContentType(true)
.post();
System.out.println(document);
Elements el = document.getElementsByTag("body");
String string = el.text();
System.out.println(string);
JSONObject jsonObject= null;
jsonObject = new JSONObject(string);
StringBuilder cs=new StringBuilder();
int current=jsonObject.getInt("current");
int limit=jsonObject.getInt("limit");
int count=jsonObject.getInt("count");
JSONArray jsonArray=jsonObject.getJSONArray("list");
for(int i=0;i<jsonArray.length();i++){
JSONObject object=jsonArray.getJSONObject(i);
String prodName=object.getString("prodName");
String prodCatid=object.getString("prodCatid");
String prodCat=object.getString("prodCat");
String lowPrice=object.getString("lowPrice");
String highPrice=object.getString("highPrice");
String place=object.getString("place");
String unitInfo=object.getString("unitInfo");
String pubDate=object.getString("pubDate");
String avgPrice=object.getString("avgPrice");
int id= object.getInt("id");
Log.d("ting","Json======"+id+"\t"+prodName+"\t"+unitInfo+"\t"+prodCatid+"\t"+
prodCat+"\t"+lowPrice+"\t"+highPrice+"\t"+avgPrice+"\t"+pubDate);
priceList=new ArrayList<>();
price pri=new price();
pri.setid(id);
pri.setplace(place);
pri.setavgPrice(avgPrice);
pri.sethighPrice(highPrice);
pri.setlowPrice(lowPrice);
pri.setprodCatid(prodCatid);
pri.setunitInfo(unitInfo);
pri.setpubDate(pubDate);
// pri.setprodpcatid(pri.prodpcatid);
pri.setprodPcat(prodCat);
pri.setprodName(prodName);
priceList.add(pri);
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
}).start();
}
private void connCs(){
priceList=new ArrayList<>();
Observable.create(new ObservableOnSubscribe<List<price>>() {
@Override
public void subscribe(@NonNull ObservableEmitter<List<price>> emitter) throws Throwable {
Document document = Jsoup.connect("http://www.xinfadi.com.cn/getPriceData.html")
.data("query", "Java")
.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.1.5162 SLBChan/11")
.cookie("auth", "token")
.timeout(8000)
.ignoreContentType(true)
.post();
System.out.println(document);
Elements el = document.getElementsByTag("body");
String string = el.text();
System.out.println(string);
JSONObject jsonObject= null;
jsonObject = new JSONObject(string);
StringBuilder cs=new StringBuilder();
int current=jsonObject.getInt("current");
int limit=jsonObject.getInt("limit");
int count=jsonObject.getInt("count");
JSONArray jsonArray=jsonObject.getJSONArray("list");
for(int i=0;i<jsonArray.length();i++){
price pri=new price();
JSONObject object=jsonArray.getJSONObject(i);
String prodName=object.getString("prodName");
String prodCatid=object.getString("prodCatid");
String prodCat=object.getString("prodCat");
String lowPrice=object.getString("lowPrice");
String highPrice=object.getString("highPrice");
String place=object.getString("place");
String unitInfo=object.getString("unitInfo");
String pubDate=object.getString("pubDate");
String avgPrice=object.getString("avgPrice");
int id= object.getInt("id");
Log.d("ting","Json======"+id+"\t"+prodName+"\t"+unitInfo+"\t"+prodCatid+"\t"+
prodCat+"\t"+lowPrice+"\t"+highPrice+"\t"+avgPrice+"\t"+pubDate);
pri.setid(id);
pri.setplace(place);
pri.setavgPrice(avgPrice);
pri.sethighPrice(highPrice);
pri.setlowPrice(lowPrice);
pri.setprodCatid(prodCatid);
pri.setunitInfo(unitInfo);
pri.setpubDate(pubDate);
// pri.setprodpcatid(pri.prodpcatid);
pri.setprodPcat(prodCat);
pri.setprodName(prodName);
priceList.add(pri);
emitter.onNext(priceList);
}
}
}) .observeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<price>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(@NonNull List<price> priceList) {
showPrice(priceList);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
;
}
}
下面是adapter
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
public class PriceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private List<price> priceList;
private Context context;
public PriceAdapter(Context context,List<price> obj){
this.context=context;
this.priceList=obj;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.item,parent,false);
return new PriceHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewholder, int i) {
PriceHolder holder= (PriceHolder) viewholder;
final price pri=priceList.get(i);
holder.id.setText(pri.getid());
holder.hignPrice.setText(pri.gethignPrice());
holder.unitInfo.setText(pri.getunitInfo());
holder.pubDate.setText(pri.getpubDate());
holder.prodpcatid.setText(pri.getprodpcatid());
holder.prodPcat.setText(pri.getprodCat());
holder.prodName.setText(pri.getprodName());
holder.place.setText(pri.getplace());
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getItemCount() {
return priceList.size();
}
}
holder
package com.example.myapplication;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class PriceHolder extends RecyclerView.ViewHolder{
TextView id;
TextView prodName;
TextView proCatid;
TextView prodCat;
TextView prodpcatid;
TextView prodPcat;
TextView lowPrice;
TextView hignPrice;
TextView avgPrice;
TextView place;
TextView unitInfo;
TextView pubDate;
public PriceHolder(@NonNull View itemView) {
super(itemView);
id = itemView.findViewById(R.id.id);
prodName = itemView.findViewById(R.id.prodName);
prodCat = itemView.findViewById(R.id.prodCat);
pubDate = itemView.findViewById(R.id.pubDate);
unitInfo= itemView.findViewById(R.id.unitInfo);
place = itemView.findViewById(R.id.place);
avgPrice= itemView.findViewById(R.id.avgPrice);
hignPrice = itemView.findViewById(R.id.hignPrice);
lowPrice = itemView.findViewById(R.id.lowPrice);
hignPrice = itemView.findViewById(R.id.hignPrice);
}
}