为ListView创建适配器,但为什么数据插入错误的位置

时间:2015-11-30 13:28:32

标签: android android-layout listview arraylist android-viewholder

我正在使用视图持有者创建自定义列表视图,并尝试将正确的数据设置到列表视图中。我已经alredy检查了我的适配器中的所有值它的retune getcount 7但是当我有加载数据时,前两个位置是正确的,而另一行再次打印0和1位置。有些时候显示其他行数据到这个位置我不知道怎么了我的错。请帮忙:(

 public class AdapterAppointment extends BaseAdapter {


private ArrayList<AppointmentInfoDto> AppointmentItemjbjects = new ArrayList<AppointmentInfoDto>();

private LayoutInflater layoutInflater;
Activity context;
ProgressDialog pg;
String techName="abhi";
LayoutInflater inflater;

public AdapterAppointment(Activity context, ArrayList<AppointmentInfoDto> AppointmentItemjbjects) {
    this.context = context;
    this.layoutInflater = LayoutInflater.from(context);
    this.AppointmentItemjbjects=AppointmentItemjbjects;

}


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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      //  convertView = layoutInflater.inflate(R.layout.activity_appointment, null);
        inflater=LayoutInflater.from(context);
        convertView=inflater.inflate(R.layout.activity_appointment,null);

        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.txtServiceName = (TextView) convertView.findViewById(R.id.txtServiceName);
        viewHolder.txtServiceAndSalesType = (TextView) convertView.findViewById(R.id.txtServiceAndSalesType);
        viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);
        viewHolder.txtCompanyName = (TextView) convertView.findViewById(R.id.txtCompanyName);
        viewHolder.txtInvoiceAmount = (TextView) convertView.findViewById(R.id.txtInvoiceAmount);
        viewHolder.txtServicesAddress = (TextView) convertView.findViewById(R.id.txtServicesAddress);
        viewHolder.txtServiceInstruction = (TextView) convertView.findViewById(R.id.txtServiceInstruction);
        viewHolder.txtServiceDateAndTime = (TextView) convertView.findViewById(R.id.txtServiceDateAndTime);
        viewHolder.txtStatus = (TextView) convertView.findViewById(R.id.txtStatus);
        viewHolder.smsBtn = (AppCompatButton) convertView.findViewById(R.id.smsBtn);
        viewHolder.emailBtn = (AppCompatButton) convertView.findViewById(R.id.emailBtn);
        viewHolder.clickViewLayout = (RelativeLayout) convertView.findViewById(R.id.clickViewLayout);
        viewHolder.txtAccountNo = (TextView) convertView.findViewById(R.id.txtAccountNo);
        viewHolder.txtWorkOrderNo = (TextView) convertView.findViewById(R.id.txtWorkOrderNo);

        viewHolder.txtServiceName.setText(AppointmentItemjbjects.get(position).getServices());
       // viewHolder.txtServiceAndSalesType.setText(AppointmentItemjbjects.get(position).getServices());
        viewHolder.txtName.setText(AppointmentItemjbjects.get(position).getFirstName()+""+AppointmentItemjbjects.get(position).getLastName());
        viewHolder.txtCompanyName.setText(AppointmentItemjbjects.get(position).getCompanyName());
        viewHolder.txtInvoiceAmount.setText("Invoice: $" +AppointmentItemjbjects.get(position).getInvoiceAmount());
        viewHolder.txtServicesAddress.setText(AppointmentItemjbjects.get(position).getServicesAddress1()+", "+AppointmentItemjbjects.get(position).getServiceAddress2()+", "+AppointmentItemjbjects.get(position).getServiceCity()+", "+AppointmentItemjbjects.get(position).getServiceState()+" and "+AppointmentItemjbjects.get(position).getServiceZipcode());
        viewHolder.txtServiceInstruction.setText(AppointmentItemjbjects.get(position).getServiceInstruction());
        viewHolder.txtServiceDateAndTime.setText(DpsFunctionFlow.getDateFormatConvert(AppointmentItemjbjects.get(position).getServiceDateTime().toString()));
        viewHolder.txtStatus.setText(AppointmentItemjbjects.get(position).getWorkorderStatus());
        viewHolder.txtAccountNo.setText("Ac#: " + AppointmentItemjbjects.get(position).getAccountNo());
        viewHolder.txtWorkOrderNo.setText("Wo#: "+AppointmentItemjbjects.get(position).getWorkOrderNo());

1 个答案:

答案 0 :(得分:1)

因为您在转换视图为空时为文本视图设置了文本。这里:

if (convertView == null) {...

表示新行,永远不会为新值设置。你必须在if语句中找到视图并将它们设置在if语句之外。像这样:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      //  convertView = layoutInflater.inflate(R.layout.activity_appointment, null);
        inflater=LayoutInflater.from(context);
        convertView=inflater.inflate(R.layout.activity_appointment,null);
    final ViewHolder viewHolder = new ViewHolder();
    viewHolder.txtServiceName = (TextView) convertView.findViewById(R.id.txtServiceName);
    viewHolder.txtServiceAndSalesType = (TextView) convertView.findViewById(R.id.txtServiceAndSalesType);
    viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);
    viewHolder.txtCompanyName = (TextView) convertView.findViewById(R.id.txtCompanyName);
    viewHolder.txtInvoiceAmount = (TextView) convertView.findViewById(R.id.txtInvoiceAmount);
    viewHolder.txtServicesAddress = (TextView) convertView.findViewById(R.id.txtServicesAddress);
    viewHolder.txtServiceInstruction = (TextView) convertView.findViewById(R.id.txtServiceInstruction);
    viewHolder.txtServiceDateAndTime = (TextView) convertView.findViewById(R.id.txtServiceDateAndTime);
    viewHolder.txtStatus = (TextView) convertView.findViewById(R.id.txtStatus);
    viewHolder.smsBtn = (AppCompatButton) convertView.findViewById(R.id.smsBtn);
    viewHolder.emailBtn = (AppCompatButton) convertView.findViewById(R.id.emailBtn);
    viewHolder.clickViewLayout = (RelativeLayout) convertView.findViewById(R.id.clickViewLayout);
    viewHolder.txtAccountNo = (TextView) convertView.findViewById(R.id.txtAccountNo);
    viewHolder.txtWorkOrderNo = (TextView) convertView.findViewById(R.id.txtWorkOrderNo);
    }
    viewHolder.txtServiceName.setText(AppointmentItemjbjects.get(position).getServices());
   // viewHolder.txtServiceAndSalesType.setText(AppointmentItemjbjects.get(position).getServices());
    viewHolder.txtName.setText(AppointmentItemjbjects.get(position).getFirstName()+""+AppointmentItemjbjects.get(position).getLastName());
    viewHolder.txtCompanyName.setText(AppointmentItemjbjects.get(position).getCompanyName());
    viewHolder.txtInvoiceAmount.setText("Invoice: $" +AppointmentItemjbjects.get(position).getInvoiceAmount());
    viewHolder.txtServicesAddress.setText(AppointmentItemjbjects.get(position).getServicesAddress1()+", "+AppointmentItemjbjects.get(position).getServiceAddress2()+", "+AppointmentItemjbjects.get(position).getServiceCity()+", "+AppointmentItemjbjects.get(position).getServiceState()+" and "+AppointmentItemjbjects.get(position).getServiceZipcode());
    viewHolder.txtServiceInstruction.setText(AppointmentItemjbjects.get(position).getServiceInstruction());
    viewHolder.txtServiceDateAndTime.setText(DpsFunctionFlow.getDateFormatConvert(AppointmentItemjbjects.get(position).getServiceDateTime().toString()));
    viewHolder.txtStatus.setText(AppointmentItemjbjects.get(position).getWorkorderStatus());
    viewHolder.txtAccountNo.setText("Ac#: " + AppointmentItemjbjects.get(position).getAccountNo());
    viewHolder.txtWorkOrderNo.setText("Wo#: "+AppointmentItemjbjects.get(position).getWorkOrderNo());
}