gpt4 book ai didi

java - 使用 customadapter 在 ListView 中重复的项目

转载 作者:行者123 更新时间:2023-12-01 21:22:30 24 4
gpt4 key购买 nike

我有一个带有 ListView 的 ToDo 项目,它使用自定义适配器显示来自 SQLite 的数据。另外,我有一个按钮来添加新任务,但是 ListView 中的项目将重复。

我的自定义适配器是

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;

}
}

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

}

我的 UpdateUI 将数据从 Activity 中的 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

最佳答案

当您迭代光标时,您需要为每一行创建一个新的Model,因此每次都会添加一个不同的对象。

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

关于java - 使用 customadapter 在 ListView 中重复的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38920406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com