I'm starting my studies on Android now, and I'm doing some testing.
I have two AutoComplitTextView fields, the two shows an id and a description. X1 and X2 (fictitious names). The X2 list is filtered by choosing X1. Is there any way that if the user erases the id (even if only a number) from X1 the field X2 is cleared?
X1 = Cost X2 = Operation
My fields xml is like this.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="C.Custo"
android:id="@+id/textView14"
android:textSize="25sp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et_ccusto"
android:layout_weight="1"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/btn_ccusto"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Operacão"
android:id="@+id/textView15"
android:textSize="25sp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et_operacao"
android:layout_weight="1"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/btn_operacao"
android:textSize="20sp" />
I'm using this in my Activity.
acetCcusto.setAdapter(adapterCcusto);
acetOperacao.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!acetCcusto.getText().toString().equals("")){
int cdEquipamento = util.getInt(tvEquip.getText().toString());
int cdCcusto = util.getInt(acetCcusto.getText().toString());
adapterOperacao = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, CcBO.getArrayOperacao(cdCcusto));
adapterOperacao = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, EOBO.getArrayCdOperacao(cdEquipamento));
acetOperacao.setAdapter(adapterOperacao);
}
}
});
btnCcusto.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick(View v) {
Log.i(TAG, "onClick: ");
new MaterialDialog.Builder(context)
.title(R.string.title)
.items(CcBO.getArrayCcusto())
.itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
acetCcusto.setText(text.toString());
acetOperacao.setText("");
}
})
.show();
}
});
btnOperacao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!acetCcusto.getText().toString().equals("")) {
int cdCcusto = util.getInt(acetCcusto.getText().toString());
cdOperacao = CcBO.getArrayOperacao(cdCcusto);
if (cdOperacao!=null){
adapterOperacao = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, cdOperacao);
}
new MaterialDialog.Builder(context)
.title(R.string.title)
.items(CcBO.getArrayOperacao(cdCcusto))
.itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
acetOperacao.setText(text.toString());
}
})
.show();
}
}
});