我有一个包含5个成员变量的类Place。单击按钮用户,输入一个新的Place对象,我将其放在listview中。我正在使用LinkedHashset来防止重复,但我仍然可以在listview中看到双重输入。请建议。 我的代码:
public class OneFragment extends Fragment {
private Button mButton;
List<String> inputStrings = new ArrayList<String>();
List<Place> placeList=new ArrayList<Place>();
LinkedHashSet<Place> hashPlace=new LinkedHashSet<Place>();
LinkedHashSet<String> hashSet=new LinkedHashSet<String>();
private ListView mListView=null;
private PlaceAdapter mPlaceAdapter;
public OneFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_one, container, false);
input=(EditText)view.findViewById(R.id.input);
mListView=(ListView)view.findViewById(R.id.myListView);
mButton=(Button)view.findViewById(R.id.submit);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String str = input.getText().toString();
String shopName = Character.toString(str.charAt(0));
String catName = Character.toString(str.charAt(1));
String offerName = Character.toString(str.charAt(2));
String expName = Character.toString(str.charAt(3));
String imageName = Character.toString(str.charAt(4));
Place plc=new Place(shopName,offerName,imageName,catName,expName);
//Check duplication
if (hashPlace.add(plc)){
placeList=new ArrayList<Place>(hashPlace);}
Place mPlaces[] = placeList.toArray(new Place[placeList.size()]);
mPlaceAdapter = new PlaceAdapter(getActivity(), R.layout.row, mPlaces, OneFragment.this);
if (mListView != null) {
mListView.setAdapter(mPlaceAdapter);
}
}
});
return view;
}
}
添加地方类:
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();
}
}
答案 0 :(得分:0)
要使LinkedHashSet正常工作,必须覆盖hashcode和equals方法。在你的情况下,你必须在Place class中进行。
答案 1 :(得分:0)
我有一个使用HashSet的解决方案。如果这对您有用,请告诉我,我没有布局文件或适配器等信息来正确测试。
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();
}
}
}
}
答案 2 :(得分:0)
@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;
}