0
\$\begingroup\$

I wrote this class for a custom list adapter for my list view. I use the view holder pattern to make the list view move smoother.

Could anyone advice tell me if I did something wrong, or some way to refine my code?

Example: defining my views. Is there way to make it faster?

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.validate.R;

import java.util.List;

/**
 * Created by Mina on 30/06/2015.
 */
public class vault_listadapter extends BaseAdapter {
    private List<Vault_item_class> vault_items;
    private Context context;
    private LayoutInflater inflater;

    TextView merchantName_tv, voucherType_tv, voucherName_tv, voucherDetail_tv, voucherValue_tv,
            issueDate_tv, expiryDate_tv, voucherCode_tv;
    ImageView barcode_imageview;

    public void vault_listadapter(Context context, List<Vault_item_class> vault_items) {
        this.context = context;
        this.vault_items = vault_items;
        this.inflater = LayoutInflater.from(context);

    }

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

    @Override
    public Vault_item_class getItem(int position) {
        return vault_items.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.vault_item, null, false);
            holder = new ViewHolder();

            holder.merchantName_tv = (TextView) convertView.findViewById(R.id.merchantName_id);
            holder.voucherName_tv = (TextView) convertView.findViewById(R.id.voucherName_id);
            holder.voucherType_tv = (TextView) convertView.findViewById(R.id.voucherType_id);
            holder.voucherDetail_tv = (TextView) convertView.findViewById(R.id.voucherDetails_id);
            holder.voucherValue_tv = (TextView) convertView.findViewById(R.id.voucherVaule_id);
            holder.issueDate_tv = (TextView) convertView.findViewById(R.id.issueDate_id);
            holder.expiryDate_tv = (TextView) convertView.findViewById(R.id.expiryDate_id);
            holder.voucherCode_tv = (TextView) convertView.findViewById(R.id.voucherCode_id);

            holder.barcode_imageview = (ImageView) convertView.findViewById(R.id.barcodeimg_id);

            convertView.setTag(holder);
        } else {
           holder = (ViewHolder) convertView.getTag();
        }


        return convertView;
    }

    static class ViewHolder {
        TextView merchantName_tv, voucherType_tv, voucherName_tv, voucherDetail_tv, voucherValue_tv,
                issueDate_tv, expiryDate_tv, voucherCode_tv;
        ImageView barcode_imageview;
    }
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Had you try the new RecyclerView? It has the ViewHolder pattern implemented and probably can help you in know what is the better form to implement it \$\endgroup\$
    – Aracem
    Commented Jun 30, 2015 at 13:42

1 Answer 1

2
\$\begingroup\$

Here is a complex example for your case:

List Adapter with ViewHolder Pattern

Note:

  • Never use a parameter as a variable (parameters shouldn't be modified!)
  • I like to use an underscore at the beginning of global fields (variables, objects) names to better show their scope.
  • You should make a class name without any special characters and beginning with an uppercase letter.
  • Try to get context from the closest possible parameter (as in my example from the parent of the getView() method).
  • The class constructor is defined without a return type (with the same name as the class name and any parameter if you desire)!
  • For later data changes in the adapter, you can define a method to add items to the list (as an example).
\$\endgroup\$
2
  • \$\begingroup\$ why are you recreating ViewHolder every-time \$\endgroup\$ Commented Jul 21, 2016 at 6:46
  • \$\begingroup\$ @prathamkesarkar - only then when the tag doesn't match defined one \$\endgroup\$
    – ceph3us
    Commented Jul 21, 2016 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.