【问题标题】:Adapter is not parsing the arraylist which I am sending to it适配器没有解析我发送给它的数组列表
【发布时间】:2025-12-05 02:00:01
【问题描述】:

我正在使用这个滑动库https://github.com/Diolor/Swipecards

这是我从中获取数据并存储在 ArrayList 中的代码(数据正确来自服务器)

JSONObject json = new JSONObject(mMessage);
                    JSONArray profiles = json.getJSONArray("profiles");

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

这些是适配器和 Arraylist 的声明

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

这是我的适配器代码

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


public profiles_adapter(Context context, int resourceId, ArrayList<profiles> item) {
    super(context, resourceId, item);
    this.context=context;
}
  Public View getView(int position, View convertView, ViewGroup parent) {

   final profiles profile = getItem(position);


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

    TextView card_name = (TextView) convertView.findViewById(R.id.card_name);
    card_name.setText(profile.getPet_name());
    TextView card_age = (TextView) convertView.findViewById(R.id.card_age);
    card_age.setText(profile.getAge());
}

【问题讨论】:

    标签: android android-recyclerview adapter


    【解决方案1】:

    您的代码有一些变化,如下所示,

                    JSONObject json = new JSONObject(mMessage);
                    JSONArray profiles = json.getJSONArray("profiles");
    
                    for (int j = 0; j < profiles.length(); j++) {
                        JSONObject object =(JSONObject) profiles.get(j);
                       //add a model
                        profiles model =new Gson().fromJson(String.valueOf(object), profiles.class);
                        profile_list.add(model);
    
                    }
                profiles_adapter.notifyDataSetChanged();
    

    适配器文件将是

      public class profiles_adapter  extends ArrayAdapter<profiles> {
     Context context;
     Arraylist<profiles> profileArraylist;
    
     public profiles_adapter(Context context, int resourceId, ArrayList<profiles> 
     item) {
    
    this.context=context;
    this.profileArraylist = item;
     }
    Public View getView(int position, View convertView, ViewGroup parent) {
    
    final profiles profile = profileArraylist.get(position);
    
    
    if (convertView == null){
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.cards, parent, false);
    }
    
    TextView card_name = (TextView) convertView.findViewById(R.id.card_name);
    card_name.setText(profile.getPet_name());
    TextView card_age = (TextView) convertView.findViewById(R.id.card_age);
    card_age.setText(profile.getAge());
     }
    

    试试这个,它会帮助你找出问题所在。

    【讨论】:

    • 不,它不起作用,但调试器说:profiles.getView(),暂停线程
    • 好的,让我检查一下
    • 我应该发布卡片布局和滑动布局的xml吗?
    • 是的,你也可以发布超出错误的日志文件