ArrayList<ArrayList<String>> vs ArrayList<Object>

372 views Asked by At

Will there be any problem if I use ArrayList<ArrayList<String>> in place of ArrayList<Object> for populating a recyclerView? or am I the only weirdo present here?

I have image links in each of the list items. Some also don't have any image links.

I'm saving the images for the first time when displaying the list then fetching the images from sdcard if image name matches. I also have unique image names.

When I'm scrolling my view, the items that does not contain any images also showing the previously loaded images but it is not duplicating the texts.

I'm also using

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    return position;
}

These functions in my adapter.

Adapter.java

public class ChatBoxAdapter extends RecyclerView.Adapter<ChatBoxAdapter.MyViewHolder> {

ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public class MyViewHolder extends RecyclerView.ViewHolder {

    public TextView text,date;
    public ImageView image;

    public MyViewHolder(View view) {
        super(view);

        text = (TextView) view.findViewById(R.id.text);
        date = (TextView) view.findViewById(R.id.date);
        image = (ImageView) view.findViewById(R.id.image);
    }
}

Context context;
Bitmap bitmap;

public ChatBoxAdapter(Context _context) {
    context = _context;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_chat_box, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

    String comment = Base.chatdatalist.get(position).get(3);
    String image_url = Base.chatdatalist.get(position).get(4);
    String date = Base.chatdatalist.get(position).get(5);

    File imageFile = null;

    if (image_url.startsWith(Base.baseUrl)) {

        String image_name = image_url.substring(image_url.lastIndexOf("/")+1);
        imageFile = new File(Environment.getExternalStorageDirectory().getPath() + "/Pictures/TMS Sent Images/" + image_name);
        bitmap = null;
        if (imageFile.exists()) {

            BitmapFactory.Options options = new BitmapFactory.Options();

            long img_size = imageFile.length() / 1024;
            if (img_size <= 200)
                options.inSampleSize = 4;
            else
                options.inSampleSize = 8;

            bitmap = BitmapFactory.decodeFile(imageFile.toString(), options);
        }
    }

    holder.text.setText(Html.fromHtml(comment, null, new MyTagHandler()));

    if (position !=0 && !chat_date.equals(Base.chatdatalist.get(position - 1).get(15)))
        holder.date.setVisibility(View.VISIBLE);
    if (position == 0)
        holder.date.setVisibility(View.VISIBLE);


    if (bitmap != null) {
        holder.image.setImageBitmap(bitmap);
        holder.image.setVisibility(View.VISIBLE);
    } else
        if (image_url.startsWith(Base.baseUrl)) {
            holder.image.setVisibility(View.VISIBLE);
            Picasso.with(context).load(image_url).into(holder.image);
        }
}

@Override
public int getItemCount() {
    return Base.chatdatalist.size();
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    return position;
}
}

What can be the problem?

Thanks in advance.

1

There are 1 answers

7
BiGGZ On

Well, if you dont mind casting every time: ArrayList<String> foo = (ArrayList<String>) commentData.get(i) as opposed to ArrayList<String> x = commentsData.get(i) sure, use ArrayList<Object>