为我的RecyclerView
创建适配器之后,我注意到了一条警告。如何解决此警告?
(List) results.values
中的 myList.addAll((List) results.values);
返回警告:
未经检查的分配:“ java.util.List”到“ java.util.Collection”?扩展com.companyname.appname.Product>'
“产品”类
public class Product {
public Product(){}
private String mProductName;
private String mProductDescription;
public Product(String productName, String productDescription) {
this.mProductName = productName;
this.mProductDescription = productDescription;
}
public String getProductName() {
return mProductName;
}
public void setProductName(String item){
this.mProductName = item;
}
public String getProductDescription() {
return mProductDescription;
}
public void setProductDescription(String description){
this.mProductDescription = description;
}
}
活动课程
public class MyActivity extends AppCompatActivity {
private static final String TAG = MyActivity.class.getSimpleName();
private Boolean mCurrentValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.md);
}
@Override
protected void onStart() {
super.onStart();
setContentView(R.layout.md);
}
}
片段类
public class MyFragment extends Fragment {
public MyFragment() {}
private MyListAdapter mAdapter;
RecyclerView mRecyclerView;
public boolean mTwoPane;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
mTwoPane = Objects.requireNonNull(getActivity()).findViewById(R.id.detail_container) != null;
mRecyclerView = view.findViewById(R.id.recyclerView_list);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
mRecyclerView.addItemDecoration(new DividerItemDecoration(Objects.requireNonNull(getContext()), LinearLayout.VERTICAL));
ArrayList<Product> myList = new ArrayList<>();
String[] items = getResources().getStringArray(R.array.product_names);
String[] itemDescriptions = getResources().getStringArray(R.array.product_descriptions);
for (int n = 0; n < items.length; n++){
Product product = new Product();
product.setProductName(items[n]);
product.setFareZone(itemDescriptions[n]);
myList.add(product);
}
mAdapter = new MyListAdapter(getActivity(), myList, mTwoPane);
mRecyclerView.setAdapter(mAdapter);
return view;
}
}
适配器类
public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.ProductViewHolder> implements Filterable {
private Context mCtx;
private List<Product> myList;
private List<Product> myListFull;
private boolean mTwoPane;
class ProductViewHolder extends RecyclerView.ViewHolder {
RelativeLayout relativeLayout;
TextView textviewTitle, textviewSubtitle;
ProductViewHolder(View itemView) {
super(itemView);
relativeLayout = itemView.findViewById(R.id.listitem_dualline_relativelayout);
textviewTitle = itemView.findViewById(R.id.listitem_dualline_title);
textviewSubtitle = itemView.findViewById(R.id.listitem_dualline_subtitle);
}
}
public MyListAdapter(Context mCtx, List<Product> myList, boolean mTwoPane) {
this.mCtx = mCtx;
this.myList = myList;
this.mTwoPane = mTwoPane;
myListFull = new ArrayList<>(myList);
}
@NonNull
@Override
public MyListAdapter.ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View v = inflater.inflate(R.layout.listitem_dualline, parent,false);
return new MyListAdapter.ProductViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final MyListAdapter.ProductViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: called.");
final Product product = myList.get(holder.getAdapterPosition());
holder.textviewTitle.setText(product.getProductName());
holder.textviewSubtitle.setText(product.getProductDescription());
}
@Override
public int getItemCount() {
return myList.size();
}
@Override
public Filter getFilter() {
return exampleFilter;
}
private Filter exampleFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Product> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(myListFull);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (Product item : myListFull) {
if (item.getProductName().toLowerCase().contains(filterPattern)) {
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
myList.clear();
myList.addAll((List) results.values);
notifyDataSetChanged();
}
};
}
答案 0 :(得分:1)
替换:
myList.addAll((List) results.values);
具有:
myList.addAll((List<Product>) results.values);