【问题标题】:Creating Adapter for ListViews but why to data insert wrong position为 ListViews 创建适配器,但为什么数据插入错误的位置
【发布时间】:2016-03-04 03:46:54
【问题描述】:

我正在使用视图持有者创建自定义列表视图,并尝试在列表视图中设置正确的数据。我已经检查了我的适配器中的所有值,它的重调 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());

【问题讨论】:

  • 你总是在 if(convertVieww == null) { insert data} else { insert data} 时插入数据

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


【解决方案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());
}

【讨论】:

  • 感谢您在我完全删除 if(convertView == null) {..} 条件 getView 时提示其工作。
  • 欢迎您。顺便说一句,我建议您使用该条件来减少 findView 调用的数量,这是一种耗时的方法。如果您查看我的答案,您会看到所有 findView 调用都在 if(convertView == null) cluase
猜你喜欢
  • 2012-09-11
  • 2015-05-03
  • 2017-04-19
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
  • 2015-10-26
  • 1970-01-01
  • 2017-06-09
相关资源
最近更新 更多