gpt4 book ai didi

android - RecyclerView 显示为空

转载 作者:行者123 更新时间:2023-11-29 15:42:28 25 4
gpt4 key购买 nike

我对 recyclerview 很陌生,所以我尝试转换一个 ListView ,我得到了这个:

    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;
}
}

在我的 Activity 中使用

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 也显示为空

最佳答案

您还没有为您的 RecyclerView 设置 LayoutManager。在设置适配器之前添加以下行。

rv.setLayoutManager(new LinearLayoutManager(this));

关于android - RecyclerView 显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38281686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com