gpt4 book ai didi

Java 类使用未经检查或不安全的操作

转载 作者:行者123 更新时间:2023-12-02 03:37:56 27 4
gpt4 key购买 nike

我有一个名为 LB_Adapter.java 的类,用于帮助在 ListView 中显示我的数据库。我的程序的所有功能都工作正常,但每次编译时我仍然收到警告错误

Note: Project/LB_Adapter.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

通过谷歌搜索,我了解到错误来 self 的 ArrayList,但我尝试实现 ArrayList() ,但它不起作用。有没有办法消除错误,或者它是否重要,因为它不会影响任何东西?

相关代码:

public class LB_Adapter extends ArrayAdapter<Object> {
List<Object> list = new ArrayList<>();

public LB_Adapter(Context context, int resource){
super(context, resource);
}

public void add(LB object){
list.add(object);
super.add(object);
}

@Override
public int getCount(){
return list.size();
}

@Override
public Object getItem(int position){
return list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LBHolder lbholder;

if(row==null){
LayoutInflater layoutinflater = (LayoutInflater) this.getContext().getSystemService(getContext().LAYOUT_INFLATER_SERVICE);
row = layoutinflater.inflate(R.layout.display_leaderboard_row, parent, false);
lbholder = new LBHolder();
lbholder.lb_rank = (TextView) row.findViewById(R.id.lb_rank);
lbholder.lb_score = (TextView) row.findViewById(R.id.lb_score);
lbholder.lb_time = (TextView) row.findViewById(R.id.lb_time);
row.setTag(lbholder);
}
else {
lbholder = (LBHolder) row.getTag();
}
LB leaderboard = (LB) getItem(position);
lbholder.lb_rank.setText(leaderboard.getRank().toString());
lbholder.lb_score.setText(Integer.toString(leaderboard.getScore()));
lbholder.lb_time.setText(leaderboard.getTime().toString());

return row;
}

static class LBHolder{
TextView lb_rank, lb_score, lb_time;
}
}

最佳答案

自 Java 5 中引入泛型以来,不鼓励使用原始类型。您应该只为您使用的 ArrayAdapterArrayList 键入参数:

public class LB_Adapter extends ArrayAdapter<LB> {
// Here --------------------------------^

// And here:
List<LB> list = new ArrayList<>();

public LB_Adapter(Context context, int resource){
super(context, resource);
}

public void add(LB object){
list.add(object);
super.add(object);
}
}

关于Java 类使用未经检查或不安全的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37214904/

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