2014-01-19 93 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; 
    } 
}