无法在自定义BaseAdapter提供的网格项上放置一个onClick监听器。

huangapple go评论91阅读模式
英文:

Not being able to place an onClickListener on the Grid Items provided by a custom BaseAdapter

问题

I currently have a grid view using a base adapter inside a fragment, and I am trying to transfer to a different fragment when one of the items is clicked but none of the solutions I found on stack overflow has worked. I might miss something.

Adapter

  1. import android.content.Context;
  2. import android.view.LayoutInflater;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.BaseAdapter;
  6. import android.widget.ImageView;
  7. import android.widget.TextView;
  8. import com.licenta.joberfrontend.R;
  9. import com.licenta.joberfrontend.rest.backend_entieties.Category;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. public class CategoriesAdapter extends BaseAdapter {
  13. public class ViewHolder {
  14. TextView textName;
  15. ImageView imageView;
  16. }
  17. private ArrayList<Category> categoryList;
  18. public Context context;
  19. public CategoriesAdapter(List<Category> apps, Context context) {
  20. this.context = context;
  21. this.categoryList = (ArrayList<Category>) apps;
  22. }
  23. @Override
  24. public int getCount() {
  25. return categoryList.size();
  26. }
  27. @Override
  28. public Object getItem(int position) {
  29. return position;
  30. }
  31. @Override
  32. public long getItemId(int position) {
  33. return position;
  34. }
  35. @Override
  36. public View getView(final int position, View view, ViewGroup parent) // inflating the layout and initializing widgets
  37. {
  38. ViewHolder viewHolder;
  39. if (view == null) {
  40. LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  41. view = inflater.inflate(R.layout.category_list_content, parent, false);
  42. viewHolder = new ViewHolder();
  43. viewHolder.textName = view.findViewById(R.id.textName);
  44. viewHolder.imageView = view.findViewById(R.id.iconView);
  45. view.setTag(viewHolder);
  46. } else {
  47. viewHolder = (ViewHolder) view.getTag();
  48. }
  49. // here we are setting up the names and images
  50. viewHolder.textName.setText(categoryList.get(position).getName());
  51. viewHolder.imageView.setImageResource(this.context.getResources().getIdentifier(categoryList.get(position).getCategoryIconId(), "mipmap", this.context.getPackageName()));
  52. return view;
  53. }
  54. }

Fragment's OnCreate

  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. final LocalStorageSaver localStorageSaver = new LocalStorageSaver(Objects.requireNonNull(getContext()));
  5. final ToastShower toastShower = new ToastShower();
  6. //REST services creation
  7. final RetrofitCreator retrofitCreator = new RetrofitCreator();
  8. final Retrofit retrofit = retrofitCreator.getRetrofit();
  9. final CategoryService categoryService = retrofit.create(CategoryService.class);
  10. final Call<List<Category>> getCategoriesRequest = categoryService.getAllCategoriesAndTheirJobs(localStorageSaver.getValueFromStorage(Constants.TOKEN));
  11. getCategoriesRequest.enqueue(
  12. new Callback<List<Category>>() {
  13. @Override
  14. public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
  15. toastShower.showToast("Categories succesfully retrieved from backend.", getContext());
  16. final GridView gridView = Objects.requireNonNull(getView()).findViewById(R.id.gridViewNewContract);
  17. gridView.setAdapter(new CategoriesAdapter(response.body(), getActivity()));
  18. }
  19. @Override
  20. public void onFailure(Call<List<Category>> call, Throwable t) {
  21. toastShower.showToast("There has been a problem with retrieving the categories data!", getContext());
  22. }
  23. }
  24. );
  25. }

Items inside the grid view

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <androidx.cardview.widget.CardView
  7. android:layout_width="100dp"
  8. android:layout_height="100dp"
  9. app:cardCornerRadius="15dp">
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:layout_gravity="center_horizontal|center_vertical"
  14. android:layout_marginLeft="10dp"
  15. android:layout_marginTop="15dp"
  16. android:layout_marginRight="10dp"
  17. android:layout_marginBottom="15dp"
  18. android:orientation="vertical">
  19. <ImageView
  20. android:id="@+id/iconView"
  21. android:layout_width="50dp"
  22. android:layout_height="50dp"
  23. android:layout_gravity="center_horizontal"
  24. android:src="@mipmap/ic_list"
  25. android:tint="@color/colorAccent" />
  26. <TextView
  27. android:id="@+id/textName"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_gravity="center_horizontal"
  31. android:fontFamily="sans-serif"
  32. android:maxLength="12"
  33. android:text="@string/appName"
  34. android:textColor="@color/colorAccent"
  35. android:textSize="13sp" />
  36. </LinearLayout>
  37. </androidx.cardview.widget.CardView>
  38. </RelativeLayout>

If you need any more information, I will gladly provide it. I can mention that I've tried placing listeners both in the adapter and in the fragment directly on the grid.

英文:

I currently have a grid view using a base adapter inside a fragment and I am trying to transfer to a different fragment when one of the items is clicked but none of the solutions I found on stack overflow has worked. I might miss something.

Adapter

  1. import android.content.Context;
  2. import android.view.LayoutInflater;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.BaseAdapter;
  6. import android.widget.ImageView;
  7. import android.widget.TextView;
  8. import com.licenta.joberfrontend.R;
  9. import com.licenta.joberfrontend.rest.backend_entieties.Category;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. public class CategoriesAdapter extends BaseAdapter {
  13. public class ViewHolder {
  14. TextView textName;
  15. ImageView imageView;
  16. }
  17. private ArrayList&lt;Category&gt; categoryList;
  18. public Context context;
  19. public CategoriesAdapter(List&lt;Category&gt; apps, Context context) {
  20. this.context = context;
  21. this.categoryList = (ArrayList&lt;Category&gt;) apps;
  22. }
  23. @Override
  24. public int getCount() {
  25. return categoryList.size();
  26. }
  27. @Override
  28. public Object getItem(int position) {
  29. return position;
  30. }
  31. @Override
  32. public long getItemId(int position) {
  33. return position;
  34. }
  35. @Override
  36. public View getView(final int position, View view, ViewGroup parent) // inflating the layout and initializing widgets
  37. {
  38. ViewHolder viewHolder;
  39. if (view == null) {
  40. LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  41. view = inflater.inflate(R.layout.category_list_content, parent, false);
  42. viewHolder = new ViewHolder();
  43. viewHolder.textName = view.findViewById(R.id.textName);
  44. viewHolder.imageView = view.findViewById(R.id.iconView);
  45. view.setTag(viewHolder);
  46. } else {
  47. viewHolder = (ViewHolder) view.getTag();
  48. }
  49. // here we are setting up the names and images
  50. viewHolder.textName.setText(categoryList.get(position).getName());
  51. viewHolder.imageView.setImageResource(this.context.getResources().getIdentifier(categoryList.get(position).getCategoryIconId(), &quot;mipmap&quot;, this.context.getPackageName()));
  52. return view;
  53. }
  54. }

Fragment's OnCreate

  1. @Override
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. final LocalStorageSaver localStorageSaver = new LocalStorageSaver(Objects.requireNonNull(getContext()));
  5. final ToastShower toastShower = new ToastShower();
  6. //REST services creation
  7. final RetrofitCreator retrofitCreator = new RetrofitCreator();
  8. final Retrofit retrofit = retrofitCreator.getRetrofit();
  9. final CategoryService categoryService = retrofit.create(CategoryService.class);
  10. final Call&lt;List&lt;Category&gt;&gt; getCategoriesRequest = categoryService.getAllCategoriesAndTheirJobs(localStorageSaver.getValueFromStorage(Constants.TOKEN));
  11. getCategoriesRequest.enqueue(
  12. new Callback&lt;List&lt;Category&gt;&gt;() {
  13. @Override
  14. public void onResponse(Call&lt;List&lt;Category&gt;&gt; call, Response&lt;List&lt;Category&gt;&gt; response) {
  15. toastShower.showToast(&quot;Categories succesfully retrieved from backend.&quot;, getContext());
  16. final GridView gridView = Objects.requireNonNull(getView()).findViewById(R.id.gridViewNewContract);
  17. gridView.setAdapter(new CategoriesAdapter(response.body(), getActivity()));
  18. }
  19. @Override
  20. public void onFailure(Call&lt;List&lt;Category&gt;&gt; call, Throwable t) {
  21. toastShower.showToast(&quot;There has been a problem with retrieving the categories data!&quot;, getContext());
  22. }
  23. }
  24. );
  25. }

Items inside the grid view

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. android:layout_width=&quot;match_parent&quot;
  5. android:layout_height=&quot;match_parent&quot;&gt;
  6. &lt;androidx.cardview.widget.CardView
  7. android:layout_width=&quot;100dp&quot;
  8. android:layout_height=&quot;100dp&quot;
  9. app:cardCornerRadius=&quot;15dp&quot;&gt;
  10. &lt;LinearLayout
  11. android:layout_width=&quot;match_parent&quot;
  12. android:layout_height=&quot;match_parent&quot;
  13. android:layout_gravity=&quot;center_horizontal|center_vertical&quot;
  14. android:layout_marginLeft=&quot;10dp&quot;
  15. android:layout_marginTop=&quot;15dp&quot;
  16. android:layout_marginRight=&quot;10dp&quot;
  17. android:layout_marginBottom=&quot;15dp&quot;
  18. android:orientation=&quot;vertical&quot;&gt;
  19. &lt;ImageView
  20. android:id=&quot;@+id/iconView&quot;
  21. android:layout_width=&quot;50dp&quot;
  22. android:layout_height=&quot;50dp&quot;
  23. android:layout_gravity=&quot;center_horizontal&quot;
  24. android:src=&quot;@mipmap/ic_list&quot;
  25. android:tint=&quot;@color/colorAccent&quot; /&gt;
  26. &lt;TextView
  27. android:id=&quot;@+id/textName&quot;
  28. android:layout_width=&quot;wrap_content&quot;
  29. android:layout_height=&quot;wrap_content&quot;
  30. android:layout_gravity=&quot;center_horizontal&quot;
  31. android:fontFamily=&quot;sans-serif&quot;
  32. android:maxLength=&quot;12&quot;
  33. android:text=&quot;@string/appName&quot;
  34. android:textColor=&quot;@color/colorAccent&quot;
  35. android:textSize=&quot;13sp&quot; /&gt;
  36. &lt;/LinearLayout&gt;
  37. &lt;/androidx.cardview.widget.CardView&gt;
  38. &lt;/RelativeLayout&gt;

If you need any more information I will gladly provided.
I can mention that I've tried placing listeners both in the adapter and in the fragment directly on the grid.

答案1

得分: 0

viewHolder.imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//进行操作
}
});

英文:

please try this

  1. viewHolder.imageView.setOnClickListener(new OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. //do stuff
  5. }
  6. });

huangapple
  • 本文由 发表于 2020年1月3日 22:27:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定