私は自分のサーバーからデータをロードする賞セクションを持っています。リストを更新する際のリストビューアのアダプタのバグ(新しい詳細の読み込み)
私はサーバから8×8の賞を引き出し、ListView
にロードしています。賞を販売することができ、私のアダプターにはチェッカーがあります。quantity < 1
の場合は、賞が販売されたことをユーザーに通知するバーが画像上に表示されます。
ユーザーはスワイプしてインデックスを増やして詳細を読み込むことができるため、サーバーは返すべき8つの賞を決定することができます。最初の8つは1、2番目の8は2などです。
問題は、最初に販売された賞にスクロールすると、ListViewが奇妙になり、すべての賞がその売り切れのバーを画像に表示します。これは活動のコードです:
public class AwardListFragment extends Fragment { int offset = 0;
int size = 8;
private int ID;
private boolean _isAwardsLoaded = false;
private SwipyRefreshLayout swipyRefreshLayout;
private ProgressDialog progressBar;
private ArrayList<Awards> listAwards;
ListView awardlist;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.rewards_section, parent, false);
awardlist = (ListView) rootView.findViewById(R.id.awardlist);
awardlist.setItemsCanFocus(true);
listAwards = new ArrayList<>();
swipyRefreshLayout = (SwipyRefreshLayout) rootView.findViewById(R.id.swipe_section_swipe);
awardlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), AwardDetailsScreen.class);
ID = listAwards.get(position).getId();
Bundle bundle = new Bundle();
bundle.putInt("ID", ID);
intent.putExtras(bundle);
startActivity(intent);
}
});
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
public void showlist() {
progressBar = new ProgressDialog(getActivity());
progressBar.setMessage("Pls Wait...");
progressBar.show();
final int firstitemposition = 0;
final int currentposition = awardlist.getFirstVisiblePosition();
NetworkSDK.getInstance().getAwards(size, offset, new Callback<List<Awards>>() {
@Override
public void onResponse(Call<List<Awards>> call, Response<List<Awards>> response) {
if (response.code() == 401) {
Intent intent = new Intent(getContext(), MainPreLogin.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
SharedData.getInstance().removeString("token");
} else {
if (response.isSuccess()) {
if (response.body().size() == 0) {
Toast.makeText(getContext(), "All awards loaded", Toast.LENGTH_SHORT).show();
progressBar.setCancelable(false);
progressBar.dismiss();
} else
for (int i = 0; i < response.body().size(); i++)
listAwards.add(response.body().get(i));
AwardsAdapter awardsAdapter = new AwardsAdapter(listAwards);
awardlist.setAdapter(awardsAdapter);
awardsAdapter.notifyDataSetChanged();
awardlist.setSelectionFromTop(currentposition, firstitemposition);
}
progressBar.setCancelable(false);
progressBar.dismiss();
}
}
@Override
public void onFailure(Call<List<Awards>> call, Throwable t) {
Toast.makeText(getContext(), R.string.errorNoconnection, Toast.LENGTH_LONG).show();
progressBar.dismiss();
}
});
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser && !_isAwardsLoaded) {
swipyRefreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh(SwipyRefreshLayoutDirection direction) {
if (direction == SwipyRefreshLayoutDirection.BOTTOM) {
offset++;
swipyRefreshLayout.setRefreshing(false);
showlist();
}
}
showlist();
});
}
}
}
これは私のアダプターです。お使いのアダプタのgetView()
で
public class AwardsAdapter extends BaseAdapter {
ArrayList<Awards> awards;
public AwardsAdapter(ArrayList<Awards> awards) {
this.awards = awards;
}
public void clearData() {
// clear the data
awards.clear();
}
@Override
public int getCount() {
return awards.size();
}
@Override
public Object getItem(int position) {
return awards.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
Integer identity;
ViewHolder viewHolder = null;
if (convertView == null) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.award_item, parent, false);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
Awards awards = (Awards) getItem(position);
if (awards != null) {
identity = awards.getId();
viewHolder.name.setText(awards.getName().toUpperCase());
viewHolder.price.setText("Money amount: " + awards.getPriceAmount().toString());
viewHolder.points.setText("Points amount :" + awards.getCreditAmount().toString());
if (awards.getImagePath().isEmpty())
Picasso.with(view.getContext()).load(R.drawable.placeholder).fit().centerCrop().into(viewHolder.picture);
else
Picasso.with(view.getContext()).load(awards.getImagePath()).fit().centerCrop().into(viewHolder.picture);
if (awards.getQuantity()<1)
Picasso.with(view.getContext()).load(R.drawable.sold).into(viewHolder.checker);
else
if (awards.getIsVip())
Picasso.with(view.getContext()).load(R.drawable.vip).into(viewHolder.checker);
}
return view;
}
private class ViewHolder {
TextView name;
TextView price;
TextView points;
ImageView picture;
ImageView checker;
public ViewHolder(View view) {
this.name = (TextView) view.findViewById(R.id.award_name);
this.price = (TextView) view.findViewById(R.id.award_price);
this.picture = (ImageView) view.findViewById(R.id.award_picture);
this.points = (TextView) view.findViewById(R.id.award_points);
this.checker=(ImageView)view.findViewById(R.id.checker);
}
}
}
リストアイテムの再利用の問題のようです。 – Sufian
そうだと思うけど、それが売れている賞を見つけたら、それは狂ったxD –