Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

eclipse - Adapter in android in list view

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;

            }


        }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...