从List执行onClick TextView

时间:2012-06-13 07:27:38

标签: android textview quickaction

我有一个包含TextViews的列表,当用户单击一个按钮时,我在相应的TextView上执行performClick()(触发QuickAction),但是我得到的视图为null,有没有人知道为什么会这样,并且如何解决这个问题?

这就是我创建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;

然后,我有一个搜索方法,在那里我从商店列表中找到商店,并希望通过performClick()方法模拟点击。调用OnClickListener,但由于TextView为空

,因此放置了错误的QuickAction
// Then from another function
s.textview.performClick();

====更新===== 我解决了自己的问题,而不是每个人的帮助!

2 个答案:

答案 0 :(得分:1)

我解决了这个问题:问题是我在视图未准备好的时候执行了performClick。所以我添加了这样的延迟来解决它:

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

答案 1 :(得分:0)

*tvlike.setTag(""+groupPosition);
            tvlike.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    int position=Integer.parseInt(v.getTag().toString());*
                //...........use this position and get your data