admin
2022-01-13 b9ed091e3b607af409e30aca468958cdb08641ad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.ysvideo.zhibo.app.ui.video.adapter;
 
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
 
import com.ysvideo.zhibo.app.R;
import com.ysvideo.zhibo.lib.common.util.common.StringUtils;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
 
public class GridSearchRecordAdapter extends BaseAdapter {
 
    private List<String> mSuggestions;
    private boolean isHot;
 
    public GridSearchRecordAdapter(List<String> suggestions, boolean isHot) {
        super();
        this.mSuggestions = suggestions;
        this.isHot = isHot;
    }
 
    @Override
    public int getCount() {
        return mSuggestions == null ? 0 : mSuggestions.size();
    }
 
    @Override
    public Object getItem(int position) {
        return mSuggestions.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.item_search_history, parent, false);
            viewHolder.tv_content = convertView
                    .findViewById(R.id.tv_content);
 
            viewHolder.iv_del = convertView
                    .findViewById(R.id.iv_del);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
 
 
        String content = mSuggestions.get(position);
        viewHolder.tv_content.setText(content);
        viewHolder.iv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteRecord(viewHolder.iv_del.getContext(), content);
                mSuggestions.remove(position);
                notifyDataSetChanged();
            }
        });
 
        return convertView;
    }
 
    private static class ViewHolder {
        TextView tv_content;
        ImageView iv_del;
    }
 
    private void deleteRecord(Context context, String key) {
 
        SharedPreferences historySp = context.getSharedPreferences("search_history",
                Context.MODE_PRIVATE);
        String historyStr = historySp.getString("history", "");
        if (!StringUtils.isBlank(historyStr)) {
            String[] historyArray = historyStr.split(",");
            Set<String> set = new HashSet<>();
            set.addAll(Arrays.asList(historyArray));
            set.remove(key);
            String str = "";
            for (String st : set) {
                str += st + ",";
            }
            if (str.endsWith(","))
                str = str.substring(0, str.length() - 1);
            SharedPreferences.Editor editor = historySp.edit();
            editor.putString("history", str);
            editor.commit();
        }
    }
}