0

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 every time 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;
}
}
Mureinik
  • 252,575
  • 45
  • 248
  • 283
Milap
  • 147
  • 1
  • 6
  • 14
  • 4
    Did you try recompiling with `-Xlint` to get more details? Why are you using raw types at all? – Jon Skeet May 13 '16 at 16:23
  • to do that I just type javac project -Xlint into the terminal? Also, not sure about the raw types, I used a tutorial to get this code. – Milap May 13 '16 at 16:31
  • The reason I'm confused about compiling is this is a program with Android Studios, so it's not just a simple java program it involves gradle and stuff – Milap May 13 '16 at 16:36
  • I strongly suspect there's a way of using Xlint in hour build configuration. I suggest you research that. – Jon Skeet May 13 '16 at 16:39

1 Answers1

2

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);
    }
}
Mureinik
  • 252,575
  • 45
  • 248
  • 283
  • I posted more of my class. When I put an error comes up on my public Object getItem(int position) method(see edit). I changed that to and built the apk and there is no more error. Do you think this is a recommended way to solve the error? – Milap May 13 '16 at 16:40
  • 1
    @Milap no - [`getItem`](http://developer.android.com/reference/android/widget/ArrayAdapter.html#getItem(int)) should an `LB` in your usecase. – Mureinik May 13 '16 at 16:42
  • Ok. It compiles without error that way as well. Thanks for your help! – Milap May 13 '16 at 16:45