ListView中的按钮上的OnClickListener更改多个项目

时间:2017-08-13 15:58:13

标签: android listview onclicklistener

所以我为ListView中的每个项目声明了一个按钮,但它发生的是它响应更多的项目,然后只响应被点击的项目。例如,如果我按下第一个按钮的按钮,第9和第18项也会被更改。如果你可以帮我解决这个问题,那将是非常棒的:)提前感谢

This is the CusomtListAdapter which fills the ListView

    public class CustomListAdapter extends BaseAdapter {

    private ArrayList<Task> tasks;
    private LayoutInflater inflater = null;
    private Application application;

    public CustomListAdapter(Activity activity, Application application,
                             ArrayList<Task> tasks) {
        this.application = application;
        this.tasks = tasks;
        inflater = (LayoutInflater) application.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public static class ViewHolder {
        TextView lv_tv_description;
        TextView lv_tv_period;
        Button lv_bt_done;
        TextView lv_gone_pk;
        TextView lv_gone_group_pk;
        TextView lv_gone_description;
        TextView lv_gone_period;
        TextView lv_gone_period_kind;
        TextView lv_gone_time;
        TextView lv_gone_done;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View vi, ViewGroup parent) {

        final ViewHolder viewHolder;

        if (vi == null) {
            vi = inflater.inflate(R.layout.listview, parent, false);
            viewHolder = new ViewHolder();

            viewHolder.lv_tv_description = (TextView) vi.findViewById(R.id.lv_tv_description);
            viewHolder.lv_tv_period = (TextView) vi.findViewById(R.id.lv_tv_period);
            viewHolder.lv_bt_done = (Button) vi.findViewById(R.id.lv_bt_done);

            viewHolder.lv_gone_pk = (TextView) vi.findViewById(R.id.lv_gone_pk);
            viewHolder.lv_gone_group_pk = (TextView) vi.findViewById(R.id.lv_gone_group_pk);
            viewHolder.lv_gone_description = (TextView) vi.findViewById(R.id.lv_gone_description);
            viewHolder.lv_gone_period = (TextView) vi.findViewById(R.id.lv_gone_period);
            viewHolder.lv_gone_period_kind = (TextView) vi.findViewById(R.id.lv_gone_period_kind);
            viewHolder.lv_gone_time = (TextView) vi.findViewById(R.id.lv_gone_time);
            viewHolder.lv_gone_done = (TextView) vi.findViewById(R.id.lv_gone_done);


            vi.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) vi.getTag();
        }

        viewHolder.lv_tv_description.setText(tasks.get(position).getDescription());
        viewHolder.lv_tv_period.setText(tasks.get(position).getPeriod_kind());

        viewHolder.lv_bt_done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatabaseHandler dbhandler = new DatabaseHandler(application);


                View pk_view = (View) v.getParent().getParent().getParent();
                TextView textview_pk = ((TextView) pk_view.findViewById(R.id.lv_gone_pk));

                View parentView = (View) v.getParent().getParent();
                TextView textview1 = ((TextView) parentView.findViewById(R.id.lv_tv_description));
                TextView textview2 = ((TextView) parentView.findViewById(R.id.lv_tv_period));
                Button button = ((Button) parentView.findViewById(R.id.lv_bt_done));

                if (viewHolder.lv_bt_done.getText().toString().equalsIgnoreCase("done")) {
                    viewHolder.lv_tv_description.setPaintFlags(viewHolder.lv_tv_description.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                    viewHolder.lv_gone_description.setPaintFlags(viewHolder.lv_gone_description.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                    viewHolder.lv_bt_done.setText("UNDO");
                    viewHolder.lv_bt_done.setBackgroundResource(R.drawable.layout_rounded_background_accent);

                    notifyDataSetChanged();

                    dbhandler.updateDone(Integer.valueOf(textview_pk.getText().toString()), "true");
                } else if (viewHolder.lv_bt_done.getText().toString().equalsIgnoreCase("undo")) {
                    viewHolder.lv_tv_description.setPaintFlags(viewHolder.lv_tv_description.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
                    viewHolder.lv_gone_description.setPaintFlags(viewHolder.lv_gone_description.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
                    viewHolder.lv_bt_done.setText("DONE");
                    viewHolder.lv_bt_done.setBackgroundResource(R.drawable.layout_rounded_background);

                    notifyDataSetChanged();

                    dbhandler.updateDone(Integer.valueOf(textview_pk.getText().toString()), "false");
                }

                dbhandler.close();
            }
        });

        viewHolder.lv_gone_pk.setText(String.valueOf(tasks.get(position).getPk()));

        if (tasks.get(position).getDone().equalsIgnoreCase("true")) {
            viewHolder.lv_tv_description.setPaintFlags(viewHolder.lv_tv_description.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            viewHolder.lv_tv_period.setPaintFlags(viewHolder.lv_tv_description.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            viewHolder.lv_bt_done.setText("UNDO");
            viewHolder.lv_bt_done.setBackgroundResource(R.drawable.layout_rounded_background_accent);
        }

        return vi;
    }}

这是所有项目的匹配布局文件

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

    <LinearLayout
        android:focusable="false"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:focusable="false"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <RelativeLayout
                android:focusable="false"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <TextView
                    android:focusable="false"
                    android:id="@+id/lv_tv_description"
                    android:textSize="20dp"
                    android:textColor="@android:color/black"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_marginTop="7dp"/>

            </RelativeLayout>

            <RelativeLayout
                android:focusable="false"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="2">

                <TextView
                    android:focusable="false"
                    android:id="@+id/lv_tv_period"
                    android:textColor="@android:color/black"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"/>

            </RelativeLayout>

        </LinearLayout>

        <RelativeLayout
            android:focusable="false"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="4">

            <Button
                android:id="@+id/lv_bt_done"
                android:text="DONE"
                android:focusable="false"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentTop="true"
                android:layout_marginTop="10dp"
                android:background="@drawable/layout_rounded_background"/>

        </RelativeLayout>

    </LinearLayout>

    <!-- invisible TextViews -->
    <TextView
        android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lv_gone_pk"
        android:visibility="visible"
        android:layout_alignParentRight="true"/>

    <TextView
        android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lv_gone_group_pk"
        android:visibility="visible"
        android:layout_alignParentRight="true"/>

    <TextView
        android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lv_gone_description"
        android:visibility="gone"
        android:layout_alignParentRight="true"/>

    <TextView
        android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lv_gone_period"
        android:visibility="gone"
        android:layout_alignParentRight="true"/>

    <TextView
        android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lv_gone_period_kind"
        android:visibility="gone"
        android:layout_alignParentRight="true"/>

    <TextView
        android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lv_gone_time"
        android:visibility="gone"
        android:layout_alignParentRight="true"/>

    <TextView
        android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lv_gone_done"
        android:visibility="gone"
        android:layout_alignParentRight="true"/>

</RelativeLayout>

如果您还有其他需要,请在评论中告诉我们:)

1 个答案:

答案 0 :(得分:1)

View通过ListView方法的第二个参数(convertView)在getView()中重复使用。

从文档中

  

convertView - 如果可能,重用旧视图。注意:你应该   检查此视图是否为非null且之前是否为适当的类型   使用。如果无法转换此视图以显示   正确的数据,这种方法可以创建一个新的视图。

因此,在任何时候,只有设备屏幕上可见的视图数量(加上几个)才能在内存中使用。当您滚动列表时,屏幕上的视图将重新用于进入屏幕的项目。

在您的情况下,第1项,第9项和第18项与重复使用的View相同。这就是为什么当您仅在第一项上按下按钮时,第9项和第18项的更改View

解决方案:

按下按钮时,在某些数据结构中添加该状态信息。例如,在array booleans中,对于按钮处于按下状态的项目保存为true,对其他按钮保存为false。

getView()方法中,检查该布尔数组中的按钮状态并分别更改视图。