RecyclerView显示为空

时间:2016-07-09 12:11:02

标签: android android-recyclerview

我对recyclerview很新,所以我尝试转换列表视图,我得到了这个:

    public class WalletListRecyclerViewAdapter extends RecyclerView.Adapter<WalletListRecyclerViewAdapter.WalletItemHolder> {

    ArrayList<WalletItem> walletItemList;
    MainActivity context;
    private static LayoutInflater inflater=null;
    public WalletListRecyclerViewAdapter(MainActivity mainActivity, ArrayList<WalletItem> walletItemList) {
        // TODO Auto-generated constructor stub

        context = mainActivity;
        this.walletItemList = walletItemList;
        inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public WalletItemHolder onCreateViewHolder(final ViewGroup viewGroup, final int i) {

        View rowView;

        final DialogInterface.OnClickListener walletItemOnEditClickHandler = new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                WalletItem.onClickEditButton(viewGroup,context,walletItemList,i);
            }

        };

        final DialogInterface.OnClickListener walletItemOnDeleteClickHandler = new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                walletItemList.remove(i);
                RecyclerView rv = (RecyclerView) context.findViewById(R.id.recyclerView);
                context.recreate();
            }

        };

        View.OnClickListener walletItemOnClickHandler = new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setNeutralButton("Edit",walletItemOnEditClickHandler);
                builder.setPositiveButton("Delete",walletItemOnDeleteClickHandler);

                builder.show();
            }
        };

        rowView = inflater.inflate(R.layout.wallet_item, null);

        return new WalletItemHolder(rowView);
    }


    @Override
    public void onBindViewHolder(WalletItemHolder holder, int i) {
        WalletItem wi = walletItemList.get(i);
        SimpleDateFormat dateFormat = new SimpleDateFormat("E dd/MM/yyyy hh:mm");
        holder.tvDescription.setText(wi.getDescription());
        holder.tvAmount.setText(""+walletItemList.get(i).getAmount());
        holder.tvDate.setText(dateFormat.format(walletItemList.get(i).getDate()));
        holder.ivIcon.setImageResource(walletItemList.get(i).getImageResource());
    }

    public static class WalletItemHolder extends RecyclerView.ViewHolder {
        public TextView tvDescription;
        public TextView tvAmount;
        public TextView tvDate;
        public ImageView ivIcon;

        /**
        *Constructor
         **/
        public WalletItemHolder(View v) {
            super(v);
            tvDescription =  (TextView) v.findViewById(R.id.textDescription);
            tvAmount = (TextView)  v.findViewById(R.id.textAmount);
            tvDate = (TextView)  v.findViewById(R.id.textDate);
            ivIcon = (ImageView) v.findViewById(R.id.imageView1);
        }
    }

    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return walletItemList.size();
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
}

在我的活动中使用

public class MainActivity extends AppCompatActivity {

private RecyclerView rv;
private Context context;
private MainActivity mainActivity = this;

private String m_DialogText = "";
private double m_DialogAmount = 0;
private WalletItem m_DialogResult = null;

private boolean drawn = false;

public static ArrayList<WalletItem> wlltList = new ArrayList<WalletItem>() ;// WalletItem.listBuilder(prgmImages, amountDummy, prgmNameList, dates);

private GoogleApiClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    context = this;

    wlltList.add(new WalletItem(R.drawable.testicongreen,15.0,"pippo",new Date()));

    rv = (RecyclerView) findViewById(R.id.recyclerView);
    if(wlltList != null) {
        rv.setAdapter(new WalletListRecyclerViewAdapter(this, wlltList));
    }

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            WalletItem.onClickAddButton(view,mainActivity,wlltList);
        }
    });

    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

ListView工作得很整齐,但是我无法弄清楚我做错了什么,因为即使我添加了一些元素,RecyclerView也显示出来了

3 个答案:

答案 0 :(得分:2)

您尚未为#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int t, n, m, s; cin>>t; while(t--){ cin>>n>>m>>s; int count = s; for(int i = 1; i < m; i++){ count = (count + 1) % n; } cout << count << endl; } return 0; } 设置LayoutManager。在设置适配器之前添加以下行。

RecyclerView

答案 1 :(得分:1)

声明 Recycler 后查看使用:

LinearLayoutManager layoutManager
    = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

之后:

rv.setLayoutManager(的layoutManager);

答案 2 :(得分:1)

为了显示recyclerView,您需要一个适配器让recyclerview知道如何将数据与视图中的每个项目相关联。此外,您需要一个布局管理器让recyclerView显示项目(它们如何对齐)。例如, 您可以使用LinearLayoutManager指定方向(水平或垂直),也可以使用GridLayoutManager指定列数。

在您的情况下,您错过了布局管理器

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

使用垂直线性布局管理器可以为常规列表视图提供类似的外观和感觉

声明linearLayoutManager后,您需要致电

rv.setLayoutManager(linearLayoutManager);