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

时间:2016-05-13 16:21:07

标签: java android generics javac

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

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

从谷歌搜索我明白错误来自我的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;
}
}

1 个答案:

答案 0 :(得分:2)

由于在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);
    }
}