Search code examples
androidtextviewquickaction

Perform onClick on TextView from List


I have a list with TextViews, and when the user clicks a button I do a performClick() on the corresponding TextView (to trigger an QuickAction), but the view I get is null, does anyone know why this is, and how to fix this?

This is how I create the TextView

//The holder of a store, which contains a TextView
public List<Store> stores = new ArrayList<Store>();

// Create TextView and QuickAction within loop in method createStores();
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.leftMargin = Math.round(location[0]);
lp.topMargin = Math.round(location[1]);
lp.gravity = Gravity.TOP | Gravity.LEFT;

TextView tv = new TextView(getContext());
tv.setTag(s);
tv.setLayoutParams(lp);
tv.setText(s.name);

final QuickAction quickAction = new QuickAction(getContext(), QuickAction.VERTICAL);
ActionItem nextItem = new ActionItem(s.ID, s.name, null, s.ID);
quickAction.addActionItem(nextItem);

tv.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        quickAction.show(v, null);  
    }
});

s.textview = tv;

Then, I have a search method, where I find a store from the stores List and want to simulate a click by the performClick() method. The OnClickListener is called, but the QuickAction is wrong placed because the TextView is null

// Then from another function
s.textview.performClick();

==== UPDATE ===== I solved my own problem, thanx everybody for helping!


Solution

  • I solved this: the problem was that I did the performClick while the view was not ready. So I added a delay like this to solve it:

    s.textview.postDelayed(new Runnable() {
                                    public void run() {
                                        s.textview.performClick();
                                    }
                                }, 300);