user2935620
user2935620

Reputation: 69

Creating Adapter for ListViews but why to data insert wrong position

i am create custom listview with view holder and try to set correct data in to list view. i have alredy checked all value in my adapter its retune getcount 7 but when time i have load data first two position is correct and other row its again to print 0 and 1 position.and some time show other row data into this position i dont know whats wrong with me. pls help :(

 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());

Upvotes: 0

Views: 60

Answers (1)

Milad Faridnia
Milad Faridnia

Reputation: 9477

its because you've set text for your textviews when convert view is null. here:

if (convertView == null) {...

which means new rows, will never set for new values. you must find views in the if statement and set them outside of if statement. like this:

@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());
}

Upvotes: 1

Related Questions