1

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.

4

3 回答 3

2

您应该在 ListView 上使用 setAdapter 函数,让它使用您的自定义适配器

编辑:CustomAdapter 构造函数中有一个错字:代码应为

this.arr = arr

代替

arr = arr
于 2012-04-30T09:50:59.367 回答
0

在你的 getview 方法中,你的 sop 是 System.out.println(arr.get(posstion));。我认为应该是 System.out.println(arr.get(position));
因为在您的 getview 方法中定义了 int 位置而不是位置。检查代码中的拼写

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

即使这不起作用,也尝试给予 sop

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

}

如果大小为 0,那么您不会从活动类中获得任何结果到此适配器

于 2012-04-30T10:14:34.770 回答
0

你拼错了位置变化

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

经过

System.out.println(arr.get(position));
于 2012-04-30T09:42:45.207 回答