2014-01-19 36 views
0

我正在从hashmap的arraylist显示列表。我创建了自己的适配器来显示列表。但我无法在列表中显示它。我甚至无法在该适配器内进行调试。我的代码是这样的。ListAdapter不充气

public class ListAdapter extends BaseAdapter { 

    Context _context; 
    ArrayList<HashMap<String, String>> lists = new ArrayList<HashMap<String, String>>(); 
    public LayoutInflater myInflater; 
    public ListAdapter(Context context,ArrayList<HashMap<String, String>> list){ 
     this._context = context; 
     this.lists = list; 
    } 
    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

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

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     this.myInflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (convertView==null) 
     { 
      convertView = myInflater.inflate(R.layout.list_items, parent, false); 
      TextView TT = (TextView) convertView.findViewById(R.id.system_text); 
      System.out.println("systemName name is"+this.lists.get(position).get("SystemName")); 
      TT.setText(this.lists.get(position).get("SystemName")); 
     }  
     return convertView; 
    } 

} 

谢谢。

+0

您getView小鬼说法也是错误的。移动这个'TT.setText(this.lists.get(position).get(“SystemName”));'out of if – Raghunandan

回答

0

您从getCount()方法返回0,这意味着您的ListView没有物品。请纠正该错误,以返回列表中的项目数量。

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return items.size(); // HERE 
} 

希望这会有所帮助。

0

更换

convertView = myInflater.inflate(R.layout.list_items, parent, false); 

通过

convertView = myInflater.inflate(R.layout.list_items, null); 

例子:

LayoutInflater mInflater = (LayoutInflater) context 
      .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
    return mInflater.inflate(view, null); 
0

这可能会帮助你...

public class ListAdapter extends BaseAdapter { 

    private ArrayList<HashMap<String, String>> lists = new ArrayList<HashMap<String, String>>(); 
    private final LayoutInflater myInflater; 

    public ListAdapter(Context context, ArrayList<HashMap<String, String>> list) { 
     this.lists = list; 
     myInflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

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

    @Override 
    public Object getItem(int position) { 
     return lists.get(position); 
    } 

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewWrapper viewWrapper = null; 
     if (convertView == null) { 
      convertView = myInflater.inflate(R.layout.list_items, null); 
      TextView textView = (TextView) convertView.findViewById(R.id.system_text); 
      viewWrapper = new ViewWrapper(); 
      viewWrapper.textView = textView; 
      convertView.setTag(viewWrapper); 
     } else { 
      viewWrapper = (ViewWrapper) convertView.getTag(); 
     } 
     System.out.println("systemName name is"+ lists.get(position).get("SystemName")); 
     viewWrapper.textView.setText(lists.get(position).get("SystemName")); 
     return convertView; 
    } 

    private static class ViewWrapper { 
     TextView textView; 
    } 
}