如何从ArrayAdapter中删除项目?

问题描述 投票:3回答:2

在我的应用程序中,我应该使用ListViewArrayAdapter显示一些列表。 (我不应该使用RecyclerView所以不建议我使用它)。

我希望当计时器收到0时删除这个项目。

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

我写这段代码用于删除:

        if (remainTime < 0) {
            startTimer();

            remove(position);
            notifyDataSetChanged();
        }

但没有工作我没有删除任何项目!

如何从适配器中删除项目?

android listview android-arrayadapter
2个回答
3
投票

我想你可以用这个:

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

嘿,如果不工作,那么我已经为你创建了一个样本。如果你可以共享你的xml文件或正确的代码正常工作,那么我们可以很容易地帮助你,你可以像这样使用。

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

0
投票

目前尚不清楚你在调用哪种remove方法。你派生的BaseAdapter没有定义任何。 ArrayAdapter确实定义了remove方法,该方法将模型对象移除,而不是索引。传递位置(即整数)时,Java会将其转换为Java Integer,因此编译但不能按预期工作。

您可以先获取该值,然后尝试删除。如果我在你的代码中正确理解了一切,那么很可能是remove(datas.get(position))

© www.soinside.com 2019 - 2024. All rights reserved.