构建项目时,我得到:
/home/myapp/app/src/main/java/co/myapp/ui/TransactionListAdapter.java: 使用或覆盖已弃用的API。用-Xlint:deprecation重新编译 有关详细信息。
1)如何使用-Xlint:deprecation重新编译?
我试图在以下位置将该标志添加到Android / IntelliJ IDE中:
文件|设置|构建,执行,部署|编译器 命令行选项
此操作失败,并显示以下信息:
未知的命令行选项'-X'。
我试图在命令行上使用该标志:
./gradlew -Xlint:deprecation build
此操作失败,并显示相同的错误。
2)下面是我的代码。如何确定不推荐使用哪个API?
3)在onCreateViewHolder上,它说:
未注释的方法将覆盖使用@NonNull更少的注释方法... (Ctrl + F1)检查信息:此检查报告相关的问题 常量中配置的@Nullable和@NotNull批注用法 条件和例外检查。
我不知道该如何解决该问题,也不知道它是否与原始警告有关。
4)为了解决该问题,我创建了一个与现有类TransactionListAdapter相同的新类Foo。
简单地添加该文件会导致TransactionListAdapter的警告消失,而TransactionListAdapter不变。
package mypackage;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.List;
public class TransactionListAdapter extends RecyclerView.Adapter<TransactionListAdapter.TransactionViewHolder> {
private static ClickListener clickListener;
private static final String TAG = "TransactionListAdapter";
class TransactionViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private final View mItemView;
private final TextView transactionItemFrom;
private final TextView transactionItemTo;
private final TextView transactionItemAmount;
private TransactionViewHolder(View itemView) {
super(itemView);
mItemView = itemView;
itemView.setOnClickListener(this);
transactionItemFrom = itemView.findViewById(R.id.transaction_from);
transactionItemTo = itemView.findViewById(R.id.transaction_to);
transactionItemAmount = itemView.findViewById(R.id.transaction_amount);
}
@Override
public void onClick(View v) {
clickListener.onItemClick(getAdapterPosition(), v);
}
public Context getContext() {
return mItemView.getContext();
}
}
void setOnItemClickListener(ClickListener clickListener) {
TransactionListAdapter.clickListener = clickListener;
}
public interface ClickListener {
void onItemClick(int position, View v);
}
private final LayoutInflater mInflater;
private List<Transaction> mTransactions = Collections.emptyList();
TransactionListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
@Override
public TransactionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.recyclerview_item_transaction, parent, false);
return new TransactionViewHolder(itemView);
}
@Override
public void onBindViewHolder(TransactionViewHolder holder, int position) {
Transaction current = mTransactions.get(position);
holder.transactionItemFrom.setText("FROM: " + current.mFromLabel);
holder.transactionItemTo.setText("TO : " + current.mToLabel);
DecimalFormat df = new DecimalFormat("#,##0.00");
holder.transactionItemAmount.setText(df.format(current.mValue));
if (current.mValue.longValue() > 99999)
holder.transactionItemAmount.setTextAppearance(holder.getContext(), R.style.list_item_small);
else
holder.transactionItemAmount.setTextAppearance(holder.getContext(), R.style.list_item);
}
void setTransactions(List<Transaction> transaction) {
mTransactions = transaction;
notifyDataSetChanged();
}
Transaction getTransaction(int position) {
return mTransactions.get(position);
}
@Override
public int getItemCount() {
return mTransactions.size();
}
}