我有這個自定義列表適配器,我出於某種原因可以使txtTitle與偵聽器一起工作,但按鈕總是返回空對象。有沒有一種特定的方式來在像這樣的回收站中添加按鈕?我以與其他回收器適配器相同的方式實施它,除非它們中沒有按鈕。空對象上的onClick(int)引用
public class resource_list_adapter extends RecyclerView.Adapter<resource_list_adapter.ViewHolder> {
private List<resource_list> listItems;
private Context context;
resource_list_adapter.OnItemClickListener onItemClickListener;
public resource_list_adapter(List<resource_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
@Override
public resource_list_adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.resource_list, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
resource_list listItem = listItems.get(position);
Double resource = listItem.getCurrent().doubleValue()/listItem.getMax().doubleValue();
holder.txtTitle.setText(listItem.getTxtTitle() + ": " + listItem.getCurrent().toString() + "/" + listItem.getMax().toString() + " (" + String.valueOf((int)(100*resource)) + "%)");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
holder.pgResource.setProgressTintList(ColorStateList.valueOf(Color.rgb(listItem.getRed(),listItem.getGreen(),listItem.getBlue())));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
holder.pgResource.setProgress((int)(100*resource), true);
}
holder.btnAddResource.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onResourceClick(position);
}
});
holder.btnSubtractResource.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClickListener.onResourceClick(position);
}
});
holder.btnAddResource.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onItemClickListener.onLongResourceClick(position);
return true;
}
});
holder.btnSubtractResource.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onItemClickListener.onLongResourceClick(position);
return true;
}
});
}
@Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtTitle;
public ProgressBar pgResource;
public Button btnAddResource;
public Button btnSubtractResource;
public ViewHolder(View itemView) {
super(itemView);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
btnAddResource = (Button) itemView.findViewById(R.id.btnAdditionResource);
btnSubtractResource = (Button) itemView.findViewById(R.id.btnSubtractResource);
pgResource = (ProgressBar) itemView.findViewById(R.id.pgResource);
}
}
public void setOnItemClickListener(resource_list_adapter.OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
public interface OnItemClickListener{
void onResourceClick(int position);
void onLongResourceClick(int position);
}
}
這就是我在我的活動中實現它的方式。
我對我的活動執行implements resource_list_adapter.OnItemClickListener
。
聲明變量:
private RecyclerView recyclerResource;
private resource_list_adapter resourceAdapter;
private List<resource_list> resourceList;
private List<String> resourceKeyList;
這是我如何調用的方法 - 同一個地方我稱之爲從其他適配器所有其他方法。
@Override
public void onResourceClick(int position) {
Toast.makeText(getContext(), "Test", Toast.LENGTH_SHORT).show();
/*Intent intent = new Intent(getActivity(), AttributeDescription.class);
intent.putExtra("SelectedCharacter", Home_Tab.activeCharacter);
intent.putExtra("name", attributeKeyList.get(position).toString());
intent.putExtra("category", "Attribute");
if (character.Edit.equals("True")){
intent.putExtra("mode", "Edit");
}
else{
intent.putExtra("mode", "View");
}
startActivity(intent);*/
}
@Override
public void onLongResourceClick(final int position) {
/*if (character.Edit.equals("True")){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Remove Attribute");
builder.setPositiveButton("Remove", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
character.removeAttribute(attributeKeyList.get(position).toString(), "Attribute");
attributeKeyList.remove(position);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
AlertDialog dialog = builder.create();
dialog.show();
}*/
}
這是我如何在我的init()方法中設置偵聽器到我的適配器(與其餘部分相同)。
resourceAdapter = new resource_list_adapter(resourceList, getContext());
resourceAdapter.setOnItemClickListener(this);
你在哪裏設置'resource_list_adapter.OnItemClickListener onItemClickListener'? –
在按鈕的onClick上。 'holder.btnAddResource.setOnClickListener' – Torewin
「空對象引用上的onClick(int)」 - 代碼中的任何地方都沒有'onClick(int)'方法。您需要發佈完整的堆棧跟蹤,以便我們看到究竟是什麼崩潰,以及在哪裏。 –