I have the following Activity code which ref. the R.layout.pool_details_new_layout (code below)
The Adapter ref. the number_of_players_dropdown_layout (code below)
Everything works fine, the view loads the AutoCompleteTextView dropdown box populates and when i click on it is shows all the entires in it
BUT
when i select an item, I am expecting for the numPlayersSelected.setOnItemClickListener to execute and log to the logcat and show a toast on the screen but nothing happens!?
Does anyone know what i am missing!?
Activity code:
public class PoolDetailsActivity extends AppCompatActivity {
ArrayAdapter<SkuItem> numPlayersAdapter;
private AutoCompleteTextView numPlayersSelected;
public static Activity activity = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pool_details_new_layout);
activity = this;
numPlayersSelected = (AutoCompleteTextView) findViewById(R.id.numOfPlayersAddSelected);
numPlayersAdapter = new IAPSkuAdapter(activity, R.layout.number_of_players_dropdown_layout, numPlayersArray);
numPlayersSelected.setAdapter(numPlayersAdapter);
// NOT WORKING!? <-------------------------------------
numPlayersSelected.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItemText = (String) parent.getItemAtPosition(position);
Toast.makeText(getBaseContext(),selectedItemText + " selected!", Toast.LENGTH_LONG).show();
Log.e(TAG, "<< SELECTED >> : " + selectedItemText);
}
}
}
}
number_of_players_dropdown_layout code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="15dp"
app:cardElevation="10dp"
app:cardUseCompatPadding="true"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:id="@+id/adminLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:padding="3dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Add Players"
android:layout_marginStart="10dp"
android:textSize="14sp"
android:textStyle="bold"
android:layout_gravity="center"
android:textColor="#605E5E"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/numPlayersDropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:hint="Select"
android:labelFor="@+id/numOfPlayersSelected"
android:background="#EAE7E7">
<AutoCompleteTextView
android:id="@+id/numOfPlayersAddSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:layout_weight="1"
android:padding="14dp"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
pool_details_new_layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:background="@color/black"
tools:context=".PoolDetailsActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/admin_support_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/pool_details_admin_layout" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
-------------- UPDATE ----------------
Adapter Class Code:
public class IAPSkuAdapter extends ArrayAdapter<SkuItem> {
public IAPSkuAdapter(@NonNull Context context, int resource, @NonNull List<SkuItem> skuItemList) {
super(context, resource, skuItemList);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.number_of_players_dropdown_layout, parent, false);
}
TextView skuTitle = convertView.findViewById(R.id.skuTitle);
TextView skuPrice = convertView.findViewById(R.id.skuPrice);
SkuItem skuItem = getItem(position);
skuTitle.setText(skuItem.getSkuTitle());
skuPrice.setText(skuItem.getSkuPrice());
return convertView;
}
}
Here are 2 things you could try:
clickListener()Method in the Adapter for each item:convertView.setOnClickListener(v -> { //Your code here });In this example we assume that the click method should be triggerd when theconvertViewis pressed.numPlayersArrayisn't empty, when the ClickListener gets assigned.Please let me know if it helped.