列表视图中的按钮根据最终列表项被错误修改

Button in listview getting wrongly modified based on final list item

我正在制作一个具有三个选项卡的应用程序(类似于 WhatsApp 布局)。这三个选项卡是朋友、家人和联系人。每个选项卡中都有一个列表。每个列表项也有以下视图:左侧的名称,名称下方的说明和右侧的背景为 Drawable 的按钮,与名称在同一行。 (我之前发布过这个但现在重新发布了这个因为我已经能够找出主要错误并减少我需要放在这里的代码)

每个列表视图项都包含一个 class(朋友、家人、联系人)。所有三个 classes 都有一个标志 (canViewYouOnlineFlag)。如果此标志设置为真,相应的联系人、家人或朋友将知道您在线,反之亦然。如果标志设置为 true,按钮的颜色必须为蓝色,如果标志设置为 false,按钮的颜色必须为黑色。

我面临的问题是打开选项卡时按钮无法正确呈现。通常,必须根据 Listview 项中包含的对象的 CanViewYouOnlineFlag 设置按钮的颜色。但是,当在下一个列表视图项上调用 getView 函数时,当前列表视图项的按钮颜色正在被下一个列表视图项的对象的 CanViewYouOnlineFlag 更改。例如:

listview 中的 3 个项目,每个项目都有一个对象,三个项目的 CanViewYouOnlineflags 如下:Listview 项目 1:true,Listview 项目 2:false,Listview 项目 3:false。

列表视图项的按钮颜色在渲染时必须是蓝色、黑色、黑色。然而,结果却是一片漆黑。

同样,如果列表视图项目 3 的对象标志设置为 "TRUE",则所有按钮都是蓝色的。基本上,按钮的颜色由最后一个 Listview 项目中存在的对象决定。列表视图项中包含的对象的实际标志不是问题,我也验证了这一点(即对象标志为真但按钮仍然呈现黑色,因为最后一个列表视图项的按钮是黑色的)。请帮我解决这个问题。 以下是 main_lp_view_item 布局的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:id="@+id/main_lp_text"
    android:textSize="18sp"
    android:textColor="@color/colorPrimaryDark"
    android:focusable="false" />

<TextView
    android:id="@+id/main_lp_subtitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/main_lp_text"
    android:textSize="13sp"
    android:focusable="false" />

<Button
    android:id="@+id/can_view_you_online"
    android:layout_width="33sp"
    android:layout_height="29sp"
    android:layout_above="@+id/main_lp_subtitle"
    android:layout_marginTop="10dp"
    android:textStyle="bold"
    android:background="@drawable/lp_list_button"
    android:layout_alignParentEnd="true"
    android:focusable="false"
    />

</RelativeLayout>

以下是 LPListAdapter 的代码class Listviews 被启动的地方

public class LPListItemAdapter<T> extends BaseAdapter{  //Class for rendering each ListItem

Context context;
List<T> rowItems;

public LPListItemAdapter(Context context, List<T> rowItems)
{
    this.context=context;
    this.rowItems=rowItems;

}

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

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

@Override
public long getItemId(int position)
{
    return rowItems.indexOf(getItem(position));
}


private class ViewHolder{
    TextView main_text; //Display Name
    TextView subtitle;  //Display Description
    Button can_view_you_online;   //Button to set and display status of CanViewYouOnline flag of the class

}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder holder=null;


    LayoutInflater mInflater= (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    holder = new ViewHolder();



    if(convertView==null)
    {
        convertView=mInflater.inflate(R.layout.main_lp_view_item, null);

        holder.main_text= (TextView) convertView.findViewById(R.id.main_lp_text);
        holder.subtitle= (TextView) convertView.findViewById(R.id.main_lp_subtitle);
        holder.canViewYouOnline= (Button) convertView.findViewById(R.id.can_view_you_online);

        convertView.setTag(holder);

    }
    else{
        holder = (ViewHolder) convertView.getTag();
    }


    final T rowItem= rowItems.get(position);

    String main_text;
    String subtitle;

    if(rowItem instanceof Friends)    //If Row Item is a Friend class
    {
        main_text= ((Friends) rowItem).getName();
        subtitle=  ((Friends) rowItem).getDescription();
    }
    else if(rowItem instanceof Family)  //If Row Item is a Family class
    {
        main_text= ((Family) rowItem).getName();
        subtitle= ((Family) rowItem).getDescription();
    }
    else if(rowItem instanceof Contact) //If Row Item is a Contact class
    {
        main_text=((Contact) rowItem).getName();
        subtitle=((Contact) rowItem).getDescription();
    }
    else
    {
        main_text="NA";
        subtitle="";
    }

    holder.main_text.setText(main_text);
    holder.subtitle.setText(subtitle);

    if(getflagstatus(rowItem))              //If rowItem's canViewYouOnline flag is set true, then let color of button be default (i.e. blue)
        holder.canViewYouOnline.getBackground().clearColorFilter(); 
    else                                        //Else (if it false), make it black
        holder.canViewYouOnline.getBackground().setColorFilter(Color.argb(255, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);     

//This part of the function is the problem because the color filter of the drawable of the button in the current list item gets overriden when
//this same code is executed for the next listview item.


    holder.canViewYouOnline.setOnClickListener(new View.OnClickListener() {
        boolean buttonClickFlag;



        @Override
        public void onClick(View v) {           //The Onclick function allows one to click the button on the list item and set/reset the canViewYouOnline flag. It is working fine. 
            buttonClickFlag=getflagstatus(rowItem);       //get buttonClickFlag from canViewYouOnline flag status
            if(buttonClickFlag==false)          //If buttonClickFlag is false
            {
                v.getBackground().clearColorFilter();   //set color of button drawable to default (which is blue)
                buttonClickFlag=true;           //set buttonClickflag to true

            }
            else    //if buttonClickFlag is true
            {

                v.getBackground().setColorFilter(Color.argb(255,0,0,0), PorterDuff.Mode.SRC_ATOP);  //set color of button drawable to black
                buttonClickFlag=false;          //set buttonClickflag to false

            }
    //update the canViewYouOnline flag in the rowItem by calling the onlineStatusUpdateButtonOnClickActivity function. 
    //Function is working perfectly so is not included in the stack overflow entry due to space constraints

            onlineStatusUpdateButtonOnClickActivity(rowItem, buttonClickFlag,v); 

        }
    });



return convertView;

}


public boolean getflagstatus(T rowItem)
{
    //Function to get the CanViewYouOnlineFlag() from the underlying class in the rowItem

    boolean flagStatus=false;
    if(rowItem instanceof Friends)      
        flagStatus=((Friends) rowItem).getCanViewYouOnlineFlag();   
    else if(rowItem instanceof  Family)
        flagStatus=((Family) rowItem).getCanViewYouOnlineFlag();
    else if(rowItem instanceof Contact)
        flagStatus= ((Contact) rowItem).getCanViewYouOnlineFlag();

    return flagStatus;
}
public void onlineStatusUpdateButtonOnClickActivity(T rowItem, boolean mLBflag, View convertView)   {//WORKS FINE SO NOT PUTTING IT HERE DUE TO SPACE CONSTRAINT}

}

你需要做这样的事情:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView list = (ListView) findViewById(R.id.list);
        ArrayList<TopicDTO> data = new ArrayList<>();
        data.add(new TopicDTO("1", "Info 1", true));
        data.add(new TopicDTO("2", "Info 2", false));
        data.add(new TopicDTO("3", "Info 3", true));
        data.add(new TopicDTO("4", "Info 4", false));
        list.setAdapter(new ButtonListAdapter(getBaseContext(), data));


    }

}

ButtonListAdapter.java

public class ButtonListAdapter extends BaseAdapter {  //Class for rendering each ListItem

    Context context;
    List<TopicDTO> rowItems;

    public ButtonListAdapter(Context context, List<TopicDTO> rowItems) {
        this.context = context;
        this.rowItems = rowItems;

    }

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

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

    @Override
    public long getItemId(int position) {
        return rowItems.indexOf(getItem(position));
    }


    private class ViewHolder {
        TextView main_text; //Display Name
        TextView subtitle;  //Display Description
        Button can_view_you_online;   //Button to set and display status of CanViewYouOnline flag of the class

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;


        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        holder = new ViewHolder();


        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.main_lp_view_item, null);

            holder.main_text = (TextView) convertView.findViewById(R.id.main_lp_text);
            holder.subtitle = (TextView) convertView.findViewById(R.id.main_lp_subtitle);
            holder.can_view_you_online = (Button) convertView.findViewById(R.id.can_view_you_online);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        final TopicDTO rowItem = rowItems.get(position);

        String main_text;
        String subtitle;


        holder.main_text.setText(rowItem.info);
        holder.subtitle.setText(rowItem.info);

        if (rowItem.canViewYouOnline) {
            holder.can_view_you_online.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
        } else {
            holder.can_view_you_online.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
        }


        holder.can_view_you_online.setOnClickListener(new View.OnClickListener() {
            boolean buttonClickFlag;


            @Override
            public void onClick(View v) {           //The Onclick function allows one to click the button on the list item and set/reset the canViewYouOnline flag. It is working fine.

            }
        });


        return convertView;

    }
}

我得到了答案。如果您使用单个可绘制资源并且仅在更改标志时更改颜色,则会出现此错误。相反,使用两个可绘制对象,当 CanViewYouOnline 为真时,将按钮背景设置为可绘制对象 1,当 CanViewYouOnline 为假时,将按钮背景设置为可绘制对象 2。