屏幕外的Android ListView项目未正确显示

时间:2015-03-30 19:36:37

标签: android listview android-listview listviewitem

正如标题所说,我遇到了使用适配器设置列表视图的问题,因此屏幕外部的底部项目与顶部项目重复。例如,在列表视图的顶部,约会14是第一个项目,但是当我向下滚动到底部时,底部项目应该是其他项目约会20,但它显示为约会14。

这是我的代码:

public class AppointmentArrayAdapter extends BaseAdapter {

    ArrayList<Appointment> appointments;
    Context context;
    LayoutInflater inflater;

    public AppointmentArrayAdapter(ArrayList<Appointment> app, Context c){
        appointments=app;
        context=c;
        inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return appointments.size();
    }

    @Override
    public Object getItem(int position) {
        return appointments.get(position);
    }

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

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

        ViewHolder holder;

        if(convertView==null){
            convertView = inflater.inflate(R.layout.appointment_list_item, parent, false);
            holder = new ViewHolder();
            TableLayout ll = (TableLayout) convertView.findViewById(R.id.tableLayout);
            TableRow[] row = new TableRow[6];
            TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT);

            for(int i=0;i<6;i++){
                row[i]=new TableRow(context);
                row[i].setLayoutParams(lp);
            }

            TextView itemId = new TextView(context);
            TextView itemType = new TextView(context);

            itemId.setText(""+appointments.get(position).getId());
            itemType.setText(appointments.get(position).getAppointmentType().toString());

            row[0].setBackgroundColor(0xFF00FF00);
            row[0].addView(itemId);
            row[0].addView(itemType);

            TextView lable1 = new TextView(context);
            TextView patient = new TextView(context);
            lable1.setText("Patient: ");
            String patientName = DatabaseHandler.getInstance(null).getUserById(appointments.get(position).getPatient()).getName();
            patient.setText(patientName);
            row[1].addView(lable1);
            row[1].addView(patient);

            TextView lable2 = new TextView(context);
            lable2.setText("Consultant: ");
            TextView consultant = new TextView(context);
            String consultantName = DatabaseHandler.getInstance(null).getUserById(appointments.get(position).getConsultant()).getName();
            consultant.setText(consultantName);
            row[2].addView(lable2);
            row[2].addView(consultant);

            TextView lable3 = new TextView(context);
            lable3.setText("Clinic: ");
            TextView clinic = new TextView(context);
            String clinicName = DatabaseHandler.getInstance(null).getUserById(appointments.get(position).getClinic()).getName();
            clinic.setText(clinicName);
            row[3].addView(lable3);
            row[3].addView(clinic);

            TextView lable4 = new TextView(context);
            lable4.setText("Timeslot: ");
            TextView timelot = new TextView(context);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String formattedDate = df.format(appointments.get(position).getTime());
            timelot.setText(formattedDate);
            row[4].addView(lable4);
            row[4].addView(timelot);

            for(int i=0;i<6;i++)
                ll.addView(row[i]);

            holder.tableLayout = ll;

            convertView.setTag(holder); 

        }else{
            holder = (ViewHolder)convertView.getTag();
        }


        return convertView;
    }
}
class ViewHolder{
    TableLayout tableLayout;
}

2 个答案:

答案 0 :(得分:0)

Android会重复使用您之前创建的视图。这就是你在convertView是!= null时得到的。所以在这些情况下,你需要做setText,setBackground等。我认为问题是if(convertView == null){block应该只是你拥有的前两行。像

这样的东西
if (convertView == null) {
    inflate the view, create new ViewHolder
} else {
    set the ViewHolder variable to the tag
}
set up the text views, background etc. in either case

答案 1 :(得分:0)

是的@Clairvoyant @Ravind Maurya。我设置了分支外的所有数据解决了这个问题,谢谢你的提示。

public class AppointmentArrayAdapter extends BaseAdapter {

    ArrayList<Appointment> appointments;
    Context context;
    LayoutInflater inflater;

    public AppointmentArrayAdapter(ArrayList<Appointment> app, Context c){
        appointments=app;
        context=c;
        inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return appointments.size();
    }

    @Override
    public Object getItem(int position) {
        return appointments.get(position);
    }

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

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

        ViewHolder viewHolder;

        if(convertView==null){
            convertView = inflater.inflate(R.layout.appointment_list_item, parent, false);

            viewHolder = new ViewHolder();

            viewHolder.tl = (TableLayout) convertView.findViewById(R.id.tableLayout);
            viewHolder.row = new TableRow[6];
            viewHolder.lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT);
            viewHolder.itemId = new TextView(context);
            viewHolder.itemType = new TextView(context);
            for(int i=0;i<6;i++){
                viewHolder.row[i]=new TableRow(context);
                viewHolder.row[i].setLayoutParams(viewHolder.lp);
            }

            viewHolder.row[0].addView(viewHolder.itemId);
            viewHolder.row[0].addView(viewHolder.itemType);

            viewHolder.lable1 = new TextView(context);
            viewHolder.patient = new TextView(context);

            viewHolder.row[1].addView(viewHolder.lable1);
            viewHolder.row[1].addView(viewHolder.patient);

            viewHolder.lable2 = new TextView(context);
            viewHolder.consultant = new TextView(context);

            viewHolder.row[2].addView(viewHolder.lable2);
            viewHolder.row[2].addView(viewHolder.consultant);

            viewHolder.lable3 = new TextView(context);
            viewHolder.clinic = new TextView(context);

            viewHolder.row[3].addView(viewHolder.lable3);
            viewHolder.row[3].addView(viewHolder.clinic);

            viewHolder.lable4 = new TextView(context);
            viewHolder.lable4.setText("Timeslot: ");
            viewHolder.timelot = new TextView(context);

            viewHolder.row[4].addView(viewHolder.lable4);
            viewHolder.row[4].addView(viewHolder.timelot);

            for(int i=0;i<6;i++)
                viewHolder.tl.addView(viewHolder.row[i]);

            convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder) convertView.getTag();
        }

        viewHolder.row[0].setBackgroundColor(0xFF00FF00);
        viewHolder.itemId.setText(""+appointments.get(position).getId());
        viewHolder.itemType.setText(appointments.get(position).getAppointmentType().toString());
        viewHolder.lable1.setText("Patient: ");
        String patientName = DatabaseHandler.getInstance(null).getUserById(appointments.get(position).getPatient()).getName();
        viewHolder.patient.setText(patientName);
        viewHolder.lable2.setText("Consultant: ");

        viewHolder.lable3.setText("Clinic: ");

        String consultantName = DatabaseHandler.getInstance(null).getUserById(appointments.get(position).getConsultant()).getName();
        viewHolder.consultant.setText(consultantName);
        String clinicName = DatabaseHandler.getInstance(null).getUserById(appointments.get(position).getClinic()).getName();
        viewHolder.clinic.setText(clinicName);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String formattedDate = df.format(appointments.get(position).getTime());
        viewHolder.timelot.setText(formattedDate);
        return convertView;
    }

}


class ViewHolder{
    public LayoutParams lp;
    public TextView timelot;
    public TextView lable4;
    public TextView clinic;
    public TextView lable3;
    public TextView consultant;
    public TextView lable2;
    public TextView patient;
    public TextView lable1;
    public TableRow[] row;
    public TextView itemId;
    public TextView itemType;
    public TableLayout tl;
}