简体   繁体   English

在列表视图中使用customadapter重复的项目

[英]Items repeated in Listview with customadapter

I have a ToDo project with listview that shows the data from SQLite with custom adapter. 我有一个带有listview的ToDo项目,该项目显示具有自定义适配器的SQLite中的数据。 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 我的带有getter和setter的LT_Model

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. 我的UpdateUI,它将数据从活动中的SQLite返回到Arraylist。 My table has 3 columns and I passed them to ArrayList. 我的表有3列,并将它们传递给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. 当我添加第二个任务时,第一个任务的标题将随之更改,其他行将重复此操作。

在此处输入图片说明

在此处输入图片说明

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. 当您遍历游标时,需要为每一行创建一个新的Model ,因此每次都添加一个不同的对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM