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

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

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

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.licenta.joberfrontend.R;
import com.licenta.joberfrontend.rest.backend_entieties.Category;

import java.util.ArrayList;
import java.util.List;

public class CategoriesAdapter extends BaseAdapter {

public class ViewHolder {
    TextView textName;
    ImageView imageView;
}

private ArrayList<Category> categoryList;
public Context context;

public CategoriesAdapter(List<Category> apps, Context context) {
    this.context = context;
    this.categoryList = (ArrayList<Category>) apps;
}

@Override
public int getCount() {
    return categoryList.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View view, ViewGroup parent) // inflating the layout and initializing widgets
{
    ViewHolder viewHolder;

    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.category_list_content, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.textName = view.findViewById(R.id.textName);
        viewHolder.imageView = view.findViewById(R.id.iconView);
        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) view.getTag();
    }

    // here we are setting up the names and images
    viewHolder.textName.setText(categoryList.get(position).getName());
    viewHolder.imageView.setImageResource(this.context.getResources().getIdentifier(categoryList.get(position).getCategoryIconId(), "mipmap", this.context.getPackageName()));

    return view;
}

}

Fragment's OnCreate

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final LocalStorageSaver localStorageSaver = new LocalStorageSaver(Objects.requireNonNull(getContext()));
    final ToastShower toastShower = new ToastShower();

    //REST services creation
    final RetrofitCreator retrofitCreator = new RetrofitCreator();
    final Retrofit retrofit = retrofitCreator.getRetrofit();
    final CategoryService categoryService = retrofit.create(CategoryService.class);

    final Call<List<Category>> getCategoriesRequest = categoryService.getAllCategoriesAndTheirJobs(localStorageSaver.getValueFromStorage(Constants.TOKEN));

    getCategoriesRequest.enqueue(
            new Callback<List<Category>>() {

                @Override
                public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
                    toastShower.showToast("Categories succesfully retrieved from backend.", getContext());
                    final GridView gridView = Objects.requireNonNull(getView()).findViewById(R.id.gridViewNewContract);
                    gridView.setAdapter(new CategoriesAdapter(response.body(), getActivity()));
                }

                @Override
                public void onFailure(Call<List<Category>> call, Throwable t) {
                    toastShower.showToast("There has been a problem with retrieving the categories data!", getContext());
                }
            }
    );

}

Items inside the grid view

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.cardview.widget.CardView
    android:layout_width="100dp"
    android:layout_height="100dp"
    app:cardCornerRadius="15dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="15dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="15dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/iconView"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal"
            android:src="@mipmap/ic_list"
            android:tint="@color/colorAccent" />

        <TextView
            android:id="@+id/textName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:fontFamily="sans-serif"
            android:maxLength="12"
            android:text="@string/appName"
            android:textColor="@color/colorAccent"
            android:textSize="13sp" />

    </LinearLayout>

</androidx.cardview.widget.CardView>

</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

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.licenta.joberfrontend.R;
import com.licenta.joberfrontend.rest.backend_entieties.Category;
import java.util.ArrayList;
import java.util.List;
public class CategoriesAdapter extends BaseAdapter {
public class ViewHolder {
TextView textName;
ImageView imageView;
}
private ArrayList&lt;Category&gt; categoryList;
public Context context;
public CategoriesAdapter(List&lt;Category&gt; apps, Context context) {
this.context = context;
this.categoryList = (ArrayList&lt;Category&gt;) apps;
}
@Override
public int getCount() {
return categoryList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View view, ViewGroup parent) // inflating the layout and initializing widgets
{
ViewHolder viewHolder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.category_list_content, parent, false);
viewHolder = new ViewHolder();
viewHolder.textName = view.findViewById(R.id.textName);
viewHolder.imageView = view.findViewById(R.id.iconView);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
// here we are setting up the names and images
viewHolder.textName.setText(categoryList.get(position).getName());
viewHolder.imageView.setImageResource(this.context.getResources().getIdentifier(categoryList.get(position).getCategoryIconId(), &quot;mipmap&quot;, this.context.getPackageName()));
return view;
}
}

Fragment's OnCreate

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LocalStorageSaver localStorageSaver = new LocalStorageSaver(Objects.requireNonNull(getContext()));
final ToastShower toastShower = new ToastShower();
//REST services creation
final RetrofitCreator retrofitCreator = new RetrofitCreator();
final Retrofit retrofit = retrofitCreator.getRetrofit();
final CategoryService categoryService = retrofit.create(CategoryService.class);
final Call&lt;List&lt;Category&gt;&gt; getCategoriesRequest = categoryService.getAllCategoriesAndTheirJobs(localStorageSaver.getValueFromStorage(Constants.TOKEN));
getCategoriesRequest.enqueue(
new Callback&lt;List&lt;Category&gt;&gt;() {
@Override
public void onResponse(Call&lt;List&lt;Category&gt;&gt; call, Response&lt;List&lt;Category&gt;&gt; response) {
toastShower.showToast(&quot;Categories succesfully retrieved from backend.&quot;, getContext());
final GridView gridView = Objects.requireNonNull(getView()).findViewById(R.id.gridViewNewContract);
gridView.setAdapter(new CategoriesAdapter(response.body(), getActivity()));
}
@Override
public void onFailure(Call&lt;List&lt;Category&gt;&gt; call, Throwable t) {
toastShower.showToast(&quot;There has been a problem with retrieving the categories data!&quot;, getContext());
}
}
);
}

Items inside the grid view

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;androidx.cardview.widget.CardView
android:layout_width=&quot;100dp&quot;
android:layout_height=&quot;100dp&quot;
app:cardCornerRadius=&quot;15dp&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_gravity=&quot;center_horizontal|center_vertical&quot;
android:layout_marginLeft=&quot;10dp&quot;
android:layout_marginTop=&quot;15dp&quot;
android:layout_marginRight=&quot;10dp&quot;
android:layout_marginBottom=&quot;15dp&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/iconView&quot;
android:layout_width=&quot;50dp&quot;
android:layout_height=&quot;50dp&quot;
android:layout_gravity=&quot;center_horizontal&quot;
android:src=&quot;@mipmap/ic_list&quot;
android:tint=&quot;@color/colorAccent&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/textName&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center_horizontal&quot;
android:fontFamily=&quot;sans-serif&quot;
android:maxLength=&quot;12&quot;
android:text=&quot;@string/appName&quot;
android:textColor=&quot;@color/colorAccent&quot;
android:textSize=&quot;13sp&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;
&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

viewHolder.imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//do stuff
}
});

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:

确定