自定义适配器按钮onClick()不会更改按钮文本?

时间:2016-07-31 19:57:33

标签: android android-layout android-arrayadapter

我的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");

            }
        });

1 个答案:

答案 0 :(得分:0)

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