Адаптер не анализирует массив, который я отправляю ему - PullRequest
0 голосов
/ 27 февраля 2020

Я использую эту библиотеку свайпов 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());
}

1 Ответ

0 голосов
/ 27 февраля 2020

Есть некоторые изменения в вашем коде, как показано ниже,

                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());
 }

Попробуйте, это поможет вам выявить проблему.

...