Android列表视图空白行。但是如果你滚动并返回到行。它的工作正常

时间:2016-02-04 08:40:24

标签: android listview

Android列表视图空白行。但是如果你滚动并返回到行。它的工作正常。 请帮忙screenshot

我的代码:

public View getView(final int position, View convertView, ViewGroup parent) {

        View row = convertView;
       final ViewHolder contactHolder;




        if(row==null){
            LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.row_layout,parent,false);
            contactHolder =  new ViewHolder();

            contactHolder.txt_fname = (TextView)row.findViewById(R.id.txt_fname);
            contactHolder.txt_email =(TextView)row.findViewById(R.id.txt_email);
            contactHolder.txt_status =(TextView)row.findViewById(R.id.txt_status);


            contactHolder.webView = (WebView)row.findViewById(R.id.webView);

            row.setTag(contactHolder);

        }
        else
        {

            contactHolder= (ViewHolder)row.getTag();
            contactHolder.position = position;
            final Contacts contacts= (Contacts)this.getItem(contactHolder.position);

            contactHolder.txt_fname.setText(contacts.getLname() + " " + contacts.getFname());
            contactHolder.txt_email.setText(contacts.getEmail());
            switch (contacts.getUserStatus()) {
                case "0":
                    contactHolder.txt_status.setTextColor(Color.RED);
                    contactHolder.txt_status.setText("Offline");

                    break;


                case "1":
                    contactHolder.txt_status.setTextColor(Color.YELLOW);
                    contactHolder.txt_status.setText("Away");

                    break;

                case "2":
                    contactHolder.txt_status.setTextColor(Color.GREEN);
                    contactHolder.txt_status.setText("Online");

                    break;


            }
//        contactHolder.txt_status.setText(contacts.getUserStatus());
            contactHolder.webView.loadUrl("http://192.168.1.109/nest/android/ViewConsultantPic/" + contacts.getFilename());


            contactHolder.txt_email.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {

                    Intent i = new Intent(getContext(),Consultant_Profile.class);
                    i.putExtra("consultant_id",contacts.getId());
                    i.putExtra("consultant_name",contacts.getLname()+" "+contacts.getFname());
                    i.putExtra("filename",contacts.getFilename());
                    i.putExtra("skype",contacts.getSkype());
                    i.putExtra("facebook",contacts.getFacebook());
                    i.putExtra("twitter",contacts.getTwitter());
                    i.putExtra("linkedin", contacts.getLinkedin());
                    i.putExtra("googleplus",contacts.getGooglePlus());
                    i.putExtra("loggedInUser", contacts.getLoggedInUser());
                    i.putExtra("user_status",contacts.getUserStatus());
                    getContext().startActivity(i);
                    Toast.makeText(getContext(), "Clicked row" + contactHolder.position , Toast.LENGTH_LONG).show();

                }

            });

        }


        return row;


    }
    public class ViewHolder{
         TextView txt_fname,txt_email,txt_status;

       WebView webView;
        RelativeLayout body;
        int position;

    }

1 个答案:

答案 0 :(得分:1)

请更改您的代码 - getView else案例存在问题。

public View getView(final int position, View convertView, ViewGroup parent) { 

        View row = convertView; 
       final ViewHolder contactHolder; 




        if(row==null){ 
            LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
            row = layoutInflater.inflate(R.layout.row_layout,parent,false); 
            contactHolder =  new ViewHolder(); 

            contactHolder.txt_fname = (TextView)row.findViewById(R.id.txt_fname); 
            contactHolder.txt_email =(TextView)row.findViewById(R.id.txt_email); 
            contactHolder.txt_status =(TextView)row.findViewById(R.id.txt_status); 


            contactHolder.webView = (WebView)row.findViewById(R.id.webView); 

            row.setTag(contactHolder); 

        } 
        else 
        { 
             contactHolder= (ViewHolder)row.getTag(); /// CHANGED HERE

            } 

 contactHolder.position = position; 
            final Contacts contacts= (Contacts)this.getItem(position); 

            contactHolder.txt_fname.setText(contacts.getLname() + " " + contacts.getFname()); 
            contactHolder.txt_email.setText(contacts.getEmail()); 
            switch (contacts.getUserStatus()) { 
                case "0": 
                    contactHolder.txt_status.setTextColor(Color.RED); 
                    contactHolder.txt_status.setText("Offline"); 

                    break; 


                case "1": 
                    contactHolder.txt_status.setTextColor(Color.YELLOW); 
                    contactHolder.txt_status.setText("Away"); 

                    break; 

                case "2": 
                    contactHolder.txt_status.setTextColor(Color.GREEN); 
                    contactHolder.txt_status.setText("Online"); 

                    break; 

//        contactHolder.txt_status.setText(contacts.getUserStatus()); 
            contactHolder.webView.loadUrl("http://192.168.1.109/nest/android/ViewConsultantPic/" + contacts.getFilename()); 


            contactHolder.txt_email.setOnClickListener(new View.OnClickListener() { 
                @Override 
                public void onClick(final View v) { 

                    Intent i = new Intent(getContext(),Consultant_Profile.class); 
                    i.putExtra("consultant_id",contacts.getId()); 
                    i.putExtra("consultant_name",contacts.getLname()+" "+contacts.getFname()); 
                    i.putExtra("filename",contacts.getFilename()); 
                    i.putExtra("skype",contacts.getSkype()); 
                    i.putExtra("facebook",contacts.getFacebook()); 
                    i.putExtra("twitter",contacts.getTwitter()); 
                    i.putExtra("linkedin", contacts.getLinkedin()); 
                    i.putExtra("googleplus",contacts.getGooglePlus()); 
                    i.putExtra("loggedInUser", contacts.getLoggedInUser()); 
                    i.putExtra("user_status",contacts.getUserStatus()); 
                    getContext().startActivity(i); 
                    Toast.makeText(getContext(), "Clicked row" + contactHolder.position , Toast.LENGTH_LONG).show(); 

                } 

            }); 

        } 


        return row; 


    }