prakash
prakash

Reputation: 464

getview method is not calling in custom listview method in android

I am having an Arraylist as [x,y,z..]. I want this ArrayList [x,y,z..] in ListView but getview() method is not getting invoked
I am trying this code :

public class CustomAdapter extends BaseAdapter
{
public static ArrayList<String> arr=new ArrayList<String>();

public Context Context;
private LayoutInflater inflater;

HashMap<String, String> map = new HashMap<String, String>();
public CustomAdapter(Context context, ArrayList<String> arr) 
{
    Context=context;
    inflater=LayoutInflater.from(context);
    arr=arr;

}
public int getCount() 
{
    // TODO Auto-generated method stub
    return arr.size();
}

public Object getItem(int position) 
{
    // TODO Auto-generated method stub
    return arr.get(position);
}

public long getItemId(int position) 
{
    // TODO Auto-generated method stub
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) 
    {
    System.out.println(arr.get(posstion));
        ViewHolder holder;

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

   holder.textViewSelectedText = (TextView)convertView.findViewById(R.id.selectedtext);
            convertView.setTag(holder);
        }
        else 
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textViewSelectedText.setText(arr.get(position));
        return convertView;
    }

    class ViewHolder
    {
        TextView textViewSelectedText = null;
    }
}

System.out.println(arr.get(posstion));

This line is inside getview() method and it is not printing the values.. please help me.

Upvotes: 1

Views: 1604

Answers (3)

miniBill
miniBill

Reputation: 1734

You should use the setAdapter function on the ListView to let it use your custom adapter

EDIT: There is a typo in the CustomAdapter constructor: the code should read

this.arr = arr

instead of

arr = arr

Upvotes: 2

user1215522
user1215522

Reputation: 158

inside ur getview method your sop is System.out.println(arr.get(posstion));. i think it should be System.out.println(arr.get(position));
because in your getview method int position is defined and not posstion. check the spelling in your code

public View getView(int position, View convertView, ViewGroup parent){
   System.out.println(arr.get(position));
}

even if this doesnt work try giving sop in

public CustomAdapter(Context context, ArrayList<String> arr) 
{     
   Context=context;  
   inflater=LayoutInflater.from(context);  
   arr=arr;  
   System.out.println("arr: " + arr.size);

}

if size is 0 then u r not getting any result from activity classto this adapter

Upvotes: 0

Nishant
Nishant

Reputation: 32222

You have wrongly spelled the position change

System.out.println(arr.get(posstion));

by

System.out.println(arr.get(position));

Upvotes: 0

Related Questions