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;
}