0

When i Add Item to listview , listview doesn't show my added item. when i add the Item to adapter i want to seem it in listview... But it doesn't work. Where is my fail ? There is any example about Listview (Add/Remove/Edit) Example too ?

Adapter Code

 android.content.Context Context;
    LayoutInflater inflater;

    ArrayList<Item> Items=new ArrayList<>();



    public MyAdapter(Context Context)
    {

        this.Context=Context;

    }


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

    @Override
    public Object getItem(int position) {
        return Items.get(position);
    }

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

    public void AddItem(Item item)
    {
        Items.add(item);
        MyAdapter.this.notifyDataSetChanged();
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageButton BtnSil;
        inflater=(LayoutInflater) Context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=inflater.inflate(R.layout.fragment_lstview,parent,false);

        BtnRemove=(ImageButton)view.findViewById(R.id.Remove);

        BtnRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Urunler.remove(position);
                MyAdapter.this.notifyDataSetChanged();
            }
        });
        return view;
    }

Item Add Code

adapter.AddItem(SecilenUrun);
1

1 Answer 1

0

It should update. While rebuilding the view is a giant waste and an error, it should work. The most suspect thing is the getItemId(int position) returning 0. If the view is treating the ids as static that will often result in no-update and no values on screen have changed. Typically the default is return position, or some proper hashed value of item.

But, certainly that should update. Though all the entries would look identical though Urunler would have the remove called for that position. Which wouldn't look significantly different other than the number of items.

Sign up to request clarification or add additional context in comments.

1 Comment

I am going to try your sad

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.