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();
|
}
|
}
|
}
|