Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to pull data from Parse.com cloud to my android application.This is the code for my custom adapter:

public class AdvertisingAdapter extends RecyclerView.Adapter<AdvertisingAdapter.TatsViewHolder> {

List<AdvertInfo> data = new ArrayList<>();
private LayoutInflater inflater;

public AdvertisingAdapter(Context context, List<AdvertInfo> data){
    inflater = LayoutInflater.from(context);
    this.data = data;

}

@Override
public TatsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = inflater.inflate(R.layout.single_ad, parent, false);
    TatsViewHolder holder = new TatsViewHolder(view);
    return holder;
}


@Override
public void onBindViewHolder(TatsViewHolder holder, int position) {
    AdvertInfo current = data.get(position);
    holder.daPoster.setText(current.theadvertiser);
    holder.daAdvert.setText(current.theadvert);

}

@Override
public int getItemCount() {
    return data.size();
}

class TatsViewHolder extends RecyclerView.ViewHolder{

    TextView daPoster;
    TextView daAdvert;

    public TatsViewHolder(View itemView) {
        super(itemView);

        daPoster = (TextView)itemView.findViewById(R.id.txtAdvertiser);
        daAdvert = (TextView)itemView.findViewById(R.id.txtAdvert);
    }
}
}

and the code for my member class

public class AdvertInfo {

public String theadvertiser;
public String theadvert;

}

then for my fragment where i am displaying the data here is the code

public class ViewAdverts extends android.support.v4.app.Fragment {


private RecyclerView myRecyclerView;
AdvertisingAdapter myAdapter;

public ViewAdverts() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View layout = inflater.inflate(R.layout.view_adverts, container, false);
    myRecyclerView = (RecyclerView)layout.findViewById(R.id.advert_list);
    myAdapter = new AdvertisingAdapter(getActivity(), getData());
    myRecyclerView.setAdapter(myAdapter);
    myRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    return layout;
}

public static List<AdvertInfo> getData(){
    final List<AdvertInfo> myposts = new ArrayList<>();



    ParseQuery<Posts> query = ParseQuery.getQuery(Posts.class);
    query.findInBackground(new FindCallback<Posts>() {
        @Override
        public void done(List<Posts> list, ParseException e) {

            if (e == null){
                for (int i = 0; i < list.size(); i++){
                    AdvertInfo ad = new AdvertInfo();
                    ad.theadvertiser = list.get(i).getString(ParseConstants.ADVERTISER);
                    ad.theadvert = list.get(i).getString(ParseConstants.DESCRIPTION);
                    myposts.add(ad);


                }
            }

        }
    });

        return myposts;
}

}

I have tried to debug by putting breaks on getData method in the fragment I am get the data but I dont know why my recycler view appearing empty when I run.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
192 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...