Listview处理点击错误

时间:2014-05-27 05:00:40

标签: android android-listview

我有一个由用户可以喜欢的Feed组成的列表视图。但是,在一个订阅源项目中单击类似文本视图时,会更改列表视图中多个订阅源中的喜欢的状态。 这是我的代码片段。

TextView likeStatusTextView = (TextView) view.findViewById(R.id.like_text);
    connectionDetector = new ConnectionDetector(view.getContext());
    likeStatusTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            boolean isConnected = connectionDetector.isConnected();
            if(isConnected){
                 isLiked = false;
                //TODO update value of isLiked from table
                if(likeStatusTextView.getText().equals("Liked")){
                    isLiked = true;
                }
                if(!isLiked){
                    likeStatusTextView.setTextColor(Color.rgb(119,185,44));
                         likeIcon.setImageDrawable(view.getResources().getDrawable(R.drawable.icon_liked));
                    likeStatusTextView.setText("Liked");
                }
                else{
                    likeStatusTextView.setTextColor(Color.rgb(103,176,181));

                    likeStatusTextView.setText("Like");
                }
            }
            else{
                Toast.makeText(rootView.getContext(),rootView.getResources().getString(R.string.no_internet),Toast.LENGTH_SHORT).show();
            }


        }
    });

1 个答案:

答案 0 :(得分:1)

connectionDetector = new ConnectionDetector(view.getContext());
likeStatusTextView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      TextView likeStatusTextView = (TextView) view.findViewById(R.id.like_text);
        boolean isConnected = connectionDetector.isConnected();
        if(isConnected){
             isLiked = false;
            //TODO update value of isLiked from table
            if(likeStatusTextView.getText().equals("Liked")){
                isLiked = true;
            }
            if(!isLiked){
                likeStatusTextView.setTextColor(Color.rgb(119,185,44));
                     likeIcon.setImageDrawable(view.getResources().getDrawable(R.drawable.icon_liked));
                likeStatusTextView.setText("Liked");
            }
            else{
                likeStatusTextView.setTextColor(Color.rgb(103,176,181));

                likeStatusTextView.setText("Like");
            }
        }
        else{
            Toast.makeText(rootView.getContext(),rootView.getResources().getString(R.string.no_internet),Toast.LENGTH_SHORT).show();
        }


    }
});