Milap Milap - 6 months ago 49
Java Question

Java class uses unchecked or unsafe operations

I have a class called LB_Adapter.java made to help show my database in a ListView. All functionalities of my program work fine, but I still get an warning error everytime I compile that says

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


From googling I understand the error comes from my ArrayList, but I've tried implementing ArrayList() which doesn't work. Is there a way to get rid of the error, or does it even matter since it doesn't affect anything?

Relevant code:

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;
}
}

Answer

Using raw types has been discouraged since generics were introduced in Java 5. You should just type parameters for both the ArrayAdapter and the ArrayList you're using:

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);
    }
}