gpt4 book ai didi

android - RecyclerView 中的 ArrayList 大小为零且无数据显示

转载 作者:行者123 更新时间:2023-11-29 15:35:06 24 4
gpt4 key购买 nike

我正在尝试在 recyclerview 中获得响应。我在日志中收到响应,但是当我将它添加到 recyclerView 时,没有显示任何数据。我不明白我错过了什么。这让我非常恼火,请帮忙

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
ArrayList<Model> arrayList;

@NonNull
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,viewGroup,false);
return new MyViewHolder(itemView);
}

public RecyclerViewAdapter(ArrayList<Model> arrayList) {
this.arrayList = arrayList;
System.out.println("size 111 "+arrayList.size());
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
Model model = arrayList.get(i);

myViewHolder.tvName.setText(model.getName());
myViewHolder.tvEmail.setText(model.getEmail());
myViewHolder.tvMobile.setText(model.getMobile());

}

@Override
public int getItemCount() {
System.out.println("size "+arrayList.size());
return arrayList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvName, tvEmail, tvMobile;

public MyViewHolder(@NonNull View itemView) {
super(itemView);
tvName = (TextView) itemView.findViewById(R.id.tvName);
tvEmail = (TextView) itemView.findViewById(R.id.tvEmail);
tvMobile = (TextView) itemView.findViewById(R.id.tvMobile);
}
}

这是我的 Activity 课

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
Model model;
ArrayList<Model> arrayList;
RecyclerView recyclerView;
RecyclerViewAdapter recyclerViewAdapter;
String url = "https://api.androidhive.info/volley/person_array.json";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList = new ArrayList<>();
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));


recyclerViewAdapter = new RecyclerViewAdapter(arrayList);
recyclerView.setAdapter(recyclerViewAdapter);
getData();



}

protected void getData() {
final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading......");
dialog.show();
JsonArrayRequest arrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.e(TAG, "onResponse: "+response.toString() );
dialog.hide();
for (int i=0;i<response.length();i++){
try {
JSONObject object = (JSONObject) response.get(i);
String name = object.getString("name");
System.out.println("name "+name);
String email = object.getString("email");
JSONObject object1 = object.getJSONObject("phone");
String mobile = object1.getString("mobile");
System.out.println("mobile "+mobile);
model = new Model(name,email,mobile);
arrayList.add(model);
System.out.println("activity sizee"+arrayList.size());
} catch (JSONException e) {
e.printStackTrace();
}
}

尝试了一切,但没有任何效果请帮我解决这个问题。我在哪里犯了错误。请帮忙

最佳答案

您需要使用 notifyDataSetChanged 通知更改在 for 循环之后告诉适配器更新 ListView 。 adapter observer 会在内部收到通知,数据源已更改,需要通过它重新将数据与布局绑定(bind)

@Override
public void onResponse(JSONArray response) {
Log.e(TAG, "onResponse: "+response.toString() );
dialog.hide();
for (int i=0;i<response.length();i++){
try {
JSONObject object = (JSONObject) response.get(i);
String name = object.getString("name");
System.out.println("name "+name);
String email = object.getString("email");
JSONObject object1 = object.getJSONObject("phone");
String mobile = object1.getString("mobile");
System.out.println("mobile "+mobile);
model = new Model(name,email,mobile);
arrayList.add(model);
System.out.println("activity sizee"+arrayList.size());
} catch (JSONException e) {
e.printStackTrace();
}
recyclerViewAdapter.notifyDataSetChanged();
//^^^^^^^^^^^^^^^^^^^^^
}

关于android - RecyclerView 中的 ArrayList 大小为零且无数据显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52319326/

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