1

我正在将数组列表项 [x,y,z...] 发送到自定义适配器。我想在列表视图中显示项目。我可以使用 for loop(x,y,z...like).in side getview 方法打印所有项目我可以 pritem itemnames[possition] 意味着它不能打印数组列表...

    i try 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)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //  imageLoader=new ImageLoader(activity);
        inflater=LayoutInflater.from(context);

     arr=arr;
    for(int i=0;i<arr.size();i++)
    {
       arr.get(i);

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

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

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}
  public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

View vi=convertView;
vi=inflater.inflate(R.layout.selecteditemlistview, null);
System.out.println(arr.get(position));
TextView text=(TextView)vi.findViewById(R.id.selectedtext);
text.setText(arr.get(position));
return vi;
  }
 }

我在哪里做错了请告诉我...

4

1 回答 1

0

您需要返回Method 而arraylist size不是.getCount()return 0

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);
        arrr=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) 
        {
            ViewHolder holder;

            if (convertView == null) 
            {
                convertView = mInflater.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;
        }
 }
于 2012-04-30T07:35:37.843 回答