使用customadapter在Listview中重复的项目

时间:2016-08-12 14:28:00

标签: java android sqlite listview

我有一个带有listview的ToDo项目,该项目显示来自带有自定义适配器的SQLite的数据。此外,我有一个按钮来添加新任务,但列表视图中的项目将重复。

我的自定义适配器是

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;

     }
 }

我的带有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;
}

}

我的UpdateUI,它在活动中从SQLite返回数据到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();
}

这是我的问题。 当我添加第二个任务时,第一个任务的标题将随之改变,并且对于其他行重复此操作。

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:3)

当你遍历光标时,你需要为每一行创建一个新的Model,所以你每次都要添加一个不同的对象。

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