0

Please, can anybody help me? I am very new in java coding. I am using Android Studio to make some apps working by copy and paste with lots of trials and errors. Now I am writing code that will retrieve data from MySQL and put them on Listview, but I am getting duplicate data when I scroll down. I've been reading many suggestions that can solve this problem for almost a week and tried them on my code but nothing work. So it will be much appreciated if someone can help me. This is my code.

PersonAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;

public class PersonAdapter extends ArrayAdapter<Person> {
public PersonAdapter(Context context, int resource,List<Person> objects) {
    super(context, R.layout.item_row, objects);
}

@Override
public View getView(int position,View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    View view;
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_row, null);
        viewHolder = new ViewHolder(convertView);
        convertView.setTag(viewHolder);
        Person p = getItem(position);
        TextView textView = (TextView) convertView.findViewById(R.id.textView2);
        textView.setText(p.getId());
        TextView textView2 = (TextView) convertView.findViewById(R.id.textView);
        textView2.setText(p.getName());
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    return convertView;
}

static class ViewHolder {
    public TextView textView;
    public TextView textView2;
    public TextView textView3;
    public Button addbutt;
    public Button delbutt;

    public ViewHolder(View convertView) {
        textView = (TextView) convertView.findViewById(R.id.textView);
        textView2 = (TextView) convertView.findViewById(R.id.textView2);
        textView3 = (TextView) convertView.findViewById(R.id.textView3);
        addbutt = (Button) convertView.findViewById(R.id.addbutt);
        delbutt = (Button) convertView.findViewById(R.id.delbutt);
    }
}

Person.java

public class Person {
private String name;
private String id;

public Person(String name, String id) {
    this.name = name;
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}
Gautam Surani
  • 1,136
  • 10
  • 21
Krairerks
  • 11
  • 1
  • Your view holder pattern is wrong. You don't need to use `findViewById()` inside `getView()` if you use view holder pattern. Check this StackOverflow [answer](https://stackoverflow.com/a/24865351/5180017) to get correct idea about view holder pattern. – Shashanth Aug 08 '18 at 05:05

1 Answers1

0

Override these functions

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public int getViewTypeCount() {

        return getCount();
    }

    @Override
    public int getItemViewType(int position) {

        return position;
    }

You adapter should be like

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.TextView;

    import java.util.List;

    public class PersonAdapter extends ArrayAdapter<Person> {
    public PersonAdapter(Context context, int resource,List<Person> objects) {
        super(context, R.layout.item_row, objects);
    }

       @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public int getViewTypeCount() {

            return getCount();
        }

        @Override
        public int getItemViewType(int position) {

            return position;
        }

    @Override
    public View getView(int position,View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        View view;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_row, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
            Person p = getItem(position);
            TextView textView = (TextView) convertView.findViewById(R.id.textView2);
            textView.setText(p.getId());
            TextView textView2 = (TextView) convertView.findViewById(R.id.textView);
            textView2.setText(p.getName());
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        return convertView;
    }

    static class ViewHolder {
        public TextView textView;
        public TextView textView2;
        public TextView textView3;
        public Button addbutt;
        public Button delbutt;

        public ViewHolder(View convertView) {
            textView = (TextView) convertView.findViewById(R.id.textView);
            textView2 = (TextView) convertView.findViewById(R.id.textView2);
            textView3 = (TextView) convertView.findViewById(R.id.textView3);
            addbutt = (Button) convertView.findViewById(R.id.addbutt);
            delbutt = (Button) convertView.findViewById(R.id.delbutt);
        }
    }
Sandeep Parish
  • 1,888
  • 2
  • 9
  • 20
  • Sir, I put these in my code but I don't know how to use it and it didn't pass (travelAgents is in red) and it has some problem with my code (Person p = getItem(position);) – Krairerks Aug 08 '18 at 06:23
  • It's working. Thank you very much Sir. – Krairerks Aug 08 '18 at 07:05