Fardin2689 Fardin2689 - 3 months ago 9
Java Question

Items repeated in Listview with customadapter

I have a ToDo project with listview that shows the data from SQLite with custom adapter. Also, I have a button to add the new task but, the items in list view will be repeated.

my custom adapter is

public class CustomAdapter extends BaseAdapter {

private final Activity activity;
private final ArrayList<LT_Model> data;
private static LayoutInflater inflater = null;

public CustomAdapter(Activity a, ArrayList<LT_Model> d) {

this.activity = a;
this.data=d;
inflater = ( LayoutInflater )this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder{
public TextView task;
public ImageView imgD;
public ImageView imgE;
public ImageView imgS;
}
@Override
public int getCount() {
if(data.size()<=0)
return 1;
return data.size();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

if(convertView==null){

convertView = inflater.inflate(R.layout.item_todo, null);

holder = new ViewHolder();
holder.task = (TextView) convertView.findViewById(R.id.task_title);
holder.imgD=(ImageView)convertView.findViewById(R.id.imgDelete);
holder.imgE=(ImageView)convertView.findViewById(R.id.imgEdit);
holder.imgS=(ImageView)convertView.findViewById(R.id.imgCheck);


convertView.setTag( holder );
}
else
holder=(ViewHolder)convertView.getTag();


if(data.size()<=0)
{
holder.task.setText("No Data");
}
else
{
holder.task.setText(data.get(position).getTaskName());
holder.imgD.setImageResource(android.R.drawable.ic_delete);
holder.imgE.setImageResource(data.get(position).getImgComment());
holder.imgS.setImageResource(data.get(position).getImgStatus());
}

return convertView;

}


}

My LT_Model with getter and setter

public class LT_Model {
private String TaskName = "";
private int ImgComment;
private int ImgStatus;

public String getTaskName() {
return TaskName;
}

public void setTaskName(String taskName) {
TaskName = taskName;
}

public int getImgComment() {
return ImgComment;
}

public void setImgComment(int imgComment) {
ImgComment = imgComment;
}

public int getImgStatus() {
return ImgStatus;
}

public void setImgStatus(int imgStatus) {
ImgStatus = imgStatus;
}


}

My UpdateUI which return the data to Arraylist from SQLite in the activity.
My table has 3 columns and I passed them to ArrayList.

private void updateUI() {
ArrayList<LT_Model> taskList = new ArrayList<>();
LT_Model lt_model = new LT_Model();
SQLiteDatabase db = mdb.getReadableDatabase();
Cursor cursor = db.query(DB_Value.Constant.List_Table,
new String[]{DB_Value.Constant._ID, DB_Value.Constant.COL_Task, DB_Value.Constant.COL_Comment,
DB_Value.Constant.COL_Status}, null, null, null, null, null);
while (cursor.moveToNext()) {
//int idx = cursor.getColumnIndex(DB_Value.Constant.COL_Task);
lt_model.setTaskName(cursor.getString(1));
Toast.makeText(getApplicationContext(), lt_model.getTaskName(), Toast.LENGTH_SHORT).show();
if (cursor.getString(2) == null || cursor.getString(2).equals("")) {
lt_model.setImgComment(android.R.drawable.ic_menu_edit);
}else{
lt_model.setImgComment(android.R.drawable.ic_menu_agenda);
}
if (cursor.getInt(3)==1){
lt_model.setImgStatus(android.R.drawable.checkbox_on_background);
}else{
lt_model.setImgStatus(android.R.drawable.checkbox_off_background);
}

taskList.add(lt_model);
}

mTaskListView.setAdapter(new CustomAdapter(dailyNew, taskList));

cursor.close();
db.close();
}


and this is my problem.
When I add the second task the title of first task would change with that, and this is repeated for the others rows. image2 image3

Many thanks in advance.

Answer

When you iterate through your cursor, you need to make a new Model for each row, so you're adding a distinct object each time.

    while (cursor.moveToNext()) {
        LT_Model lt_model = new LT_Model();