Adapter in android in list view

依然范特西╮ 提交于 2019-12-12 05:27:39

问题


I am using an adapter in ListView. The problem is no row is displayed and no exception raised, but there are 10 records which I am sending to adapter. When I debug it I don't know why getView() is not executing
I have set adapter like this

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



and my adapter is like this:

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:


need to change your List object name

 List<students> StudentOnject = null;

see this may Helps you change as per your requirment

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

}


来源:https://stackoverflow.com/questions/12387474/adapter-in-android-in-list-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!