I create a DialogFragment which contains various filters to customize searches on app, but the gridView with the various category doesn't work at all. I tried a lot of different code and search everything on internet, but none of them solve my problem. Hope someone can help me.
This is the Grid Adapter
public class GridFilterAdapter extends BaseAdapter {
Context context;
String[] category;
LayoutInflater inflater;
public GridFilterAdapter(Context context, String[] category){
this.context = context;
this.category = category;
}
@Override
public int getCount() {
return this.category.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = LayoutInflater.from(context).inflate(R.layout.column_filter, parent);
Log.w("IO", "controllo l'inflate nell'if");
/*view = inflater.inflate(R.layout.column_filter, parent);
Log.w("IO", "controllo convertView nell'if");*/
} else{
view = convertView;
}
TextView text = view.findViewById(R.id.crossfit_filter);
text.setText(category[position]);
return view;
}
}
This is my FragmentDialog
public class FilterFragment extends DialogFragment {
FragmentFilterBinding binding;
public static String TAG = "Filtri ricerca";
public FilterFragment() {
// Required empty public constructor
}
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.fragment_filter,null);
binding = FragmentFilterBinding.inflate(getLayoutInflater());
DisplayMetrics metrics = getResources().getDisplayMetrics();
int width = metrics.widthPixels;
view.setMinimumWidth((6 * width)/7);
builder.setView(view);
String[] item = {
"Crossfit",
"Boxe",
"Running",
"Body Building"
};
binding.gridtab.setAdapter(new GridFilterAdapter(getContext(), item));
binding.gridtab.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id){
Toast.makeText(getContext(),"Ho percepito il click su " + item[position], Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_filter, container, false);
return v;
}
}
And these are the xml file of the DialogFragment, fragment_filter.xml
<RelativeLayout 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:minHeight="450dp"
android:minWidth="1000dp"
tools:context=".FilterFragment"
android:orientation="vertical"
android:padding="@dimen/leftMargin_title">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/filter_title">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="FILTRI PER LA RICERCA"
android:fontFamily="@font/encode_sans_medium"
android:layout_marginStart="35dp"
android:layout_marginEnd="35dp"
android:layout_marginBottom="@dimen/topMargin_title"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="@dimen/topMargin_title"
android:textColor="#3b3b3b"/>
</LinearLayout>
<View
android:id="@+id/filter_divider"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="@drawable/filter_btn_gradient"
android:layout_below="@id/filter_title"
/>
<TextView
android:id="@+id/cityFilter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/filter_divider"
android:layout_marginStart="@dimen/leftMargin_title"
android:layout_marginTop="@dimen/leftMargin_title"
android:layout_marginEnd="@dimen/leftMargin_title"
android:layout_marginBottom="@dimen/leftMargin_title"
android:fontFamily="@font/encode_sans_semibold"
android:text="Filtra per città"
android:textColor="#212121"
android:textSize="20sp" />
<EditText
android:id="@+id/search_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/input_white_bck"
android:layout_below="@id/cityFilter"
android:hint="Città"
android:textColorHint="#803b3b3b"
android:textSize="19sp"
android:fontFamily="@font/encode_sans_medium"
android:paddingStart="@dimen/topMargin_title"
android:paddingEnd="@dimen/topMargin_title"
android:paddingBottom="15dp"
android:paddingTop="15dp"
android:layout_marginEnd="@dimen/topMargin_title"
android:layout_marginStart="@dimen/topMargin_title"
android:layout_marginTop="@dimen/leftMargin_title"
/>
<TextView
android:id="@+id/title_evaluation_filter"
android:layout_below="@id/search_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Filtra per recensione"
android:textSize="20sp"
android:fontFamily="@font/encode_sans_semibold"
android:textColor="#212121"
android:layout_marginStart="@dimen/leftMargin_title"
android:layout_marginTop="@dimen/leftMargin_title"
android:layout_marginEnd="@dimen/leftMargin_title"
android:layout_marginBottom="@dimen/leftMargin_title"
/>
<Spinner
android:id="@+id/spinner_evaluation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title_evaluation_filter"
android:entries="@array/evaluation"
android:layout_marginStart="@dimen/topMargin_title"
android:layout_marginEnd="@dimen/topMargin_title"
android:padding="20dp"
android:background="@drawable/input_white_bck"
/>
<TextView
android:id="@+id/title_category"
android:layout_below="@id/spinner_evaluation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Categoria di sport"
android:textSize="20sp"
android:fontFamily="@font/encode_sans_semibold"
android:textColor="#212121"
android:layout_marginStart="@dimen/leftMargin_title"
android:layout_marginTop="@dimen/leftMargin_title"
android:layout_marginEnd="@dimen/leftMargin_title"
android:layout_marginBottom="@dimen/leftMargin_title"
/>
<GridView
android:id="@+id/gridtab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/title_category"
android:numColumns="5"
>
</GridView>
</RelativeLayout>
and xml for the Grid's items, column_filter.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/crossfit_filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/category_selector"
android:textSize="20sp"
android:paddingTop="@dimen/topMargin_title"
android:paddingBottom="@dimen/topMargin_title"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:textAllCaps="false"
android:fontFamily="@font/encode_sans_medium"
android:layout_marginEnd="@dimen/topMargin_title"
android:text="Rose"
/>
</LinearLayout>