RecyclerView与光标

时间:2016-01-11 15:12:37

标签: android android-recyclerview

我一直在努力了解RecyclerView的流程和实施。我有光标数据所以我改变了代码,因为我可以忍受它,因为我是编程和开发的新手。所以我试过,这里是代码。但没有任何事情发生。

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

private Cursor cursor;
private View view;
public MyContactsRecyclerViewAdapter(Cursor cur){
       cur = cursor;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.fragment_contacts, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
while(cursor.movetoNext()){
        String nam = cursor.getString(cursor.getColumnIndex(mDatabase.Tableinfo.Contacts_name));
        String phon = cursor.getString(cursor.getColumnIndex(mDatabase.Tableinfo.Contacts_phone));
        byte[] b = cursor.getBlob(cursor.getColumnIndex(mDatabase.Tableinfo.Contact_pic));
        holder.name.setText(nam);
        holder.phone.setText(phon);
    if (b==null) {
        Profile bla = new Profile();
     holder.pic.setImageBitmap(bla.getRoundedShape(BitmapFactory.decodeResource(Resources.getSystem(), R.drawable.samplepro),60,60));
    }   else   {
     holder.pic.setImageBitmap(BitmapFactory.decodeByteArray(b,0,b.length));
    }
  }
}
@Override
public int getItemCount() {
    if(cursor != null){
    return cursor.getCount();}
    return 0 ;
}


public class ViewHolder extends RecyclerView.ViewHolder {
    public final View mView;
    public TextView name;
    public TextView phone;
    public ImageView pic;

    public ViewHolder(View view) {
        super(view);
        mView = view;
        pic = (ImageView) view.findViewById(R.id.image);
        phone = (TextView) view.findViewById(R.id.phone);
        name = (TextView)view.findViewById(R.id.name);
    }
}}

并在主要活动中.....

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_contacts_list, container, false);
    DataBaseOperations hell = new DataBaseOperations(getActivity());
    SQLiteDatabase db = hell.getReadableDatabase();
    String[] columns = {mDatabase.Tableinfo.ID,mDatabase.Tableinfo.Contacts_phone,mDatabase.Tableinfo.Contacts_name,mDatabase.Tableinfo.Contact_pic};
    Cursor Contacts = db.query(mDatabase.Tableinfo.contacts, columns, null, null, null, null, null);
      LinearLayoutManager llv = new LinearLayoutManager(getContext());
        llv.setOrientation(LinearLayoutManager.VERTICAL);
        contacts.setLayoutManager(llv);
        MyContactsRecyclerViewAdapter lv = new     MyContactsRecyclerViewAdapter(Contacts);
        contacts.setAdapter(lv);

            return view;
} 

1 个答案:

答案 0 :(得分:0)

private Cursor cursor;
private View view;
public MyContactsRecyclerViewAdapter(Cursor cur){
   cur = cursor;
}

cur = cursor; 不正确。复制粘贴错误或您的代码无效,因为您没有正确更新变量光标吗?

此行应为cursor=cur;