gpt4 book ai didi

java - ListView删除元素后如何刷新?

转载 作者:行者123 更新时间:2023-12-01 11:19:18 26 4
gpt4 key购买 nike

我创建了一个 ListView ,但是当我删除一个元素时,它不会刷新。我已经尝试过了

 adapter.notifyDataSetChanged();

getListView().invalidateViews();

在主要 Activity 中:

case R.id.ordini:

Ordini O = new Ordini();
android.app.FragmentManager fragmentManager0 = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction0 = fragmentManager0
.beginTransaction();

fragmentTransaction0.replace(R.id.frame, O);
fragmentTransaction0.commit();
  • Ordini.java

    public class Ordini extends ListFragment implements AdapterView.OnItemClickListener {

    int ArraySize;
    CustomAdapterOrdini adapter;
    private List<RowItemOrdini> rowItems;
    Button SendButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    return inflater.inflate(R.layout.ordini, null, false);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    ArraySize = MainActivity.ArrayProdotto.size();
    SendButton = (Button) getActivity().findViewById(R.id.send_btn);

    rowItems = new ArrayList<RowItemOrdini>();

    for (int i = 0; i < ArraySize; i++) {
    RowItemOrdini items = new RowItemOrdini(
    MainActivity.ArrayProdotto.get(i),
    R.drawable.ic_delete_black_24dp,
    MainActivity.ArrayCode.get(i),
    MainActivity.ArrayPrezzo.get(i),
    MainActivity.ArrayColli.get(i),
    MainActivity.ArrayQuantita.get(i));

    rowItems.add(items);
    }

    adapter = new CustomAdapterOrdini(getActivity(), rowItems);
    setListAdapter(adapter);

    getListView().setOnItemClickListener(this);


    SendButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Toast.makeText(getActivity(), "invio ordine in corso... ", Toast.LENGTH_SHORT)
    .show();
    SendOrdine();
    }
    });
    }
    @Override
    public void onResume(){
    super.onResume();
    adapter.notifyDataSetChanged();
    getListView().invalidateViews();
    }
  • 自定义适配器Ordini

    public class CustomAdapterOrdini extends BaseAdapter {

    Context context;
    List<RowItemOrdini> rowItem;

    ImageView Del;
    TextView Colli, Prezzo, Quantita, Code, Product;




    CustomAdapterOrdini(Context context, List<RowItemOrdini> rowItem) {
    this.context = context;
    this.rowItem = rowItem;

    }

    @Override
    public int getCount() {

    return rowItem.size();
    }

    @Override
    public Object getItem(int position) {

    return rowItem.get(position);
    }

    @Override
    public long getItemId(int position) {

    return rowItem.indexOf(getItem(position));
    }
    @Override
    public boolean areAllItemsEnabled() {
    return false;
    }

    @Override
    public boolean isEnabled(int position) {
    return false;
    }




    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
    LayoutInflater mInflater = (LayoutInflater) context
    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    convertView = mInflater.inflate(R.layout.list_ordini, null);
    }
    Code = (TextView) convertView.findViewById(R.id.prodcode);
    Product = (TextView) convertView.findViewById(R.id.PRODOTTO);
    Del = (ImageView) convertView.findViewById(R.id.del);

    Colli = (TextView) convertView.findViewById(R.id.T1);
    Prezzo = (TextView) convertView.findViewById(R.id.T2);
    Quantita = (TextView) convertView.findViewById(R.id.T3);


    Del.setFocusable(true);
    Del.setClickable(true);

    Del.setTag(convertView);

    final RowItemOrdini row_pos = rowItem.get(position);
    // setting the image resource and title
    Del.setImageResource(R.drawable.ic_delete_black_24dp);
    Product.setText(row_pos.getTitle());
    Code.setText(row_pos.getCode());

    Colli.setText(row_pos.getColli());
    Prezzo.setText(row_pos.getPrezzo());
    Quantita.setText(row_pos.getQuantita());

    Del.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    // HERE I WANT REFRESH MY LISTVIEW

    MainActivity.ArrayQuantita.remove(0);
    MainActivity.ArrayCode.remove(0);
    MainActivity.ArrayProdotto.remove(0);
    MainActivity.ArrayColli.remove(0);
    MainActivity.ArrayPrezzo.remove(0);

    CustomAdapter.ShowDialog("Elemento eliminato con successo!", context);

    }
    });

    return convertView;

    }

有人可以帮我刷新 ListView 吗?我想刷新 CustomAdapterOrdini 中的 ListView 或 Ordini.java 中的 onResume 中的 ListView 。

public void onClick(View v) {

// HERE I WANT REFRESH MY LISTVIEW

有简单的方法吗?非常感谢!

最佳答案

您已在 MainActivity 中声明 ArrayProdotto,并且您将在 CustomAdapterOrdini 中显示来自 rowItem 的数据。您必须从两个列表中删除数据并调用 notifyDataSetChanged();

如下面的代码。

Del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// HERE I WANT REFRESH MY LISTVIEW
MainActivity.ArrayProdotto.remove(position); //Delete from main source
rowItem.remove(position) // Delete from adapter
CustomAdapter.ShowDialog("Elemento eliminato con successo!", context);
notifyDataSetChanged(); // Refresh Adapter

}
});

关于java - ListView删除元素后如何刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31449570/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com