gpt4 book ai didi

android - 如何将数据从 okhttp 和 Asynctask 放入 RecyclerView

转载 作者:行者123 更新时间:2023-11-29 02:38:08 25 4
gpt4 key购买 nike

我已经通过 okhttp 从服务器获取了一些 Json 数据,我想在一个 使用自定义适配器在 fragment 内部回收 View 并收到以下错误。

当我运行这段代码时,logcat 告诉我以下信息:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference

我在 logcat 中打印 list ,它显示为 null。但我可以从 IE 中获取一些 json 数据。有人帮我吗?

public class customer extends AppCompatActivity {

private static final String TAG = "MyDebug______ " ;
private RecyclerView cutomerRecyclerView;
private customerAdapter mAdapter;
private ArrayList<custModal> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer);
Toolbar toolbar = (Toolbar)findViewById(R.id.custom_toolbar);
setSupportActionBar(toolbar);

new getHttpData().execute();
Log.d(TAG, "the list is :" +list);
mAdapter = new customerAdapter(this,list);
cutomerRecyclerView = (RecyclerView)findViewById(R.id.customer_list);
cutomerRecyclerView.setLayoutManager(new LinearLayoutManager(this));
cutomerRecyclerView.setAdapter(mAdapter);
cutomerRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration(){
@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state){
super.onDraw(c,parent,state);
c.drawColor(R.color.colorDevider);
}
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDrawOver(c, parent, state);
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);

outRect.offset(0,1);
}
});
}


public class customerAdapter extends RecyclerView.Adapter<customerAdapter.customerViewHolder>{
private Context mcontext;
private ArrayList<custModal> mlist;
// private LayoutInflater inflater;
class customerViewHolder extends RecyclerView.ViewHolder{
private TextView name,mem,cust_id;

public customerViewHolder(View view){
super(view);
cust_id = (TextView )view.findViewById(R.id.cust_id);
name = (TextView)view.findViewById(R.id.cust_name);
mem = (TextView)view.findViewById(R.id.cust_mem);
}
}
public customerAdapter(Context context,ArrayList<custModal> list){
this.mcontext = context;
this.mlist = list;
}
@Override
public customerViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
customerViewHolder holder = new customerViewHolder(LayoutInflater.from(
customer.this).inflate(R.layout.customer_item,parent,false));
return holder;
}
@Override
public void onBindViewHolder(customerViewHolder holder,int position){
holder.cust_id.setText(mlist.get(position).getId());
holder.name.setText(mlist.get(position).getName());
holder.mem.setText(mlist.get(position).getMobiphone());
}
@Override
public int getItemCount(){ return mlist.size();}


}

类 custModal {

    private String id;
private String name;
private String gender;
private String mobiphone;
private String creat_time;
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}

public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}

public void setGender(String gender) {
this.gender = gender;
}
public String getGender() {
return gender;
}

public void setMobiphone(String mobiphone) {
this.mobiphone = mobiphone;
}
public String getMobiphone() {
return mobiphone;
}

public void setCreat_time(String creat_time) {
this.creat_time = creat_time;
}
public String getCreat_time() {
return creat_time;
}

}



public class getHttpData extends AsyncTask<String,Integer,ArrayList<custModal> >{
//private ArrayList<custModal> custlist;

@Override

// protected void onPreExecute(){}

@Override

protected ArrayList<custModal> doInBackground(String... params){
String url = "http://aaaa.bbbbb.cn/index.php/mobi/customer/app_getlist";

try{
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
Log.d(TAG, "doInBackground: "+ responseData);
if(responseData != null){
//return getCustFromJson(responseData);
Gson gson = new Gson();
ArrayList<custModal> custlist = gson.fromJson(responseData,
new TypeToken<List<custModal>>() {}.getType()
);

return custlist;
}else{
return null;
}
}catch (IOException I){
I.printStackTrace();
}

return null;
}
@Override

protected void onPostExecute(ArrayList<custModal> custlist){
if(!custlist.isEmpty()){
super.onPostExecute(custlist);
list = custlist;
mAdapter.notifyDataSetChanged();
}
}

}

最佳答案

确保在 onCreate 中初始化适配器、适配器数组列表并将适配器附加到回收器然后,您必须在需要进行 http 调用的地方添加新的 yourAsyncTask().execute。在您的 onBackground 中将项目添加到数组列表,并在 postExecute 中简单地调用通知数据更改方法

关于android - 如何将数据从 okhttp 和 Asynctask 放入 RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45877601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com