如何避免设置内联onClickListner在getView() [英] How to avoid setting inline onClickListner in getView()

查看:182
本文介绍了如何避免设置内联onClickListner在getView()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求重构 getView() code $ P $下面psented。

我有自定义适配器的ListView。每一行包含可点击的按钮和文本。

目前onClickListeners在的机身上设置getView(),这是很疯狂的想法,因为这种方法被称为非常频繁。每一个内部的onClick功能,我需要访问私有数据调用新的活动与捆绑。

我怎样才能移动的onClick定义getView外()函数?

当的onClick会叫我需要有关列表元素的位置信息(访问私人数据),并查看单击了(开始正确的活动)。

 公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    如果(convertview == NULL){
        convertView =((活动)的getContext())getLayoutInflater()膨胀(R.layout.photo_tweet_row,空)。
    }    TextView的userTweet =(TextView中)v.findViewById(R.id.feed_user_tweet_body);
    RepliesButton repliesBtn =(RepliesButton)v.findViewById(R.id.feed_replies_button);    状态twitterDataItem =的getItem(位置);    如果(twitterDataItem!= NULL){        如果(userTweet!= NULL){
            userTweet.setText(twitterDataItem.getText());
        }
        如果(repliesBtn!= NULL){
            repliesBtn.setText(回复);
        }        userTweet.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
              意向意图=新意图(的getContext(),ProfileActivity.class);
              intent.putExtra(的getContext()的getString(R.string.serializable_user),twitterDataItem.getUser());
              。的getContext()startActivity(意向);
            }
        });        repliesBtn.setOnClickListener(新View.OnClickListener(){            公共无效的onClick(查看视图){
                如果(!twitterDataItem.getComments()。contentEquals(0)){
                    意图myIntent =新意图(的getContext(),RepliesToFeedActivity.class);
                    myIntent.putExtra(photoTweet,twitterDataItem);
                    。的getContext()startActivity(myIntent);
                }
            }
        });
    }
    返回convertView;
}


解决方案

自己的活动必须实现OnClickListener,并移动实现的方法到活动水平。

根据视图你是能够检测方法的参数从UI对象事件来自(按钮,TextView的)。

至于如何在ListView检测为其记录/行。你必须设置一个自定义的标记 getView 方法的按钮和textviews,你会在事件读通过 getTag 这个标签可以是一个自定义对象太多,如果字符串是不够的。可能/推荐的方式是要在适配器的位置。

I was asked to refactor the getView() code presented below.

I have got ListView with custom Adapter. Every row contains clickable buttons and texts.

At the moment onClickListeners are set in the body of getView(), this is quite insane idea, because this method is called very frequently. Inside of every onClick function I need access to the private data to call new activity with bundle.

How can i move onClick definition outside getView() routine?

When onClick will be called I need info about position of list element (to access private data) and which View was clicked (to start correct Activity) .

public View getView(int position, View convertView, ViewGroup parent) {
    if(convertview == null) {
        convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.photo_tweet_row, null);
    }

    TextView userTweet = (TextView) v.findViewById(R.id.feed_user_tweet_body);
    RepliesButton repliesBtn = (RepliesButton) v.findViewById(R.id.feed_replies_button);

    Status twitterDataItem = getItem(position);

    if (twitterDataItem != null) {

        if (userTweet != null) {
            userTweet.setText(twitterDataItem.getText());
        }
        if (repliesBtn != null) {
            repliesBtn.setText(" replies");
        }

        userTweet.setOnClickListener(new View.OnClickListener() {           
            @Override
            public void onClick(View v) {                    
              Intent intent = new Intent(getContext(), ProfileActivity.class); 
              intent.putExtra(getContext().getString(R.string.serializable_user), twitterDataItem.getUser());
              getContext().startActivity(intent);
            }
        });

        repliesBtn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                if (!twitterDataItem.getComments().contentEquals("0")) {
                    Intent myIntent = new Intent(getContext(), RepliesToFeedActivity.class);
                    myIntent.putExtra("photoTweet", twitterDataItem);
                    getContext().startActivity(myIntent);
                }
            }
        });
    }
    return convertView;
}

解决方案

Your activitiy has to implement the OnClickListener, and move the implemented method up to the activity level.

Based on the view parameter of the method you are able to detect from which UI object the event came from (button, textview).

As for how to detect for which record/row in a listview. You have to set a custom tag on the buttons and textviews in the getView method, that you will read in the event via getTag this tag can be a custom object too if string is not enough. Probably/recommended way is to be the position in the adapter.

这篇关于如何避免设置内联onClickListner在getView()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆