在列表视图中的android适配器

时间:2012-09-12 11:46:58

标签: android eclipse android-layout listview simplecursoradapter

我在ListView中使用了适配器。问题是没有显示行并且没​​有引发异常,但是我发送给适配器的记录有10条。当我调试它时,我不知道为什么getView()没有执行
我已经设置了这样的适配器

abc = layoutInflater.inflate(R.layout.mainlayoutcontainer, container,false);<br>
            mainListView = (ListView)abc.findViewById(R.id.listviewlayout);<br>
            mainListView.setAdapter(new myAdapter((Activity)BaseActivity.getContext(), Mydate) ); 



我的适配器是这样的:

class MyAdapter extends BaseAdapter {
            private Context context;
            List<students> myObj = null;

            public MyAdapter(Context context, List<students> Objects) {
                this.context = context;

                StudentOnject = Objects;
            }

            public Context getContext() {
                return context;
            }

            public void setContext(Context context) {
                this.context = context;
            }

            @Override
            public int getCount() {
                if(StudentOnject == null || StudentOnject.size() == 0){
                    return 0 ;
                }
                return StudentOnject.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

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

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
        try
        {
                if (StudentOnject != null) {
                    ViewHolder holder;
                    User item = StudentOnject.get(position);

                    if (item != null) {
                        if (convertView == null) {
                            convertView = (RelativeLayout) View.inflate(context,
                                    context.getResources().getIdentifier(
                                            "inviteStudents", "layout",
                                            context.getPackageName()), null);
                            holder = new ViewHolder();
holder.itemRow=convertView.findviewbuid("row");

                            convertView.setTag(holder);
                        }else {
                            holder = (ViewHolder) convertView.getTag();
                        }
holder.itemRow.setbackground("bla bla bla"); <br>
                        <br>return convertView;<br>
                    <br>}
                <br>}
                return null;<br>
        }catch (Exception e) {

            return convertView; 
        }

            }

            class ViewHolder{
                RelativeLayout itemRow;

            }


        }

}

1 个答案:

答案 0 :(得分:1)

需要更改列表对象名称

 List<students> StudentOnject = null;

看到这可能会帮助您根据您的要求进行更改

public class MyAdapter extends BaseAdapter {

    private Context mcontContext;   
    List<students>  mcpulist;

    String mcheck;

    public RowCpuUsagelist(Context context, List<CpuUsagelistData> cpulist) {

        this.mcontContext = context;
        this.mcpulist = cpulist;
        this.mcheck = check;    
    }

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

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

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

    @Override
    public View getView(int position, View converView, ViewGroup parent) {              

        View  gridview;     
        LayoutInflater  inflater  =  (LayoutInflater) mcontContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        if(converView == null) {        

            gridview = new View(mcontContext);              
             // here your row xml 
        gridview  = inflater.inflate(R.layout.rowcpulist, null);


            TextView  txtappname = (TextView) gridview.findViewById(R.id.tvappname);
            TextView  txtcputotal = (TextView) gridview.findViewById(R.id.tvcputotalusing);
            TextView  txtcpuusage = (TextView) gridview.findViewById(R.id.tvcputotalassign);

                      // set here your data using getter method of class
            students   studentsobj = mcpulist.get(position);
            txtappname.setText(studentsobj.getStrAppname());
            txtcputotal.setText(studentsobj.getStrCputotla());
            txtcpuusage.setText(studentsobj.getStrCpuUsage());                  

        } else {            
            gridview = converView;
        }       

        return gridview;
    }

}