我有一个带有自定义布局和自定义适配器的android微调器。
当我点击微调器时,它会显示一个CheckButtons列表。我已将CheckboxListener与每个复选框相关联,以便在选中复选框时我想更改Spinner显示的值。
在CheckboxListener中,我引用了微调器的TextView(微调器显示的文本)。当我在适配器中使用方法setText()
时,显示的值会更改,但是当我在CheckBoxListener中使用该方法时,不会显示设置文本。我不明白为什么。
以下是代码的一部分:
的onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter_apply);
//get spinner reference
Spinner spinnerLane = (Spinner) findViewById(R.id.filt_apply_spinner_lane);
//create adapter
SpinnerAdapter adapterLane = new SpinnerAdapter(FilterApplyActivity.this,R.layout.spinner_row_layout,R.array.lanes_array,"Lane");
//assegna gli adapter
spinnerLane.setAdapter(adapterLane);
}
自定义适配器:
class SpinnerAdapter extends ArrayAdapter {
LayoutInflater inflater;
String[] data;
CheckBoxListener chkboxListener;
TextView displayedText;
//Constructor
{.....}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View layout = inflater.inflate(R.layout.spinner_row_layout, parent, false);
TextView row = (TextView) layout.findViewById(R.id.spinner_row_txt);
displayedText = row;
displayedText.setText("This is a text");
row.setTypeface(custom_font);
return layout;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View layout = inflater.inflate(R.layout.spinner_row_layout_check, parent, false);
CheckBox row = (CheckBox) layout.findViewById(R.id.spinner_row_chk);
row.setTag(position);
chkboxListener = new CheckBoxListener(displayedText);
row.setOnCheckedChangeListener(chkboxListener);
row.setText(data[position]);
row.setTypeface(custom_font);
return layout;
}
}
自定义侦听器:
public class CheckBoxListener implements CompoundButton.OnCheckedChangeListener{
String text;
TextView txtView;
public CheckBoxListener(TextView txt){
txtView = txt;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
text = "";
System.out.println("row gettext 2: " + txtView.getText()); //when i tap on a chkbox it prints: "This is a text"
if(isChecked == true){
text = "bar";
txtView.setText(text);
System.out.println("txt gettext: " + txtView.getText()); //it prints: "bar"
//txtView.invalidate();
}
}
}
spinner_row_layout.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:background="@color/very_dark_gray"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/spinner_row_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dp"
android:textSize="13dp"
android:text="foo"
android:textColor="@color/orange_words" />
</LinearLayout>
spinner_row_layout_check.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:background="@color/very_dark_gray"
android:gravity="center"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/spinner_row_chk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dp"
android:textColor="@color/orange_words"
android:textSize="13dp" />
</LinearLayout>
当我查看图片中的“中等”项目时,我需要这样显示的文本“这是一个文本”随任意文本而变化。