图像不会显示在android列表视图中

ygya80vv  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(364)

我正在制作一个待办事项应用程序,当一个项目被标记为完成时,一个图像应该显示在列表项目的前面。但是,即使数据库中的项目状态更改为“完成”,图像在列表视图中也不可见。
这是我的适配器类。
公共类ToLoadAdapter扩展了arrayadapter{

private Context context;
private int resource;
List<ToDoModel> todos;

public ToDoAdapter(@NonNull Context context, int resource, @NonNull List<ToDoModel> objects) {
    super(context, resource, objects);

    this.context = context;
    this.resource = resource;
    this.todos = objects;
}

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

    //Inflate card view
    LayoutInflater inflater = LayoutInflater.from(context);
    View row = inflater.inflate(resource, parent, false);

    TextView title = row.findViewById(R.id.title);
    TextView description = row.findViewById(R.id.txtDescription);
    ImageView img = (ImageView)row.findViewById(R.id.img_done);
    img.setVisibility(View.VISIBLE);

    //Set data in card view
    ToDoModel todo = todos.get(position);
    title.setText(todo.getTitle());
    description.setText(todo.getDescription());

    //Show done image
    if(todo.getFinished() > 0){
        img.setVisibility(View.VISIBLE);
    }

    return row;
}

}
这是主要的活动。
公共类mainactivity扩展了appcompatactivity{

private ListView todoList;
private TextView countTxt;
private Button addTodo;
private List<ToDoModel> toDos;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    todoList = (ListView)findViewById(R.id.listViewToDo);
    countTxt = (TextView)findViewById(R.id.txtCount);
    addTodo = (Button)findViewById(R.id.btnAdd);
    toDos = new ArrayList<>();

    addTodo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Go to next activity
            startActivity(new Intent(getApplicationContext(), AddToDO.class));
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    DBHandler dbh = new DBHandler(getApplicationContext());

    //Set count
    int count = dbh.getCountTODO();
    countTxt.setText("You have " + count + " TODOs");

    //Save list
    toDos = dbh.getAllToDos();

    //Set adapter to list
    ToDoAdapter toDoAdapter = new ToDoAdapter(getApplicationContext(), R.layout.todo_cardview, toDos);
    todoList.setAdapter(toDoAdapter);

    //Add click listener to list element
    todoList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            //Get clicked item
            ToDoModel todo = toDos.get(position);

            //Dialog box
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle(todo.getTitle());
            builder.setMessage(todo.getDescription());

            //Buttons
            builder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dbh.deleteItem(todo.getId());
                    Toast.makeText(getApplicationContext(), "ToDo Deleted", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getApplicationContext(), MainActivity.class));
                }
            });

            builder.setPositiveButton("Finished", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    long time = System.currentTimeMillis();
                    //Update status
                    dbh.updateFinished(todo.getId(), time);

                    startActivity(new Intent(getApplicationContext(), MainActivity.class));
                }
            });

            builder.setNeutralButton("Edit", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                  Intent intent = new Intent(MainActivity.this, AddToDO.class);
                  intent.putExtra("id", todo.getId());
                  intent.putExtra("title", todo.getTitle());
                  intent.putExtra("description", todo.getDescription());
                  intent.putExtra("finished", todo.getFinished());
                  startActivity(intent);

                }
            });

            builder.show();
        }
    });
}

}

yzckvree

yzckvree1#

使用glide或毕加索库以任何其他形状显示图像。
滑翔示例:
glide.with(mcontext).load(responselist.getlogourl()).apply(requestoptions.circlecroptransform()).into(holder.imagename);
毕加索的例子:
picasso.get().load(clsobjname.getlogourl()).into(viewholder.imagename);

相关问题