Java 我使用LinkedHashset来防止在自定义对象的Arraylist中出现重复条目,但会发生重复
我有一个有5个成员变量的班级。点击一个按钮用户,输入一个新的Place对象,我把它放在一个listview中。我使用LinkedHashset来防止重复,但我仍然可以在我的listview中看到重复条目。请建议。 我的代码:Java 我使用LinkedHashset来防止在自定义对象的Arraylist中出现重复条目,但会发生重复,java,android,arraylist,Java,Android,Arraylist,我有一个有5个成员变量的班级。点击一个按钮用户,输入一个新的Place对象,我把它放在一个listview中。我使用LinkedHashset来防止重复,但我仍然可以在我的listview中看到重复条目。请建议。 我的代码: 要使LinkedHashSet正常工作,必须重写hashcode和equals方法。在您的情况下,您必须在本地类中执行此操作。我有一个使用哈希集的解决方案。让我知道如果这对你有效,我没有布局文件,或适配器等信息来测试它正确 public class OneFragment
要使LinkedHashSet正常工作,必须重写hashcode和equals方法。在您的情况下,您必须在本地类中执行此操作。我有一个使用哈希集的解决方案。让我知道如果这对你有效,我没有布局文件,或适配器等信息来测试它正确
public class OneFragment extends Fragment implements View.OnClickListener {
private Button mButton;
private EditText mInput;
private ListView mListView;
private HashSet<Place> mPlaces;
private PlaceAdapter mPlaceAdapter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create hash set
mPlaces = new HashSet<>();
// Create adapter based on hash set
mPlaceAdapter = new PlaceAdapter(getActivity(), R.layout.row, mPlaces, OneFragment.this);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
// Initialise the view objects
initViews(view);
return view;
}
private void initViews(View view) {
// Find the views from the resource file
mListView = (ListView) view.findViewById(R.id.myListView);
mInput = (EditText) view.findViewById(R.id.input);
mButton = (Button) view.findViewById(R.id.submit);
// Set the lists adapter
mListView.setAdapter(mPlaceAdapter);
// Attach on click listener
mButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.submit:
// On click of mButton
String str = mInput.getText().toString();
String shopName = "" + str.charAt(0);
String catName = "" + str.charAt(1);
String offerName = "" + str.charAt(2);
String expName = "" + str.charAt(3);
String imageName = "" + str.charAt(4);
// If the place was not already in the hash map, the 'add' method will return true.
if (mPlaces.add(new Place(shopName, offerName, imageName, catName, expName))) {
// Update the adapter / list view only if a new place is added.
mPlaceAdapter.notifyDataSetChanged();
}
}
}
}
public类OneFragment扩展了片段实现View.OnClickListener{
私人按钮;
私有文本输入;
私有列表视图;
私有哈希集模板;
私募适配器;
@凌驾
创建时的公共void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//创建哈希集
mPlaces=newhashset();
//基于哈希集创建适配器
mPlaceAdapter=newplaceadapter(getActivity(),R.layout.row,mPlaces,OneFragment.this);
}
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u one,容器,错误);
//初始化视图对象
初始视图(视图);
返回视图;
}
私有void initViews(视图){
//从资源文件中查找视图
mListView=(ListView)view.findViewById(R.id.myListView);
mInput=(EditText)view.findViewById(R.id.input);
mButton=(按钮)view.findViewById(R.id.submit);
//设置列表适配器
setAdapter(mPlaceAdapter);
//单击时附加侦听器
mButton.setOnClickListener(此);
}
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.提交:
//点击mButton
String str=mInput.getText().toString();
字符串shopName=”“+str.charAt(0);
字符串catName=”“+str.charAt(1);
字符串offerName=“”+str.charAt(2);
字符串expName=“”+str.charAt(3);
字符串imageName=”“+str.charAt(4);
//如果该位置不在哈希映射中,“add”方法将返回true。
if(mPlaces.add(新地点(shopName、offerName、imageName、catName、expName))){
//仅在添加新位置时更新适配器/列表视图。
mPlaceAdapter.notifyDataSetChanged();
}
}
}
}
@覆盖
受保护的空位背景(空位…空位){
HttpHandler sh=新的HttpHandler();
字符串jsonStr=sh.makeServiceCall(Utils.SERVER\uURL);
Log.e(标签,“来自url的响应:+jsonStr”);
if(jsonStr!=null){
试一试{
JSONObject JSONObject=新的JSONObject(jsonStr);
JSONArray result=jsonObject.getJSONArray(“结果”);
for(int i=0;i
您是否正确重写了equals()
和hashCode()
方法?如果您没有定义两个位置相同的的含义,那么HashSet
如何知道重复项是什么,我添加了我的Place类代码。您能帮我覆盖equal和hashcode方法吗?我没有时间做代码基础,所以如果您使用eclipse,请安装插件“code sugar”,让您生成hashcode()函数。
public class Place implements Serializable {
public String mPlace;
public String mOffer;
public String mImage;
public String mCat;
public String mExp;
public Place(){
}
public Place(String place, String offer, String image,String cat,String exp) {
this.mPlace = place;
this.mOffer = offer;
this.mImage = image;
this.mCat=cat;
this.mExp=exp;
}
public String getPlace() {
return mPlace;
}
public void setPlace(String place) {
mPlace = place;
}
public String getOffer() {
return mOffer;
}
public void setOffer(String offer) {
mOffer = offer;
}
public String getImage() {
return mImage;
}
public void setImage(String image) {
mImage = image;
}
public String getCat() {
return mCat;
}
public void setCat(String cat) {
mCat = cat;
}
public String getExp() {
return mExp;
}
public void setExp(String exp) {
mExp = exp;
}
@Override
public boolean equals(Object object) {
boolean result = false;
if (object == null || object.getClass() != getClass()) {
result = false;
} else {
Place place= (Place) object;
if (this.mPlace== place.getPlace()
&& this.mCat == place.getCat()) {
result = true;
}
}
return result;
}
@Override
public int hashCode() {
return super.hashCode();
}
}
public class OneFragment extends Fragment implements View.OnClickListener {
private Button mButton;
private EditText mInput;
private ListView mListView;
private HashSet<Place> mPlaces;
private PlaceAdapter mPlaceAdapter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create hash set
mPlaces = new HashSet<>();
// Create adapter based on hash set
mPlaceAdapter = new PlaceAdapter(getActivity(), R.layout.row, mPlaces, OneFragment.this);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
// Initialise the view objects
initViews(view);
return view;
}
private void initViews(View view) {
// Find the views from the resource file
mListView = (ListView) view.findViewById(R.id.myListView);
mInput = (EditText) view.findViewById(R.id.input);
mButton = (Button) view.findViewById(R.id.submit);
// Set the lists adapter
mListView.setAdapter(mPlaceAdapter);
// Attach on click listener
mButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.submit:
// On click of mButton
String str = mInput.getText().toString();
String shopName = "" + str.charAt(0);
String catName = "" + str.charAt(1);
String offerName = "" + str.charAt(2);
String expName = "" + str.charAt(3);
String imageName = "" + str.charAt(4);
// If the place was not already in the hash map, the 'add' method will return true.
if (mPlaces.add(new Place(shopName, offerName, imageName, catName, expName))) {
// Update the adapter / list view only if a new place is added.
mPlaceAdapter.notifyDataSetChanged();
}
}
}
}
@Override
protected Void doInBackground(Void... voids) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(Utils.SERVER_URL);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObject = new JSONObject(jsonStr);
JSONArray result = jsonObject.getJSONArray("result");
for (int i = 0; i < result.length(); i++) {
JSONObject data = result.getJSONObject(i);
String day = data.getString("day");
dataModels.add(day);
LinkedHashSet<String> lhs = new LinkedHashSet<String>();
lhs.addAll(dataModels);
// Removing ArrayList elements
dataModels.clear();
dataModels.addAll(lhs);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}