如何检索解析对象以使用片段回收视图

时间:2015-12-09 04:33:15

标签: java android parse-platform

我尝试在recyclerview上检索解析对象。但数据没有显示。在logcat

12-08 22:14:41.222 23950-23950/com.keboo.ime E/RecyclerView: No adapter attached; skipping layout
12-08 22:14:41.313 23950-23950/com.keboo.ime E/RecyclerView: No adapter attached; skipping layout


public class TerasFragment extends Fragment {

public TerasAdapter terasAdapter;
private RecyclerView recyclerView;

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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ParseQuery<ParseObject> query = ParseQuery.getQuery("teras");
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, ParseException e) {
            if (e != null) {
                Toast.makeText(getActivity().getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            } else {
                terasAdapter = new TerasAdapter(getActivity().getApplicationContext(), list);
            }
        }
    });
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_teras, container, false);

    LinearLayoutManager llm = new LinearLayoutManager(getActivity().getBaseContext());
    llm.setOrientation(LinearLayoutManager.VERTICAL);

    recyclerView = (RecyclerView) view.findViewById(R.id.recycle_status);
    recyclerView.setLayoutManager(llm);
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(terasAdapter);
    return view;
}
}


  public class TerasAdapter extends RecyclerView.Adapter<TerasAdapter.ViewHolder>{

List<ParseObject> mlist;
Context mContext;
LayoutInflater inflater;

public TerasAdapter(Context context, List<ParseObject> list){
    this.mlist = list;
    this.mContext = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = inflater.from(parent.getContext()).inflate(R.layout.teras_item, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    ParseObject status = mlist.get(position);
    holder.name.setText(status.getString("nama"));
    holder.status.setText(status.getString("status"));
    ParseFile picture = (ParseFile) status.get("picture");
    picture.getDataInBackground(new GetDataCallback() {
        @Override
        public void done(byte[] bytes, ParseException e) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
            holder.profil.setImageBitmap(bitmap);
        }
    });
}

@Override
public int getItemCount() {
    return 0;
}

public static class ViewHolder extends RecyclerView.ViewHolder{

    CardView mCardView;
    CircleImageView profil;
    TextView name, type, time, status;

    public ViewHolder(View itemView) {
        super(itemView);
        mCardView = (CardView) itemView.findViewById(R.id.cv_status);
        profil = (CircleImageView) itemView.findViewById(R.id.de_status_profil);
        name = (TextView) itemView.findViewById(R.id.tv_status_nama);
        type = (TextView) itemView.findViewById(R.id.tv_status_type);
        time = (TextView) itemView.findViewById(R.id.tvstatus_time);
        status = (TextView) itemView.findViewById(R.id.tv_status_deskription);
    }
}
}

1 个答案:

答案 0 :(得分:1)

看起来你在UI线程以外的线程中进行解析,因此它与UI线程异步工作,所以你必须在处理解析后设置适配器,否则你必须通知适配器。您收到消息跳过布局,因为您的Recyclerview中没有数据显示。所以你需要做的是

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ParseQuery<ParseObject> query = ParseQuery.getQuery("teras");
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, ParseException e) {
            if (e != null) {
                Toast.makeText(getActivity().getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
            } else {
                setListView(list);
            }
        }
    });
}



public void setListView(List<ParseObject< list){
  terasAdapter = new TerasAdapter(getActivity().getApplicationContext(), list);
  recyclerView = (RecyclerView) view.findViewById(R.id.recycle_status);
  recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
  recyclerView.setHasFixedSize(true);
  recyclerView.setAdapter(terasAdapter);
}

并且适配器的 getItemCount()方法出错,而不是返回0,您必须返回列表的大小。

例如:

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