我一直在寻找一个解决方案,只遇到HashSet,我在实现它时遇到了麻烦。大多数解释都是模糊的,这使我很难理解它并用我的例子来实现它。我想使用HashSet或同等有效的解决方案。我没有使用HashSet,所以如果有人可以解释如何使用我的代码实现并解释它是如何工作的或其他解决方案。如果您需要更多代码或不理解我的问题,我会更新我的答案。
适配器
public class LogSearchAdapter extends ArrayAdapter<LogSearch> {
private static LogSearchAdapter instance;
Context mContext;
public static List<LogSearch> mLogs;
public LogSearchAdapter(Context context, int textViewResourceId, List<LogSearch> logs) {
super(context, textViewResourceId);
mContext = context;
mLogs = logs;
}
public void setLogs(List<LogSearch> logs) {
mLogs = logs;
}
public List<LogSearch> getLogs() {
return mLogs;
}
public void add(LogSearch log) {
mLogs.add(log);
}
public void remove(LogSearch log) {
LogSearchAdapter.mLogs.remove(log);
}
public static LogSearchAdapter getInstance(Context mContext) {
if (instance == null) {
instance = new LogSearchAdapter(mContext.getApplicationContext(), 4, mLogs);
}
return instance;
}
public int getCount() {
return mLogs.size();
}
public LogSearch getItem(int position) {
return mLogs.get(position);
}
public View getView(int position, View convertView, ViewGroup parent) {
LogSearchRow view = (LogSearchRow) convertView;
if (view == null) {
view = new LogSearchRow(mContext);
}
LogSearch log = getItem(position);
view.setLog(log);
return view;
}
}
活动
etSearch = (EditText) findViewById(R.id.etSearch);
listSearch = (ListView) findViewById(R.id.listSearch);
logSearchAdapter = new LogSearchAdapter(this, 0, LogSearch.all());
listSearch.setAdapter(logSearchAdapter);
etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
search();
// Saving items to the Database
LogSearch LogSearch = new LogSearch();
LogSearch.setName(etSearch.getText().toString());
LogSearch.save();
// Adding the saved item to the Adapter
logSearchAdapter.add(LogSearch);
logSearchAdapter.notifyDataSetChanged();
// listSearch.setVisibility(View.GONE);
return true;
}
return false;
}
});
工作解决方案感谢Pramod Yadav
etSearch = (EditText) findViewById(R.id.etSearch);
listSearch = (ListView) findViewById(R.id.listSearch);
Set<String> set=new HashSet<String>();
logSearchAdapter = new LogSearchAdapter(this, 0, LogSearch.all());
listSearch.setAdapter(logSearchAdapter);
etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// search();
// Adding the names from the adapter to to the Set
for (int i = 0; i < logSearchAdapter.getCount(); i++) {
LogSearch ls = logSearchAdapter.getItem(i);
set.add(ls.getName());
}
if (set.add(etSearch.getText().toString())) {
// Saving items to the Database
LogSearch LogSearch = new LogSearch();
LogSearch.setName(etSearch.getText().toString());
LogSearch.save();
// Adding the saved item to the Adapter
logSearchAdapter.add(LogSearch);
logSearchAdapter.notifyDataSetChanged();
} else {
//do whatever you want to do for duplicate values
}
// listSearch.setVisibility(View.GONE);
return true;
}
return false;
}
});
答案 0 :(得分:2)
您可以做的是创建一个hashmap并跟踪没有重复值
etSearch = (EditText) findViewById(R.id.etSearch);
listSearch = (ListView) findViewById(R.id.listSearch);
Set<String> set=new HashSet<String>();
logSearchAdapter = new LogSearchAdapter(this, 0, LogSearch.all());
listSearch.setAdapter(logSearchAdapter);
etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
search();
// Saving items to the Database
if(set.add(etSearch.getText().toString()))
{
LogSearch LogSearch = new LogSearch();
LogSearch.setName(etSearch.getText().toString());
LogSearch.save();
// Adding the saved item to the Adapter
logSearchAdapter.add(LogSearch);
logSearchAdapter.notifyDataSetChanged();
}
else
{
//do whatever you want to do for duplicate values
}
// listSearch.setVisibility(View.GONE);
return true;
}
return false;
}
});
因为您将值存储在hashset中,就好像hashset不包含该值,然后set.add()方法将返回true并且该值将被添加到hashset并且如果该值已经存在于hashset中然后它会返回false,希望它会澄清事情