-1

I would like to ask 4 questions. I am building a adapter for my listview which i plan to have multi-columns. My friend has recommend me the following code, the code work but i still have queries about part of the code.

  1. Since public listviewAdapter(Activity activity, ArrayList<HashMap> list) does not return anything, shouldnt the code be public void.

  2. Doesn't super() have to have anything inside

  3. What does (convertView == null) does

  4. Last but not least, can I use other thing beside HashMap.

when i try to build my own adapter out of her code, i got the following errors:

  1. Constructor call must be the first statement in a constructor

  2. Return type for the method is missing

And under my main activity:

  1. The constructor listviewadapter(new View.OnClickListener(){}, ArrayList<HashMap>) is undefined

    public class listviewAdapter extends BaseAdapter{
    
    public ArrayList<HashMap> list;
    Activity activity;
    
    public listviewAdapter(Activity activity, ArrayList<HashMap> list) {
        super();
        this.activity = activity;
        this.list = list;
    }
    
    @Override
    public int getCount() {
        return list.size();
    }
    
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        return 0;
    }
    
    private class ViewHolder {
           TextView txtFirst;
           TextView txtSecond;
           TextView txtThird;
           TextView txtFourth;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            LayoutInflater inflater =  activity.getLayoutInflater();
            if (convertView == null){
                    convertView = inflater.inflate(R.layout.listview_row, null);
                    holder = new ViewHolder();
                    holder.txtFirst = (TextView) convertView.findViewById(R.id.FirstText);
                    holder.txtSecond = (TextView) convertView.findViewById(R.id.SecondText);
                    holder.txtThird = (TextView) convertView.findViewById(R.id.ThirdText);
                    holder.txtFourth = (TextView) convertView.findViewById(R.id.FourthText);
                    convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
    
            HashMap map = list.get(position);
            holder.txtFirst.setText((CharSequence) map.get(FIRST_COLUMN));
            holder.txtSecond.setText((CharSequence) map.get(SECOND_COLUMN));
            holder.txtThird.setText((CharSequence) map.get(THIRD_COLUMN));
            holder.txtFourth.setText((CharSequence) map.get(FOURTH_COLUMN));
            return convertView;
        }
    }
    
alex
  • 10,900
  • 15
  • 70
  • 100
lonelearner
  • 1,637
  • 4
  • 14
  • 22
  • 1
    `public listviewAdapter(Activity activity, ArrayList list) ` is your class constructor – gunar Mar 01 '14 at 10:49

2 Answers2

1
Since "public listviewAdapter(Activity activity, ArrayList list) " does not return anything, shouldnt the code be public void.

public listviewAdapter(Activity activity, ArrayList<HashMap> list)

Is a Constructor it does not have a return type.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8

Doesnt super() have to have anything inside

super need not have anything. I guess you are extending BaseAdapter. You can look at the public constructor

http://developer.android.com/reference/android/widget/BaseAdapter.html

What does (convertView == null) does

If view is null you inflate hence

if (convertView == null) // avaoid inflating and initializing view if view is not null

and you are using a ViewHolder which improves performance.

You will understand if you understand listview reycling

How ListView's recycling mechanism works

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Last but not least, can i use other thing beside HashMap.

Yes you can What you intend to use beside hashmap

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • I place the following code " ArrayList list = null; populateList(); listviewadapter adapter = new listviewadapter(this, list); moregold.setAdapter(adapter);" under my onclick listener – lonelearner Mar 01 '14 at 11:12
  • @lonelearner whatever error you have post it in the question section not as a comment. What you are doing is wrong. Wrong params – Raghunandan Mar 01 '14 at 15:36
0

Answer to the fifth question:

1.the class name and constructor name are not the same. My class name is Adapter and constructor name is adapter.

  1. the declaration of arraylist list should be put under my mainactivity class and not in oncreate. (Field id instead of local variable).
lonelearner
  • 1,637
  • 4
  • 14
  • 22