【问题标题】:Custom adapter button onClick() does not change button text?自定义适配器按钮 onClick() 不会更改按钮文本?
【发布时间】:2016-07-31 19:57:32
【问题描述】:

我的 Android 项目有一个自定义数组适配器。我在布局中添加了一个按钮,单击该按钮时,我希望它更改按钮上的文本以及测试 TextView,但是,使用我当前的代码,它并没有这样做....为什么这是?我错过了什么吗?这是我的代码:

适配器:

public class JobAdapter extends ArrayAdapter<Job> {
    private final Context context;
    private final ArrayList<Job> jobs;
    private final int layoutResourceId;
    private ExpandableTextView desc = null;
    private Button expand = null;
    private TextView test = null;
    private boolean isExpanded = false;

    public JobAdapter(Context context, int layoutResourceId, ArrayList<Job> jobs) {
        super(context, layoutResourceId, jobs);
        this.context = context;
        this.jobs = jobs;
        this.layoutResourceId = layoutResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder holder = null;
        String pay, hrs;
        final Bundle fragmentParams = new Bundle();

        //LayoutInflater inflater = (LayoutInflater) context.getSystemService((Activity.LAYOUT_INFLATER_SERVICE));
        LayoutInflater inflater = LayoutInflater.from(context);

        if (view == null) {
            view = inflater.inflate(layoutResourceId, parent, false);

            holder = new ViewHolder();
            desc = (ExpandableTextView)view.findViewById(R.id.tv_JobDesc);
            test = (TextView) view.findViewById(R.id.test);
            expand = (Button) view.findViewById(R.id.btn_descExpand);

            view.setTag(holder);
        } else {
            holder = (ViewHolder)view.getTag();
        }

        Job j = jobs.get(position);

        test.setText("testing1");

        //when user clicks the expand/collapse button, expand or collapse the description field
        expand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                expand.setText("test");
                desc.toggle();
                test.setText("testing2");

            }
        });

        return view;
    }

    static class ViewHolder
    {
        TextView title;
        TextView payrate;
        TextView dateRange;
        TextView workinghrs;
        TextView location;
        TextView companyname;
        ExpandableTextView desc;
        TextView experience;
        TextView equipment, test;
        Button apply, dismiss, expand;
    }
}

布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/test"
        android:background="@color/white"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@color/laborswipe_darkgray"
        android:textAlignment="center"
        android:text="ABC Company"
        android:layout_gravity="center_horizontal|left"
        android:paddingLeft="10dp" />

    <at.blogc.android.views.ExpandableTextView
        android:id="@+id/tv_JobDesc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="asdfaa
        \n aaaaaaaaa
        \n aaaaaaa
        \n aaaaaaa
        \n aaaaaaaaaaa
        \n aaaaaaaaa
        \n aaaaaaaaaa
        \n aaaaaaaaa
        \n aaaaaaaa"
        android:maxLines="2"
        android:ellipsize="end"
        app:animation_duration="1000"/>

    <!-- Optional parameter animation_duration: sets the duration of the expand animation -->

    <Button
        android:id="@+id/btn_descExpand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="expand"/>

</LinearLayout>

知道为什么 onClick() 不会改变 textview 文本或按钮文本吗?我不知道我做错了什么。

单击按钮时打印日志消息有效,我根本无法通过单击按钮编辑布局中控件的属性。

谢谢!

编辑:

当前 onCLick 代码:

holder.expand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.expand.setText("testing");

                holder.desc.setMaxLines(15);
                holder.test.setText("testing2");

            }
        });

【问题讨论】:

  • 按钮“展开”或“文本”上显示什么?
  • 默认情况下,按钮显示“展开”,然后当它被点击时,我希望它显示“测试”。
  • comment this // View view = convertView;并再次运行您的代码

标签: android android-layout android-arrayadapter


【解决方案1】:

我认为变量 desc,expand, text 应该是持有者变量而不是适配器变量。在当前情况下,您在每次运行 getView 方法时都会覆盖它们,因此只有最后一行是可点击的(它将覆盖变量中的所有先前视图)。

【讨论】:

  • 谢谢!我试图让它们成为持有者变量,但我得到“变量'持有者'是从内部类访问的,必须声明为最终的。”如果我在持有人中将变量声明为最终变量,那么我无法为它们赋值......有没有办法解决这个问题?
  • 为什么你的持有人是静态的?将其更改为普通类并将公共添加到参数中。
  • @user2573690 有帮助吗?
  • 是的,这似乎允许我更改文本。还有一个问题,来自同一个 onClick,如果我尝试为 textview 设置 MaxLines(),那么在公共更改后它不起作用。
  • 公开更改无权成为此问题的根源。复制当前的点击代码。
猜你喜欢
  • 1970-01-01
  • 2012-05-27
  • 1970-01-01
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多