适配器类中的getView函数在从服务器获取配置文件后以无限循环运行?

问题描述 投票:0回答:1

我的适配器代码

public class profiles_adapter extends ArrayAdapter<profiles> {
Context context;

public profiles_adapter(Context context, int resourceId, List<profiles> items) {
    super(context, resourceId, items);
    this.context = context;
}

public View getView(int position, View convertView, @NotNull ViewGroup parent) {


    profiles profile = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.cards, parent, false);

    }

    TextView card_name = convertView.findViewById(R.id.card_name);
    card_name.setText(profile.getPet_name());
    TextView card_age = convertView.findViewById(R.id.card_age);
    card_age.setText(String.valueOf(profile.getAge()));
    TextView college_name = convertView.findViewById(R.id.college_name);
    college_name.setText(profile.getCollege());

return convertView;
   }

我正在设置适配器的班级

    profiles_adapter = new profiles_adapter(getContext(), R.layout.cards, profile_list);
    SwipeFlingAdapterView flingContainer = view.findViewById(R.id.swipe_layout);
    flingContainer.setAdapter(profiles_adapter);

我在其中设置配置文件并通知适配器有关更改的代码

getActivity().runOnUiThread(new Runnable() {

                            @Override
                            public void run() {

                                for (int j = 0; j < profiles.length(); j++) {
                                    JSONObject object = null;
                                    try {
                                        object = (JSONObject) profiles.get(j);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                    //add a model
                                    profiles model = new Gson().fromJson(String.valueOf(object), profiles.class);
                                    profile_list.add(model);
                                }
                                profiles_adapter.notifyDataSetChanged();
                                progressHUD.dismiss();
                            }

                        });

这是因为我要在runOnUI方法内通知适配器,任何人都可以帮我解决这个问题

android android-adapter okhttp android-adapterview
1个回答
0
投票

放置

profiles profile = getItem(position);

检查convertView是否为null之后,就这样了>>

© www.soinside.com 2019 - 2024. All rights reserved.