recyclerview没有填充

时间:2017-04-13 15:55:16

标签: android android-recyclerview recycler-adapter

我正在使用recyclelerview适配器来填充数据库中的值:

package com.whiteboard.schedulemessage.util;

import android.database.Cursor;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.whiteboard.schedulemessage.R;
import com.whiteboard.schedulemessage.data.DatabaseTableContract;

/**
 * Created by Stark on 11/04/2017.
 */

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageAdapterHolders>{

    OnClickHandler handler;
    Cursor cursor;
    private static final String TAG = "MessageAdapter";

    public MessageAdapter(Cursor cursor, OnClickHandler handler){
        Log.e(TAG, "MessageAdapter: "+cursor.getCount());
        this.cursor = cursor;
        this.handler = handler;
    }

    public void updateCursor(Cursor cursor){
        if(this.cursor!=null)
            this.cursor.close();
        this.cursor = cursor;
        notifyDataSetChanged();
    }

    public interface OnClickHandler {
        void onClick(int id);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public MessageAdapterHolders onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.e(TAG, "onCreateViewHolder: ");
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.recyclerveiw_custom_layout, parent, false);
        MessageAdapterHolders holder = new MessageAdapterHolders(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MessageAdapterHolders holder, int position) {
        Log.e(TAG, "onBindViewHolder: "+position);
        if(cursor!=null && cursor.moveToPosition(position)){
            Log.e(TAG, "onBindViewHolder inside if: "+position);
            holder.toNumber.setText(cursor.getString(cursor.getColumnIndex(DatabaseTableContract.TableColumns.NUMBER_TO)));
            holder.message.setText(cursor.getString(cursor.getColumnIndex(DatabaseTableContract.TableColumns.MESSAGE)));
            holder.time.setText(cursor.getString(cursor.getColumnIndex(DatabaseTableContract.TableColumns.SEND_ON)));
            holder.status.setText(cursor.getString(cursor.getColumnIndex(DatabaseTableContract.TableColumns.STATUS)));
        }
    }

    @Override
    public int getItemCount() {
        Log.e(TAG, "getItemCount: "+cursor.getCount());
        if(cursor==null)
            return 0;
        return cursor.getCount();   
    }

    public class MessageAdapterHolders extends RecyclerView.ViewHolder
            implements View.OnClickListener {
        TextView toNumber, message, time, status;

        public MessageAdapterHolders(View itemView) {
            super(itemView);
            Log.e(TAG, "MessageAdapterHolders: Inner class" );
            toNumber = (TextView) itemView.findViewById(R.id.tv_to_number);
            message  = (TextView) itemView.findViewById(R.id.tv_message);
            time = (TextView) itemView.findViewById(R.id.tv_message_time);
            status = (TextView) itemView.findViewById(R.id.tv_status);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int adapterPosition = getAdapterPosition();
            if(cursor!=null && cursor.moveToPosition(adapterPosition)){
                int id = cursor.getInt(0);
                handler.onClick(id);
            }
        }
    }
}

并将主要活动称为:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scheduled_tasks);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    //DatabaseUtils.fakeData(this);

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    recyclerView.setHasFixedSize(true);

    cursor = DatabaseUtils.findAllMessages(this);
    adapter = new MessageAdapter(cursor, this);

    recyclerView.setAdapter(adapter);

    Log.e("cursor.getColumnCount()", "fakeData: "+cursor.getColumnCount());

}

我已经检查了以下内容:

  1. 检查getItemCount,如果没有返回0:此商品代码返回非零值

  2. 检查回收站视图布局

  3. 覆盖setItemId

  4. 但仍然没有代码。

    更新:

    通过记录我检查,onCreateViewHolderonBindViewHolder无效。

0 个答案:

没有答案