在Android中通过ArrayAdapter在ListView中单击单个ImageView背景后如何更改它?

Posted

技术标签:

【中文标题】在Android中通过ArrayAdapter在ListView中单击单个ImageView背景后如何更改它?【英文标题】:How can I change single ImageView background after clicking on it in ListView via ArrayAdapter in Android? 【发布时间】:2021-06-30 02:45:06 【问题描述】:

我的 Fragment 活动中有一个 ListView,它获取存储在内部存储中的文件的名称。 ListView 也有一个 ImageView,当点击它时,设置一个显示通知的警报。我希望在单击时更改特定 ImageView 的背景。我的问题是关闭应用程序后,ImageView 的背景变回正常。那么,我应该如何更改每个 ImageView 被点击的背景呢?

这是我的 Fragment 类代码:

ListView listView;
CustomArrayAdapter arrayAdapter;
ArrayList<String> FilesInFolder;

FilesInFolder = GetFiles(getActivity().getFilesDir());

arrayAdapter = new CustomArrayAdapter(FilesInFolder, getContext());

Collections.sort(FilesInFolder);

listView.setAdapter(arrayAdapter);

这是我的 CustomArrayAdapter 类:

public class CustomArrayAdapter extends ArrayAdapter<String> 

    private ArrayList<String> list;
    Context mContext;

    TimePickerDialog timePickerDialog;
    DatePickerDialog datePickerDialog;
    int currentHour, currentMinute, currentYear, currentMonth, currentDay, currentDate;
    int setHour, setMinute, setYear, setMonth, setDay;
    Calendar calendar;

    AlarmManager alarmManager;
    PendingIntent pendingIntent;
    public static final String NOTIFICATION_CHANNEL_ID = "10001";

    public static class ViewHolder 
        TextView titleTextView;
        ImageView imageView;
    

    public CustomArrayAdapter(ArrayList<String> list, Context context) 
        super(context, R.layout.custom_listview, list);
        this.list = list;
        this.mContext = context;

        calendar = Calendar.getInstance();
        currentHour = calendar.get(Calendar.HOUR_OF_DAY);
        currentMinute = calendar.get(Calendar.MINUTE);

        currentYear = calendar.get(Calendar.YEAR);
        currentMonth = calendar.get(Calendar.MONTH);
        currentDay = calendar.get(Calendar.DAY_OF_MONTH);
        currentDate = calendar.get(Calendar.DATE);
    

    public View getView(int position, View convertView, ViewGroup parent) 
        String string = getItem(position);
        ViewHolder viewHolder;

        if (convertView == null) 
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.custom_listview, parent, false);
            viewHolder.imageView = convertView.findViewById(R.id.bellIcon);
            viewHolder.titleTextView = convertView.findViewById(R.id.textView);
            convertView.setTag(viewHolder);
            viewHolder = (ViewHolder) convertView.getTag();
        
        else 
            viewHolder = (ViewHolder) convertView.getTag();
        

        ViewHolder finalViewHolder = viewHolder;
        viewHolder.imageView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                if (finalViewHolder.imageView.getTag() == "done") 
                    Snackbar.make(v, setHour + " " + setMinute + ", on" + " " + setDay + " " + setMonth + " " + setYear, Snackbar.LENGTH_LONG).show();
                 else 
                    timePickerDialog = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() 
                        @Override
                        public void onTimeSet(TimePicker timePicker, final int hourOfDay, int minute) 
                            setHour = hourOfDay;
                            setMinute = minute;

                            datePickerDialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() 
                                @Override
                                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) 
                                    setYear = year;
                                    setMonth = month;
                                    setDay = dayOfMonth;

                                    Calendar setTime = Calendar.getInstance();
                                    setTime.set(setYear, setMonth, setDay, setHour, setMinute, 0);

                                    Date date = setTime.getTime();

                                    scheduleNotification(string, date.getTime());

                                    finalViewHolder.imageView.setImageResource(R.drawable.ic_baseline_notifications_active_24);
                                    finalViewHolder.imageView.setTag("done");

                                    Snackbar.make(v, "Reminder set for: " + string + "!", Snackbar.LENGTH_LONG)
                                            .setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_SLIDE)
                                            .show();
                                
                            , currentYear, currentMonth, currentDay);

                            datePickerDialog.show();
                        
                    , currentHour, currentMinute, false);

                    timePickerDialog.show();
                
            
        );

        viewHolder.titleTextView.setText(string);
        return convertView;
    

    public void scheduleNotification (String fileName, long delay) 
        Intent notificationIntent = new Intent(this.mContext, MyBroadCastReceiver.class);
        notificationIntent.putExtra(MyBroadCastReceiver.NOTIFICATION_ID, (int)System.currentTimeMillis());
        notificationIntent.putExtra("title", fileName);

        pendingIntent = PendingIntent.getBroadcast(getContext(), (int)System.currentTimeMillis(), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
            alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, delay, pendingIntent);
         else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) 
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, delay, pendingIntent);
         else 
            alarmManager.set(AlarmManager.RTC_WAKEUP, delay, pendingIntent);
        
    

【问题讨论】:

里面有什么? Java? Kotlin? 【参考方案1】:

这是预期的行为,因为您没有保存数据的状态。我建议创建一个反映真实状态的模型。可能是这样的:

class File 
    String filepath;
    boolean reminderSet;

现在您已经有了一个模型类,您可以使用Room 以持久的方式保存数据。执行点击时,只需去更新 DB 中的相应对象(将提醒集设置为 true)。这样,当你在一个应用启动后加载数据时,它仍然会保持之前的状态。

【讨论】:

以上是关于在Android中通过ArrayAdapter在ListView中单击单个ImageView背景后如何更改它?的主要内容,如果未能解决你的问题,请参考以下文章

ArrayAdapter 从未使用过 ArrayList null 并且所有项目都出现在单行中

在Android中通过经度和纬度获取高度

在Android中通过经度和纬度获取高度

在android中通过蓝牙进行多连接

在 Android 中通过蓝牙接收音频

如何在 Turbo C/C++ 中通过引用传递字符串