【问题标题】:Preventing duplicates from being added to a Custom Adapter and database防止将重复项添加到自定义适配器和数据库
【发布时间】:2015-01-25 15:41:28
【问题描述】:

我一直在寻找解决方案,但只是遇到了 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;
    }
});

【问题讨论】:

  • 你不想在你的 logsearch 类中重复值
  • @PramodYadav 是的,这正是我想要做的

标签: android listview adapter hashset


【解决方案1】:

您可以做的是创建一个哈希图并跟踪没有重复值

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

因为您将永远将值存储在哈希集中,就好像哈希集不包含该值一样,那么 set.add() 方法将返回 true,并且该值将被添加到哈希集中,并且如果该值已经存在于哈希集中然后它会返回false,希望它能澄清事情

【讨论】:

  • 如何将 Set 连接到适配器?仅当您在单个生命周期中搜索相同的事物时,它才有效。我试图防止每次用户搜索时出现重复。
  • 我已经在你的 onEditorActionListener 上实现了这个,所以如果值是重复的,那么不会创建 LogSearch 对象并将其添加到列表中,如果不是,则不会调用 notifydatasetchanged()一个重复的值,那么一切都将按照您的编码进行,您是否实现了代码
  • 我让它与它一起工作; for (int i = 0; i
  • 我调用了循环语句来添加所有名称,然后实现了您所说的操作并且它起作用了。如果您想用我之前的评论更新您的答案,我相信如果他们有同样的问题,它可以帮助人们。
猜你喜欢
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-09
  • 2017-07-16
  • 2017-10-15
相关资源
最近更新 更多