ListAdapter不膨胀

时间:2014-01-19 06:12:55

标签: android android-adapter listadapter

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

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;
    }

}

感谢。

4 个答案:

答案 0 :(得分:2)

您需要返回要显示的列表的大小

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

检查this for implementing the ListView with Custom BaseAdapter

选中此ViewHolder Pattern

答案 1 :(得分:0)

您将从0方法返回getCount(),这意味着您的ListView没有任何内容。请更正以返回列表中的项目数。

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

希望这有帮助。

答案 2 :(得分: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);

答案 3 :(得分: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;
    }
}