How to get a view from an Android adapter from an element

Given the item that appears in the ListView,

Can I use its adapter (or ListView itself) to find the view that displays the item?

I am handling the storage of item data and should propagate this change back to the ListView. Adding an element causes it to be displayed using notifyDataSetChanged (), but changed elements are not redrawn using notifyDataSetChanged ()

Here is the code of my activity that edits the element:

@Override
public void onClick(View button) {
    EditText text = (EditText)(findViewById(R.id.itemText));
    if (item == null) {
       // Add - this path updates ListView
       ItemsAdapter.instance().items.add(new Item(text.getText().toString()));
    } else {
       // Save - this path does not update ListView
       this.item.setTitle(text.getText().toString());
    }
    ItemsAdapter.instance().notifyDataSetChanged();
    this.finish(); // Close activity
}

Relevant parts of my adapter:

public class ItemsAdapter extends BaseAdapter {
    ArrayList<Item> items = new ArrayList<Item>();

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Item item = (Item)getItem(position);
        // ItemTextView is a subclass of TextView which keeps a reference to the item
        // So that in my OnClickListener, I can get the underlying Item and edit it
        ItemTextView textView = null;
        if (convertView == null) {
            textView = new ItemTextView(context, item);
            // Associate with itemListener which opens edit activity for item
            textView.setOnClickListener(itemListener);
            textView.setTextSize(16);
            textView.setPadding(5, 5, 5, 5);
        } else {
            textView = (ItemTextView) convertView;
            textView.setItem(item);
        }

        return textView;
    }
}

And the Item class is a POJO that implements Parcelable.

Thanks in advance

Jeff

+3
source share
1 answer

Well, I was wrong, and the chamber was right.

. Item, . , .

0

Source: https://habr.com/ru/post/1795627/


All Articles