【发布时间】: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