ytyuj
ytyuj

Reputation: 31

How to remove item from ArrayAdapter?

In my application i should use ListView and ArrayAdapter for show some list. (I shouldn't use RecyclerView so not recommend to me to use it).

I want when timer receive to 0 delete this item.

public class AuctionTodayListAdapter extends BaseAdapter {

    private TextView proName, progressTxt, txtPercent, txtPrice2, txtStartPrice, txtPrice, progressFinishedTxt,
            text_view_169622, price, timeView, edtUserName, today_inputPriceTxt, today_basePriceTxt,
            txtDate, under10_priceTxt;
    private CustomBadge bidCount, offerCount;
    private ProgressBar progressBar;
    private ImageView GoodPic, userPic, User;
    private CountdownView countdownView;

    public AuctionTodayListAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<TodayGmodel> objects) {
        super(context, resource, objects);

    }

    @SuppressLint("SetTextI18n")
    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        final TodayGmodel model = (TodayGmodel) datas.get(position);

        View view;
        if (convertView != null)
            view = convertView;
        else
            view = View.inflate(context, layout, null);

        Constants.setFont(parent);
        df = new DecimalFormat(",###.##");
        Log.e("todayTimer", model.getCalculateEnd() + "");
        proName = (TextView) view.findViewById(R.id.text_view_16962);
        progressTxt = (TextView) view.findViewById(R.id.progressTxt);
        txtPercent = (TextView) view.findViewById(R.id.txtPercent);
        txtPrice2 = (TextView) view.findViewById(R.id.txtPrice2);
        txtPrice = (TextView) view.findViewById(R.id.txtPrice);
        text_view_169622 = (TextView) view.findViewById(R.id.text_view_169622);
        txtStartPrice = (TextView) view.findViewById(R.id.txtStartPrice);

        countdownView.setOnCountdownIntervalListener(1000, new CountdownView.OnCountdownIntervalListener() {
            @Override
            public void onInterval(CountdownView cv, long remainTime) {
                if (remainTime < 10000 && remainTime > 0) {
                    winnerLay.setVisibility(View.GONE);
                    listItem_winnerLay.setVisibility(View.GONE);
                    listItem_todayUpper10Lay.setVisibility(View.GONE);
                    listItem_todayPriceLay.setVisibility(View.VISIBLE);
                    listItem_under10Lay.setVisibility(View.VISIBLE);
                    under10_priceTxt.setText(df.format(model.getPrice()) + "Dollar");

                    progressBar.setMax((int) timeInMillis / 1000);
                    isRunning = true;

                    countDownTimer = new CountDownTimer(timeInMillis, 100) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            progressTxt.setText("" + String.valueOf(Math.round(millisUntilFinished * 0.001f)));
                            progressBar.setProgress(Math.round(millisUntilFinished * 0.001f));
                        }

                        @Override
                        public void onFinish() {

                        }
                    }.start();
                    countDownTimer.start();
                }
                if (remainTime < 0) {
                    startTimer();

                    remove(position);
                    notifyDataSetChanged();
                }
            }
        });

I write this code for delete :

        if (remainTime < 0) {
            startTimer();

            remove(position);
            notifyDataSetChanged();
        }

but not work me and not deleted any item!

How can i remove item from adapter?

Upvotes: 2

Views: 1544

Answers (2)

aj0822ArpitJoshi
aj0822ArpitJoshi

Reputation: 1182

i think you can use this:

if (remainTime < 0) {
        startTimer();
      // your list object
        datas.remove(position);
        notifyDataSetChanged();
    }

hey if is not working then i have create one sample for you.its working properly if you can share your xml file or proper code then we can help you easily else you can use like this.

public class MainActivity extends AppCompatActivity {
EditText textView;

  ArrayList<Integer> newArraylist;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    newArraylist = new ArrayList<>();
    newArraylist.add(R.mipmap.ic_launcher);
    newArraylist.add(R.mipmap.ic_launcher);
    newArraylist.add(R.mipmap.ic_launcher);
    newArraylist.add(R.mipmap.ic_launcher);
    newArraylist.add(R.mipmap.ic_launcher);
    newArraylist.add(R.mipmap.ic_launcher);
    newArraylist.add(R.mipmap.ic_launcher);
    newArraylist.add(R.mipmap.ic_launcher);

    GridView simpleGrid = (GridView) findViewById(R.id.simpleGridView);
    AuctionTodayListAdapter customAdapter = new AuctionTodayListAdapter(newArraylist);
    simpleGrid.setAdapter(customAdapter);
}


public class AuctionTodayListAdapter extends BaseAdapter {
    Context context;
    private TextView proName, progressTxt, txtPercent, txtPrice2, txtStartPrice, txtPrice, progressFinishedTxt,
            text_view_169622, price, timeView, edtUserName, today_inputPriceTxt, today_basePriceTxt,
            txtDate, under10_priceTxt;
    ArrayList<Integer> newString;

    public AuctionTodayListAdapter(ArrayList<Integer> objects) {
        newString = objects;
        context = MainActivity.this;
    }

    @Override
    public int getCount() {
        return newString.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflter = (LayoutInflater.from(getApplicationContext()));
        View view = inflter.inflate(R.layout.activity_grid, null);//set layout for displaying items
        ImageView icon = (ImageView) view.findViewById(R.id.icon);
        icon.setImageResource(newString.get(position));
        icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                newString.remove(position);
                notifyDataSetChanged();
            }
        });

        return view;
    }
}}

Upvotes: 3

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21748

It is not clear which remove method are you calling. BaseAdapter you derive from does not define any. ArrayAdapter does define the remove method that takes the model object to be removed, not the index. When you pass the position (that is an integer), Java converts it into Java Integer so this compiles but does not work as expected.

You may fetch the value first and then try to remove. Most likely remove(datas.get(position)) if I understand everything correctly in your code.

Upvotes: 0

Related Questions