java - 如何为列表适配器中的按钮创建 OnClickListener,该按钮将允许我设置 EditText View 的可见性

标签 java android onitemclicklistener

我当前有一个包含按钮和 EditText View 的 ListView 。如何在列表适配器中正确实现 OnClickListener,以便在单击每个按钮时, View 中关联的 EditText 通过 setVisibility 方法隐藏。

根据我当前在列表适配器中实现的 OnClickListener,当我单击按钮来隐藏 View 中相应的 EditText 时,它会隐藏视口(viewport)中的最后一个 EditText,并且不会隐藏同一 View 中相应的 EditText查看为按钮。下面是我的 ListView xml 文件 (inspection_single_row.xml)、我的列表适配器 (InspectionAdapter.java) 和主要 Activity (MainActivity)。

inspection_single_row.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Was the sink cleaned floors mopped"
            android:id="@+id/text_id"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/check_boxes"
            android:layout_marginBottom="20dp"
            android:gravity="center_horizontal">


            <RadioGroup
                android:id="@+id/groupRadio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/radioComplete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Complete"
                    android:checked="false"
                    android:textColor="@color/grey_mid"/>

                <RadioButton
                    android:id="@+id/radioIncomplete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Incomplete"
                    android:checked="false"
                    android:textColor="@color/grey_mid"
                    android:layout_marginLeft="25dp"/>

            </RadioGroup>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="click"
                android:onClick="clickMe"
                android:id="@+id/btn"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/master_linlayout"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp">


            <EditText
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_marginBottom="20dp"
                android:gravity="top"
                android:padding="10dp"
                android:textSize="14sp"
                android:background="@drawable/border2"
                android:inputType="textMultiLine"
                android:textColor="@color/grey_mid"
                android:id="@+id/edit_text"/>


        </LinearLayout>


    </LinearLayout>


</LinearLayout>

InspectionAdapter.java

public class InspectionAdapter extends ArrayAdapter<InspectionObject> {

    ArrayList<InspectionObject> arrayList;
    Context context;
    int Resource;
    LayoutInflater layoutInflater;
    ProgressHolder holder;

    public InspectionAdapter(Context context, int resource, ArrayList<InspectionObject> objects) {
        super(context, resource, objects);


        this.context = context;
        arrayList = objects;
        Resource = resource;

        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    private static class ProgressHolder {

        public RadioGroup radio_group;
        public EditText deficiency_notes;
        public TextView inspection_task;
        public RadioButton radio_yes;
        public RadioButton radio_no;
        public LinearLayout master_layout;
        public Button my_button;

    }

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

        View v = convertView;
        holder = new ProgressHolder();

        if(v == null)
            {
            v = layoutInflater.inflate(Resource, null);

            holder.radio_group = (RadioGroup)v.findViewById(R.id.groupRadio);
            holder.deficiency_notes = (EditText)v.findViewById(R.id.edit_text);
            holder.inspection_task = (TextView)v.findViewById(R.id.text_id);
            holder.radio_yes = (RadioButton)v.findViewById(R.id.radioComplete);
            holder.radio_no = (RadioButton)v.findViewById(R.id.radioIncomplete);
            holder.master_layout = (LinearLayout)v.findViewById(R.id.master_linlayout);
            holder.my_button = (Button)v.findViewById(R.id.btn);

            v.setTag(holder);

            }else{
            holder = (ProgressHolder)v.getTag();
        }

        final InspectionObject inspectionObject = arrayList.get(position);

        holder.my_button.setTag(position);
        holder.deficiency_notes.setTag(position);
        holder.my_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int pos = (Integer) v.getTag();  //the real and updated position
                Log.i("ConfirmAdapter","Button @ position : " + pos);

                Log.i("ConfirmAdapter","EditText @ position : " + holder.deficiency_notes.getTag());

            }
        });


        return v;
    }

}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    ListView lv;
    InspectionAdapter inspection_adapter;
    ArrayList<InspectionObject> inspectionList;
    Boolean eval;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = findViewById(R.id.listView2);

        inspectionList = new ArrayList<InspectionObject>();

        inspectionList = new ArrayList<InspectionObject>();

        inspectionList.add(new InspectionObject(true, "", "Were the floor mopped?"));
        inspectionList.add(new InspectionObject(true, "", "Were the mirrors cleaned?"));
        inspectionList.add(new InspectionObject(false, "", "Were the toilets cleaned?"));
        inspectionList.add(new InspectionObject(true, "", "Was high/low dusting performed?"));

        inspection_adapter = new InspectionAdapter(getApplicationContext(), R.layout.inspection_single_row, inspectionList);
        lv.setAdapter(inspection_adapter);



    }


}

最佳答案

编辑: 在这一部分

Log.i("ConfirmAdapter","EditText @ position : " + holder.deficiency_notes.getTag())

您仍在引用最后创建的holder变量。正如我之前所说:在 getView 中,对于每个 View ,您都会创建一个新的 ProgressHolder,并将其分配给holder 变量。所以每次调用 getView 时,holder 都会被覆盖。这就是为什么 Log.i 给出了你的最后一个项目。

尝试以下操作:

将新的 ProgressHolder 放入 if 子句中。

if(v == null)
        {
            holder = new ProgressHolder();

这样,当 View 为空时,它只会创建一个新实例。

您可以将其设置为支架,而不是为按钮设置标签来定位

holder.my_button.setTag(holder);

您不需要为EditText设置标签。

然后在 onClick 中通过 getTag() 获取相应的 ProgressHolder 实例并更改可见性,如下所示:

holder.my_button.setTag(holder);
holder.my_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        ProgressHolder clickedHolder = (ProgressHolder)view.getTag();
        clickedHolder.deficiency_notes.setVisibility(View.GONE);

    }
});

关于java - 如何为列表适配器中的按钮创建 OnClickListener,该按钮将允许我设置 EditText View 的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55479349/

相关文章:

java - 我如何告诉 JBoss 4.2.3.GA 首先在我的 war 文件 lib 目录中加载 jar?

android - 如何更新媒体 Controller android的进度条?

android 操作栏 onNavigationItemSelected

android - 我如何使用 OnItemClickListener 根据单击的项目启动新 Intent ?

java - 如何在ListView中使用onItemClickListener获取其他列

java - Hibernate 使用命名查询调用存储过程

java - 打开 Eclipse juno 时出错

java - 安装 Roxio 时出现命令路径问题

php - 我们如何识别搜索字符串中的逗号并相应地在 MySQL 中触发查询?

java - Android - 使用 getSelectedItem 时出现 NullPointer 异常